0

①文末のダブルクォーテーションを出力する方法を教えて下さい。
print(" Cells(2, myRelation) = ""<=""")
print(" Cells(2, myRelation) = """""<=""""")
print(" Cells(2, myRelation) = """""<=""""""""")

出力結果
Cells(2, myRelation) = <=
Cells(2, myRelation) = "<=
Cells(2, myRelation) = "<=

②一方しか使えない?時はどうしたらいいですか?
③一括で変換(vba→python用)できるエディタありますか?
1行づつ、修正しました。
よろしくお願いします。

(参考)

以下は、シングルクォーテーションの時は、ダブルクォーテーション
    ダブルクォーテーションの時は、シングルクォーテーション
で対応しました。
(参考)
Pythonのシングルクォーテーションとダブルクォーテーションの使い分けってどうするの問題
「ExcelのソルバーをVBAで自動化する方法」(最小値)をやってみた。

print("'最大値最小値2*x+y")
print("'制約条件4*x+y <= 9, x+2*y >= 4,2*x-3*y >= -6")
print("'(x,y)=(3/2,3) max=6")
print("'(x,y)=(0  ,2) min=2")
print('Sub maxmin_MySolver()')
print('    Const myRow As Long = 3')
print('    Const startRow As Long = 2')
print('    Dim i As Long')
print('')
print('    myRelation = 3')
print('    myFormula = 4')
print('    myRefer = 5')
print('    mymaxRow = 5')
print('    myminRow = 6')
print('    ')
print('    Cells(1, 1) = 2')
print('    Cells(1, 2) = 1')
print('')
print('    Cells(2, 1) = 4')
print('    Cells(2, 2) = 1')
print('    Cells(2, myRelation) = "<="')
print('    Cells(2, myFormula) = 9')
print('    Cells(2, myRefer) = "=RC1*R5C1+RC2*R5C2"')
print('    Cells(2, myRefer + 1) = "=RC1*R6C1+RC2*R6C2"')
print('')
print('    Cells(3, 1) = 1')
print('    Cells(3, 2) = 2')
print('    Cells(3, myRelation) = ">="')
print('    Cells(3, myFormula) = 4')
print('    Cells(3, myRefer) = "=RC1*R5C1+RC2*R5C2"')
print('    Cells(3, myRefer + 1) = "=RC1*R6C1+RC2*R6C2"')
print('')
print('    Cells(4, 1) = 2')
print('    Cells(4, 2) = -3')
print('    Cells(4, myRelation) = ">="')
print('    Cells(4, myFormula) = -6')
print('    Cells(4, myRefer) = "=RC1*R5C1+RC2*R5C2"')
print('    Cells(4, myRefer + 1) = "=RC1*R6C1+RC2*R6C2"')
print('')
print('    myMaximize = 1')
print('    Cells(5, myRelation) = "max"')
print('    Cells(5, myFormula) = "=R1C1*RC1+R1C2*RC2"')
print('')
print('    SolverReset')
print('    SolverOk setCell:=Cells(mymaxRow, myFormula), _')
print('             MaxMinVal:=myMaximize, _')
print('             ByChange:=Range(Cells(mymaxRow, 1), Cells(mymaxRow, 2)), _')
print('             EngineDesc:="Simplex LP"')
print('')
print('    For i = startRow To startRow + myRow - 1')
print('        SolverAdd CellRef:=Cells(i, myRefer), _')
print('                  Relation:=myFutogo(Cells(i, myRelation)), _')
print('                  FormulaText:=Cells(i, myFormula)')
print('    Next i')
print('    SolverSolve UserFinish:=True')
print('')
print('    myMinimize = 2')
print('    Cells(myminRow, myRelation) = "min"')
print('    Cells(myminRow, myFormula) = "=R1C1*RC1+R1C2*RC2"')
print('')
print('    SolverReset')
print('    SolverOk setCell:=Cells(myminRow, myFormula), _')
print('             MaxMinVal:=myMinimize, _')
print('             ByChange:=Range(Cells(myminRow, 1), Cells(myminRow, 2)), _')
print('             EngineDesc:="Simplex LP"')
print('')
print('    For i = startRow To startRow + myRow - 1')
print('        SolverAdd CellRef:=Cells(i, myRefer + 1), _')
print('                  Relation:=myFutogo(Cells(i, myRelation)), _')
print('                  FormulaText:=Cells(i, myFormula)')
print('    Next i')
print('    SolverSolve UserFinish:=True')
print('')
print('End Sub')
print('Function myFutogo(Moji As String) As Long')
print('    If Moji = "<=" Then')
print('       myFutogo = 1')
print('    ElseIf Moji = ">=" Then')
print('       myFutogo = 3')
print('    End If')
print('End Function')
print("'x:cells(5,1)=1.5")
print("'y:cells(5,2)=3")
print("'max:cells(5,4)=6")
print("'x:cells(6,1)=0")
print("'y:cells(6,2)=2")
print("'min:cells(6,4)=2")
1
  • "VBA から Python に一括で変換できるエディタ" については、別の質問として投稿すべきだと思います。
    – cubick
    2021年4月21日 12:17

