I have a file that's poorly formatted, if I try to open it with simply open('data.csv', 'r')
I get :
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in
position 4681: character maps to <undefined>
So I used open('data.csv', 'rb')
instead and it works fine. Then I tried to transfer the needed information to a new file without success using:
with open('datacsv', 'rb') as file, open('new.csv', 'w') as newf:
for f in file:
newf.write(str(f.split(',')[0:5]))
If I take off the split()
it writes the data to the new file fine, but if I add the split which I'm using to extract first few columns I get:
TypeError: 'str' does not support the buffer interface
I tried the suggestions in here TypeError: 'str' does not support the buffer interface but none of them help.
How else can I prevent the TypeError
from rising?