1

I am trying to convert a csv file into another file (file type doesn't matter as the program using the converted data just opens it like a text file).

So far I have managed to convert and print the original csv data into the the data structure I want but I now need to save that as another file.

import csv
file = open('newData', 'w')

with open('initialData.csv', 'rb') as f:
reader = csv.reader(f, delimiter=',', quotechar='|')
for row in reader:
    print row[13] + ' 1:' + row[0] + ' 2:' + row[1]
    file.write(f)

file.close()

Whenever I run this I get the error:

TypeError: expected a character buffer object

I know there is nothing wrong with converting the csv file as that prints fine when I comment out the file.write(f).

Many thanks in advance!

5
  • 2
    Please do include the full traceback when reporting a python error, so we won't have to guess as much. Commented Jan 2, 2013 at 21:37
  • 1
    Your indentation is incorrect. Please ensure that the indentation you see in your question matches your original source code exactly. Commented Jan 2, 2013 at 21:37
  • 1
    Fix your indentation. For SO code samples, it's best to set your editor to indent with spaces, not tabs. Commented Jan 2, 2013 at 21:37
  • 3
    Also, while you're at it, you should use with..as for the output file as well. Also, don't name them file (that's a built-in) and f. Use something like infile and outfile. Commented Jan 2, 2013 at 21:38
  • 1
    You're using csv.reader for the input. Why not use csv.writer for the output? Commented Jan 2, 2013 at 21:51

2 Answers 2

8

Why are you trying to write the original file (the f object) to the new file? Don't you want to write the re-formatted data?

import csv

with open('initialData.csv', 'rb') as f_in, open('newData', 'w') as f_out:
    reader = csv.reader(f_in, delimiter=',', quotechar='|')
    for row in reader:
        print row[13] + ' 1:' + row[0] + ' 2:' + row[1]
        f_out.write(row[13] + ' 1:' + row[0] + ' 2:' + row[1])

Edit: as suggested by Jon Clements, use context manager for output as well + indentation fix.

Sign up to request clarification or add additional context in comments.

3 Comments

Gosh I can't believe I didn't see that before! Thanks for your help!
@mrpopo - don't sweat it, I've made that sort of mistake more times than I care to admit. So easy to overlook.
Couple of changes: with open('in.csv') as f, open('out.csv','wb') as fo (make the most of your with), then don't indent the for
0

You're trying to print out the file handle for the whole csv file. I'm guessing you want the text you're printing to be written out into a file, in that case just do:

with open('initialData.csv', 'rb') as infile, open('newData.txt') as outfile:
    reader = csv.reader(infile, ...)
    for row in reader:
        outfile.write(row[13] + ' 1:' + row[0] + ' 2:' + row[1])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.