2 件の回答 2

1

公式ドキュメントの説明はここですね。
2.4.1. 文字列およびバイト列リテラル

色々書いてあるので長いですが、先ずは以下の部分を覚えれば良いでしょう。

より平易な説明: これらの型のリテラルは、対応する一重引用符 (') または二重引用符 (") で囲われます。また、対応する三連の一重引用符や二重引用符で囲うこともできます (通常、三重クオート文字列: triple-quoted string と呼ばれます)。バックスラッシュ (\) 文字で、本来特別な意味を持つ文字、例えば改行文字、バックスラッシュ自身、クオート文字などを、エスケープできます。

似たような記事はこちら。
Python文字列におけるシングルクォーテーションとダブルクォーテーションの違い

上記リンクが「データベース接続確立エラー」で見えないので類似と思われるもの。
Python3、「''」「""」の使いわけ ただし中で同じ上記リンクを紹介していますが。
Pythonの文字列の使い方
Pythonのシングルクォーテーションとダブルクォーテーションの違い

他にシングルクォーテーションにすべしという記事
Pythonにおけるシングルクォーテーションとダブルクォーテーション:シングルにすべし


①文末のダブルクォーテーションを出力する方法を教えて下さい。

文字列リテラルをダブルクォーテーションで示した時に、その中にダブルクォーテーションを含めたいなら、そのダブルクォーテーションの前にバックスラッシュ\を付ければ良いでしょう。

文末だけに付けるなら:
print(" Cells(2, myRelation) = <=\"")

<=の前後に付けるなら:
print(" Cells(2, myRelation) = \"<=\"")


②一方しか使えない?時はどうしたらいいですか?

何に対して 何が 「一方しか使えない」のでしょう?

おそらくシングルクォーテーションかダブルクォーテーションのどちらかしか使えないということだと思われますが、そうした制約条件や適用対象に関する説明は明確に行ってください。

単純に上記条件ならば、処理内容に依るでしょうが①の回答と同様にバックスラッシュ\を付ければ良いでしょう。
それ以外の条件の時は質問に詳細を追記してください。


③一括で変換(vba→python用)できるエディタありますか?

そういうピンポイントに見えるけれども、実際には範囲が広くて仕様のあいまいな質問は、助言や答えが得難いものです。

②の質問と類似で、何を如何する と 「一括で変換(vba→python用)できる」と言えるのか? について条件が提示されていないし、そうした処理に対する一般的なコンセンサスがありませんね。

実際に「一括で変換(vba→python用)できる」と機能説明に書いてあるエディタがあるかどうか、検索してみてはどうでしょう?

おそらくそのものズバリの機能を1つのキー/クリック/メニュー等で呼び出せるようなエディタは無いと思われます。

その場合、編集するテキストの内容を基に処理を細かく制御できるマクロ機能を備えたエディタなら、そうした処理を実現することが可能と思われます。

ただしそれはエディタである必要はあるのでしょうか?
例えばPython自身でそうした処理を実現しても得られる結果は同等だと考えられます。

0

③行末に¥N追加。pycharmで¥Nを¥nに置換え

myCode=(
'Sub maxmin_MySolver()\n'
'    Const myRow As Long = 3\n'
'    Const startRow As Long = 2\n'
'    Dim i As Long\n'
'    myRelation = 3\n'
'    myFormula = 4\n'
'    myRefer = 5\n'
'    mymaxRow = 5\n'
'    myminRow = 6\n'
'    \n'
'    Cells(1, 1) = 2\n'
'    Cells(1, 2) = 1\n'
'    Cells(2, 1) = 4\n'
'    Cells(2, 2) = 1\n'
'    Cells(2, myRelation) = "<="\n'
'    Cells(2, myFormula) = 9\n'
'    Cells(2, myRefer) = "=RC1*R5C1+RC2*R5C2"\n'
'    Cells(2, myRefer + 1) = "=RC1*R6C1+RC2*R6C2"\n'
'    Cells(3, 1) = 1\n'
'    Cells(3, 2) = 2\n'
'    Cells(3, myRelation) = ">="\n'
'    Cells(3, myFormula) = 4\n'
'    Cells(3, myRefer) = "=RC1*R5C1+RC2*R5C2"\n'
'    Cells(3, myRefer + 1) = "=RC1*R6C1+RC2*R6C2"\n'
'    Cells(4, 1) = 2\n'
'    Cells(4, 2) = -3\n'
'    Cells(4, myRelation) = ">="\n'
'    Cells(4, myFormula) = -6\n'
'    Cells(4, myRefer) = "=RC1*R5C1+RC2*R5C2"\n'
'    Cells(4, myRefer + 1) = "=RC1*R6C1+RC2*R6C2"\n'
'    \n'
'    myMaximize = 1\n'
'    Cells(5, myRelation) = "max"\n'
'    Cells(5, myFormula) = "=R1C1*RC1+R1C2*RC2"\n'
'    \n'
'    SolverReset\n'
'    SolverOk setCell:=Cells(mymaxRow, myFormula), _\n'
'             MaxMinVal:=myMaximize, _\n'
'             ByChange:=Range(Cells(mymaxRow, 1), Cells(mymaxRow, 2)), _\n'
'             EngineDesc:="Simplex LP"\n'
'    \n'
'    For i = startRow To startRow + myRow - 1\n'
'        SolverAdd CellRef:=Cells(i, myRefer), _\n'
'                  Relation:=myFutogo(Cells(i, myRelation)), _\n'
'                  FormulaText:=Cells(i, myFormula)\n'
'    Next i\n'
'    SolverSolve UserFinish:=True\n'
'    \n'
'    myMinimize = 2\n'
'    Cells(myminRow, myRelation) = "min"\n'
'    Cells(myminRow, myFormula) = "=R1C1*RC1+R1C2*RC2"\n'
'    \n'
'    SolverReset\n'
'    SolverOk setCell:=Cells(myminRow, myFormula), _\n'
'             MaxMinVal:=myMinimize, _\n'
'             ByChange:=Range(Cells(myminRow, 1), Cells(myminRow, 2)), _\n'
'             EngineDesc:="Simplex LP"\n'
'    \n'
'    For i = startRow To startRow + myRow - 1\n'
'        SolverAdd CellRef:=Cells(i, myRefer + 1), _\n'
'                  Relation:=myFutogo(Cells(i, myRelation)), _\n'
'                  FormulaText:=Cells(i, myFormula)\n'
'    Next i\n'
'    SolverSolve UserFinish:=True\n'
'    \n'
'End Sub\n'
'Function myFutogo(Moji As String) As Long\n'
'    If Moji = "<=" Then\n'
'       myFutogo = 1\n'
'    ElseIf Moji = ">=" Then\n'
'       myFutogo = 3\n'
'    End If\n'
'End Function\n'
)
print(myCode)
1
  • この記事は質問の詳細化(質問のソースを一回でこれに変換したい)ですか? それともPyCharmでこう出来ましたという記録ですか?
    – kunif
    2021年4月21日 13:11

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。