Passing a string as an argument to a python script

喜欢而已 提交于 2021-01-28 08:00:22

问题


I want to pass a string of ZPL codes from one python script to another python script. The string becomes malformed when used in the second script. How can I pass a string literal as an argument to another python script without it being malformed?

Original String

^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

Malformed string

XAFO20,20BQ,2,3FDQA,001D4B02107A;1001000;49681207FSFO50,50ADN,36,20FDMAC:

Code where I call the second script

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))

Code that receives the argument

if __name__ == "__main__":
    zplString = str(sys.argv[1])
    print zplString
    printZPL(zplString)

回答1:


If your code needs to be written just as it is (including the rather odd way of stringing together the ZPL code, and calling a separate script via a shell intermediary, and the avoidance of subprocess, for that matter), you can resolve your issue with a few small adjustments:

First, wrap your code string in double-quotes.

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"'

Second, make sure you're actually calling python from the shell:

command = "python script2.py "+label

Finally, if you're concerned about special characters not being read in correctly from the command line, use unicode_escape from codecs.decode to ensure correct transmission.
See this answer for more on unicode_escape.

# contents of second script
if __name__ == "__main__":
    from codecs import decode
    import sys
    zplString = decode(sys.argv[1], 'unicode_escape')
    print(zplString)

Now the call from your first script will transmit the code correctly:

import sys
import os

sys.stdout.flush()
exitCode = os.system(str(command))

Output:

^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



回答2:


Some demo code:

import sys

if __name__ == "__main__":
    for i, arg in enumerate(sys.argv):
        print("{}: '{}'".format(i, arg))

when called like

python test.py ^this^is^a^test

it gives

0: 'test.py'
1: 'thisisatest'

when called like

python test.py "^this^is^a^test"

it gives

0: 'test.py'
1: '^this^is^a^test'

Solution: enclose your parameter string in double-quotes, ie

label = '"' + label + '"'



回答3:


You can put your string inside a double-quotes, or just import the other python script:

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))

b.py

import sys

if __name__ == "__main__":

    first_argument = str(sys.argv[1])
    print(first_argument)

Output

a b c d



来源:https://stackoverflow.com/questions/44079332/passing-a-string-as-an-argument-to-a-python-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!