EDIT: I put it in the title, but just realized I didn't mention it in the body. This seems to be specific to Windows.
I'm having a hard time writing output using the csv
Python module in a script that works with both Python 2.7 and 3.3.
First try which works as expected in Python 2.7:
with open('test.csv', 'wb') as csv_file:
writer = csv.DictWriter(csv_file, ['header1', 'header2'])
writer.writeheader()
for item in items:
writer.writerow(item)
However, when that same thing is run in Python 3.3 you wind up with:
TypeError: 'str' does not support the buffer interface
So I change 'wb'
to 'wt'
and it runs, but now I have an extra blank row every other line in the file.
To fix that, I change:
with open('test.csv', 'wt') as csv_file:
to:
with open('test.csv', 'wt', newline='') as csv_file:
But now, it breaks Python 2.7:
TypeError: 'newline' is an invalid keyword argument for this function
I know I could just do something like:
try:
with open('test.csv', 'wt', newline='') as csv_file:
writer = csv.DictWriter(csv_file, ['header1', 'header2'])
writer.writeheader()
for item in items:
writer.writerow(item)
except TypeError:
with open('test.csv', 'wb') as csv_file:
writer = csv.DictWriter(csv_file, ['header1', 'header2'])
writer.writeheader()
for item in items:
writer.writerow(item)
However, that has some seriously bad duplication.
Does anyone have a cleaner way of doing this?
EDIT: The test data is simple and has no newlines or anything:
items = [{'header1': 'value', 'header2': 'value2'},
{'header1': 'blah1', 'header2': 'blah2'}]
A CSV file object should be opened with newline=” otherwise, newline characters inside the quoted fields will not be interpreted correctly. csv. writer class provides two methods for writing to CSV. They are writerow() and writerows() .
Steps for writing a CSV file First, open the CSV file for writing ( w mode) by using the open() function. Second, create a CSV writer object by calling the writer() function of the csv module. Third, write data to CSV file by calling the writerow() or writerows() method of the CSV writer object.
To write to a CSV file in Python, we can use the csv. writer() function. The csv. writer() function returns a writer object that converts the user's data into a delimited string.
I've tried a few ways. As far as I can see, simple using 'w'
could be a solution:
with open('test.csv', 'w') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=['header1', 'header2'], lineterminator='\n')
# write something
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With