2

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)
2
  • Both scripts are written in Python, so why involve the shell at all? Just import the relevant functions and pass the data directly. Commented May 19, 2017 at 21:52
  • I'm writing the second script as a library intended to be called by a c# application. Just testing it with another py script. Ideally I'd have a way to handle the string as it gets passed to my (second) script. Otherwise I'd have to specify using quotes in my usage docs. Commented May 19, 2017 at 22:16

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.