Switch variables in Pythonscript

Post Reply
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Switch variables in Pythonscript

Post by jan_suhr »

Maybe this should go in the scripting group.

I'm trying to run an Executable Command configurator with a Python script that will convert .CSV to .XML In this Python script it call for the input file and the output file. If I hard code that to file and paths it works running in Switch. But if I replace those filepaths with the Switch variables "$1" and "$2" it will not work. I've tried with "%1" and "%2" as well and variants of those two but it never works.

I get the error from Switch that the Python Script can't find the input file.

I suspect that I have to make the Switch variables to Python variables for this to work but I don't know how to do that.

Python script looks like this, I have tried to make Python variables here :-)

Code: Select all

#!/usr/local/bin/python

import csv
import sys

inputfile = "$1"
outputfile = "$2"

csv.register_dialect('custom',
                     delimiter=',',
                     doublequote=True,
                     escapechar=None,
                     quotechar='"',
                     quoting=csv.QUOTE_MINIMAL,
                     skipinitialspace=False)



with open(inputfile) as ifile:
    data = csv.reader(ifile, dialect='custom')
    
    sys.stdout=open(outputfile, 'w')
    
    print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    print "<csv>"
    for record in data:
        print "     <row>"
        for i, field in enumerate(record):
            print "         <col%s>" % i + field + "</col%s>" % i
        print "     </row>"
    print "</csv>"
The Execute Command looks like this:

Code: Select all

#!/bin/bash

python /shellscripts_for_switch/csv2xml.py "$1" "$2"
The python script is named csv2xml.py

I have tried another CLI script that does this job but the Python script has more control over what ends up in the XML, the other one just converts. Its called "csvprintf"

All this is for a flow that will make business cards with QR code and names collected from an XLSX file. I got inspired from the Summer camp on QR-codes.

It looks like this:
Image


Thanks


Jan
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
bens
Advanced member
Posts: 252
Joined: Thu Mar 03, 2011 10:13 am

Re: Switch variables in Pythonscript

Post by bens »

I haven't used python, but a quick search on the interwebs suggests this:

Code: Select all

#!/usr/local/bin/python

import csv
import sys

inputfile = sys.argv[1]
outputfile = sys.argv[2]

...

sys.argv is a list (array) of the command-line arguments to the script. sys.argv[0] is the script name, so sys.argv[1] is the first real argument.

For more information check out http://www.tutorialspoint.com/python/py ... uments.htm, http://www.diveintopython.net/scripts_a ... ments.html, and http://www.diveintopython.net/toc/index.html.
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Switch variables in Pythonscript

Post by jan_suhr »

Thanks Ben,
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
jazzmigar
Newbie
Posts: 1
Joined: Tue Aug 28, 2018 12:22 pm

Re: Switch variables in Pythonscript

Post by jazzmigar »

More on...Python
Post Reply