0

I have a csv file that looks like this:

123456,456789,12345,123.45,123456 
123456,456789,12345,123.45,123456
123456,456789,12345,123.45,123456

I am extremly new to Python programming but I'm learning and finding Python to be very useful. I basically want the output to look like this:

123456    456789    12345    123.45    123456
123456    456789    12345    123.45    123456
123456    456789    12345    123.45    123456

Basically, all fields right justified, having fixed length. There are no heading in the csv file.

Here's the code I have tried so far and like I said, I'm very new to Python:

import csv
 with open('test.csv') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
    print(', '.join(row))
    with open('test2.txt', 'wb') as f:
writer = csv.writer(f)
writer.writerows(f)

Any help would be greatly appreciated: Thank You in advance.

  • As in a tab delimited file? Check out this question is so... http://stackoverflow.com/questions/10220412/convert-tab-delimited-txt-file-into-a-csv-file-using-python – Al.Sal Sep 05 '13 at 12:16

2 Answers2

1

OK you have a mess of problems with your code:

  1. Your indentation is all wrong. That's one of the basic concepts of python. Go search the web and read a little about it if you don't understand what I mean
  2. the part that opens 'test2.txt' is inside the loop of spamreader, meaning it is re-opened and truncated for every row in 'test.csv'.
  3. you are trying to write the file to itself with this line: writer.writerows(f) (remember? f is the file you are writing to...)
  4. You are using a csv.writer to write lines to a txt file.
  5. You want a spacing between each item but you're not doing that anywhere in your code

So to sum up all those problems, here's a fixed example, which is really not that far away from your code as it is:

import csv

res = []
# start a loop to collect the data
with open('test.csv') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')
    for row in spamreader:
        line = '\t'.join(row) + '\r\n' # the \n is for linebreaks. \r is so notepad loves you too
        res.append(line)

# now, outside the loop, we can do this:
with open('test2.txt', 'wb') as f:
    f.writelines(res)

EDIT

If you want to control the spacing you can use ljust function like this:

        line = ''.ljust(2).join(row) 

This will make sure there are 2 spaces between each item. space is the default, but if you want to specify what ljust will be using you can add a second parameter to it:

        line = ''.ljust(5, '-').join(row) 

then each line would look like this:

123456-----456789-----12345-----123.45-----123456 

And thanks for Philippe T. who mentioned it in the comments

2nd Edit

If you want a different length for each column you need to predefine it. The best way would be to create a list in the same length as your csv file columns, with each item being the length of that column and last one being the ending of the line (which is convenient because ''.join doesn't do that by itself), then zip it with your row. Say you want a tab for the first column, then two spaces between each of the other columns. Then your code would look like this:

spacing = ['\t', '  ', '  ', '  ', '\r\n']
# ... the same code from before ...
        line = ''.join([j for i in zip(row, spacing) for j in i])
# ... rest of the code ...

The list comprehension loop is a bit convoluted, but think about it like this:

for i in zip(row, spacing): # the zip here equals ==> [(item1, '\t'), (item2, '  ') ...]
    for j in i: # now i == (item1, '\t')
       j # so j is just the items of each tuple

With the list comprehension, this outputs: [item1, '\t', item2, ' ', ... ]. You join that together and thats it.

yuvi
  • 18,155
  • 8
  • 56
  • 93
0

Try this:

import csv
with open('data.csv') as fin, open('out.txt','w') as fout:
    data = csv.reader(fin,delimiter=',')
    resl = csv.writer(fout,delimiter='\t')
    resl.writerows(data)
Developer
  • 8,258
  • 8
  • 49
  • 58