User-specified csv reading and printing in Python 3
I have a script that should read in a user-specified 2-column csv file
located in the same directory, do some maths on the data, and print the
results out to stdout which is another user-defined csv file. I made it in
2.7.5 but now having just finally moved over to 3.2, it doesn't work. Not
a shock I know but I'm pretty inexperienced in programming and I'm having
trouble working out how to make it work in Python 3.
I've stripped back the code to its basic reading and printing elements
that used to work and it still isn't working in 3.2 despite doing some of
the more obvious modifications, it just gets stuck and never prints to the
new file. I have tried using 2to3.py but it throws up a bad input
parseerror and no changes are made! I'd rather learn exactly why it now
doesn't work anyway.
The type of command I want is;
somedirectory>myscript inputdata.csv > outputdata.csv
Here's a stripped-back version of the Python 2.7.5 script that worked so
you can see what i am doing, (I have left in all the imports)
import fileinput, math, sys, numpy as np
from numpy import linspace, loadtxt, ones, convolve
from optparse import OptionParser
def main():
parser = OptionParser()
options,args = parser.parse_args()
try:
data = [(line.rstrip()).split(',') for line in fileinput.input(args)]
except IOError as detail:
print >> sys.stderr, detail
sys.exit(2)
'''kept these lines in just to make sure the data is in the same
format as when it worked before'''
t = [float(row[0]) for row in data]
m = [float(row[1]) for row in data]
result = [[a,b] for a,b in zip(t, m)]
for line in result:
print >> sys.stdout, str(line[0]) + ',' + str(line[1])
options = 0
if __name__ == "__main__":
main()
Obviously Print is now a function, deprecated Optparse can be converted
over to Argparse etc
so I thought something like this would be fine,
import fileinput, math, sys, inspect, numpy as np
from numpy import linspace, loadtxt, ones, convolve
from argparse import ArgumentParser
def main():
parser = ArgumentParser()
options,args = parser.parse_args()
try:
data = [(line.rstrip()).split(',') for line in fileinput.input(args)]
except IOError as detail:
print(detail, file=sys.stderr)
sys.exit(2)
'''kept these lines in just to make sure the data is in the same
format as when it worked before'''
t = [float(row[0]) for row in data]
m = [float(row[1]) for row in data]
result = [[a,b] for a,b in zip(t, m)]
for line in result:
print(str(line[0]) + ',' + str(line[1]), file=sys.stdout)
options = 0
if __name__ == "__main__":
main()
There are probably some glaring issues with this but after researching all
the elements I'm stuck on where exactly it's breaking.
Additionally there are probably better ways of doing this type of csv-file
reading and writing, i just stuck with this as I know it did work. I know
about the csv module for example, but i cannot find any examples of using
it on user-specified files from command line like how i need it, all the
examples I can find define the file to be opened in the script itself.
Thank you in advance.
No comments:
Post a Comment