将字符串作为参数传递给python脚本
我想将一组ZPL代码从一个python脚本传递到另一个python脚本。在第二个脚本中使用该字符串时,其格式会出现错误。如何将字符串文本作为参数传递给另一个python脚本而不使其格式错误 原始字符串将字符串作为参数传递给python脚本,python,arguments,Python,Arguments,我想将一组ZPL代码从一个python脚本传递到另一个python脚本。在第二个脚本中使用该字符串时,其格式会出现错误。如何将字符串文本作为参数传递给另一个python脚本而不使其格式错误 原始字符串 ^XA^FO20,20^BQ,2,3^FDQA,001D4B02107A;1001000;49681207^FS^FO50,50^ADN,36,20^FDMAC:001D4B02107A^FS^FO50150^ADN,36,20^FDSN:1001000^FS^FO50250^ADN,36,20^
^XA^FO20,20^BQ,2,3^FDQA,001D4B02107A;1001000;49681207^FS^FO50,50^ADN,36,20^FDMAC:001D4B02107A^FS^FO50150^ADN,36,20^FDSN:1001000^FS^FO50250^ADN,36,20^FD代码:49681207^FS^XZ
格式错误的字符串
XAFO20,20BQ,2,3FDQA,001D4B02107A;1001000;49681207FSFO50,50ADN,36,20FDMAC:
我调用第二个脚本的代码
def printLabel():
label = "^XA"+"^FO20,20^BQ,2,3^FDQA,"+"001D4B02107A;1001000;49681207"+"^FS"+"^FO50,50"+"^ADN,36,20"+"^FD"+"MAC: "+"001D4B02107A"+"^FS"+"^FO50,150"+"^ADN,36,20"+"^FD"+"SN: "+"1001000"+"^FS"+"^FO50,250"+"^ADN,36,20"+"^FD" + "Code: "+"49681207"+"^FS"+"^XZ"
command = "zt320print.py "+label
print command
sys.stdout.flush()
exitCode = os.system(str(command))
if __name__ == "__main__":
zplString = str(sys.argv[1])
print zplString
printZPL(zplString)
接收参数的代码
def printLabel():
label = "^XA"+"^FO20,20^BQ,2,3^FDQA,"+"001D4B02107A;1001000;49681207"+"^FS"+"^FO50,50"+"^ADN,36,20"+"^FD"+"MAC: "+"001D4B02107A"+"^FS"+"^FO50,150"+"^ADN,36,20"+"^FD"+"SN: "+"1001000"+"^FS"+"^FO50,250"+"^ADN,36,20"+"^FD" + "Code: "+"49681207"+"^FS"+"^XZ"
command = "zt320print.py "+label
print command
sys.stdout.flush()
exitCode = os.system(str(command))
if __name__ == "__main__":
zplString = str(sys.argv[1])
print zplString
printZPL(zplString)
一些演示代码:
import sys
if __name__ == "__main__":
for i, arg in enumerate(sys.argv):
print("{}: '{}'".format(i, arg))
当被称为
python test.py ^this^is^a^test
python test.py "^this^is^a^test"
它给
0: 'test.py'
1: 'thisisatest'
0: 'test.py'
1: '^this^is^a^test'
当被称为
python test.py ^this^is^a^test
python test.py "^this^is^a^test"
它给
0: 'test.py'
1: 'thisisatest'
0: 'test.py'
1: '^this^is^a^test'
解决方案:将参数字符串用双引号括起来,即
label = '"' + label + '"'
您可以将字符串放在双引号内,或者只导入其他python脚本: a.py
import sys, os
text = "a b c d"
# or '{} {} "{}"'.format("python", "b.py", text)
command = "python b.py \"" + text + "\""
os.system(str(command))
import sys
if __name__ == "__main__":
first_argument = str(sys.argv[1])
print(first_argument)
b.py
import sys, os
text = "a b c d"
# or '{} {} "{}"'.format("python", "b.py", text)
command = "python b.py \"" + text + "\""
os.system(str(command))
import sys
if __name__ == "__main__":
first_argument = str(sys.argv[1])
print(first_argument)
输出
def printLabel():
label = "^XA"+"^FO20,20^BQ,2,3^FDQA,"+"001D4B02107A;1001000;49681207"+"^FS"+"^FO50,50"+"^ADN,36,20"+"^FD"+"MAC: "+"001D4B02107A"+"^FS"+"^FO50,150"+"^ADN,36,20"+"^FD"+"SN: "+"1001000"+"^FS"+"^FO50,250"+"^ADN,36,20"+"^FD" + "Code: "+"49681207"+"^FS"+"^XZ"
command = "zt320print.py "+label
print command
sys.stdout.flush()
exitCode = os.system(str(command))
if __name__ == "__main__":
zplString = str(sys.argv[1])
print zplString
printZPL(zplString)
a b c d
如果您的代码需要按原样编写(包括将ZPL代码串在一起,并通过shell中介调用单独的脚本,以及避免子流程),您可以通过一些小的调整来解决您的问题:
首先,将代码字符串用双引号括起来
label= '"^XA'+"^FO20,20^BQ,2,3^FDQA,"+"001D4B02107A;1001000;49681207"+"^FS"+"^FO50,50"+"^ADN,36,20"+"^FD"+"MAC: "+"001D4B02107A"+"^FS"+"^FO50,150"+"^ADN,36,20"+"^FD"+"SN: "+"1001000"+"^FS"+"^FO50,250"+"^ADN,36,20"+"^FD" + "Code: "+"49681207"+"^FS"+'^XZ"'
其次,确保您实际上是在从shell调用python
:
command = "python script2.py "+label
最后,如果您担心无法从命令行正确读取特殊字符,请使用编解码器中的unicode\u escape
进行解码以确保正确传输。
有关unicode\u escape
的详细信息,请参阅
# contents of second script
if __name__ == "__main__":
from codecs import decode
import sys
zplString = decode(sys.argv[1], 'unicode_escape')
print(zplString)
现在,来自第一个脚本的调用将正确传输代码:
import sys
import os
sys.stdout.flush()
exitCode = os.system(str(command))
输出:
^XA^FO20,20^BQ,2,3^FDQA,001D4B02107A;1001000;49681207^FS^FO50,50^ADN,36,20^FDMAC: 001D4B02107A^FS^FO50,150^ADN,36,20^FDSN: 1001000^FS^FO50,250^ADN,36,20^FDCode: 49681207^FS^XZ
这两个脚本都是用Python编写的,那么为什么还要使用shell呢?只需导入相关函数并直接传递数据。我正在编写第二个脚本,作为一个库,供c#应用程序调用。只是用另一个py脚本测试它。理想情况下,当字符串传递到我的(第二个)脚本时,我会有一种方法来处理它。否则,我必须在我的使用文档中指定使用引号。