Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

pass a python variable to subprocess

pass a python variable to subprocess

pass a python variable to subprocess

I am thinking about a python script to generate a xml file from a csv file, every row in csv is an element for the generated xml file. In order to obtain file value in csv file, I used awk. In example
for i in range(int(total_line)):
     elementName=os.popen("awk -F ':' 'NR==a {print $5}' a=i input.csv").read()

I'd like to fetch the ith row and 5th column value and assign it to python variable. The problem is I need to pass the python variable "i" to awk to select a line. How to solve this problem? Thanks in advance.  

RE: pass a python variable to subprocess


Only to answer your question :


for i in range(int(total_line)):
     elementName=os.popen("awk -F ':' 'NR==a {print $5}' a=%d input.csv" % i).read()
But please give up with that stupidity. If you want to use Python, then use Python :

CODE --> csv2xml.py

import fileinput

print '<root>'
for line in fileinput.input():
  print '  <row>'
  for field in line.rstrip().split():
    print '    <field>%s</field>' % field
  print '  </row>'
print '</root>'

CODE --> input.csv

one two three
four five six
seven eigth nine

CODE --> output



RE: pass a python variable to subprocess

IMHO, instead of reading CSV file with awk and writing XML file with python, you can do both things with python.
In one python program you can read CSV file, parse values from it and write them into XML file.

RE: pass a python variable to subprocess

Thank you very much,  I will consider that use a complete python script for the object.  

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close