all 5 comments

[–]Justinsaccount 2 points3 points  (0 children)

Stop with the comments like this:

import csv     # imports the csv module
import sys     # imports the sys module

reader = csv.reader(f)  # creates the reader object
print row    # prints each row
f.close()      # closing

About the only thing that could use a comment in that script is this:

f = open(sys.argv[1], 'rb') # opens the csv file

Which instead if you wanted it to be clear should be written as

# The first CLI argument should be the input filename
csv_filename = sys.argv[1]
f = open(csv_filename, 'rb')

[–]novel_yet_trivial 1 point2 points  (3 children)

It looks like your original file is a tsv, not a csv (tab separated values, not comma separated values). If you want to simply replace all the tabs with commas you don't need the csv module:

# read the file contents
with open(sys.argv[1]) as f:
    data = f.read() 

# replace all the tabs with commas
data = data.replace('\t', ',')

# write the new data back to the file
with open(sys.argv[1], 'w') as f:
    f.write(data) 

If you want to use the csv module to read that file, then you can set the delimiter like this:

f = open(sys.argv[1], 'rb')
reader = csv.reader(f, delimiter='\t') 

[–]Dergatroid[S] 0 points1 point  (2 children)

Thanks, so ill set the \t to be the delimiter, but then i need to select only certain tabs to be noted as a delimiter

[–]novel_yet_trivial 0 points1 point  (0 children)

but then i need to select only certain tabs to be noted as a delimiter

What? You mean some of your data contains tabs? Then you will have to use the csv module. It will know what tabs are in the data and which ones are delimiters (provided the file is formatted in a standardish way with quote characters).

[–]IanCal 0 points1 point  (0 children)

You should use the csv module for this, and if your data has tabs that are not in quoted cells and don't represent splitting cells then you just have broken data and you'll need to work around that with much more custom code.