Remove flag "b" in CSV output

I'm trying to write outputs to a CSV file in Python 3.4, but the CSV file always contains the "b" flags. For example b'The output output1 ', b' Text output2 ', ... I am wondering if there is a way to get rid of the' b 'flags. I understand this is not a problem in Python 2.X.

Here are the codes i used

with open('test.csv', 'w') as f:
    writer = csv.DictWriter(f, ['field'], extrasaction='ignore')
    writer.writeheader()
    test_text = mongo.test.find({'text': text})
    for t in test_text
        writer.writerow({i:v.encode('utf') for i,v in t.items()})

      

Many thanks

------ Updates -----------

Thanks a lot for Tim Pitzker, John Zwink and Warren Walker for helpful comments and answers. Per Warren suggestions if I change my codes to

import csv

data = [chr(0x03d5) + 'oo', 'b' + chr(0x0101) + 'r']

with open('test.csv', 'w') as f:
    writer = csv.writer(f)
    for item in data:
        writer.writerow([item])

      

I get the error

UnicodeEncodeError: 'charmap' codec can't encode character '\u03d5' in position 0: character maps to <undefined>

      

if i change my codes to

import csv

data = [chr(0x03d5) + 'oo', 'b' + chr(0x0101) + 'r']

with open('test.csv', 'w') as f:
    writer = csv.writer(f)
    for item in data:
        writer.writerow([item.encode('utf')])

      

I get outputs with 'b' flags

b'\xcf\x95oo'
b'b\xc4\x81r'

      

Any thoughts on how this is happening and how I can fix it? Thanks again.

------ Updates 2 -----------

Thanks a lot for Warren's solution. The following codes were used:

import csv

data = [chr(0x03d5) + 'oo', 'b' + chr(0x0101) + 'r']

with open('test.csv', 'w', encoding='utf8') as f:
    writer = csv.writer(f)
    for item in data:
        writer.writerow([item])

      

+3


source to share


1 answer


You don't need to directly encode strings; let the writer take care of it. For example, this code:

import csv

data = [chr(0x03d5) + 'oo', 'b' + chr(0x0101) + 'r']

with open('test.csv', 'w') as f:
    writer = csv.writer(f)
    for item in data:
        writer.writerow([item])

      

writes a file

ϕoo
bār

      

with UTF-8 encoding (at least on my system where it locale.getpreferredencoding(False)

returns 'UTF-8'

). To make the encoding explicit, you can set the encoding in the call open

:

    with open('test.csv', 'w', encoding='utf8') as f:

      

If the last line is changed to writer.writerow([item.encode('utf')])

(which converts the lines to bytes

) it produces

b'\xcf\x95oo'
b'b\xc4\x81r'

      



For your example, try changing this line:

        writer.writerow({i:v.encode('utf') for i,v in t.items()})

      

:

        writer.writerow(t)

      

Then if it works, you can replace this:

    for t in test_text
        writer.writerow({i:v.encode('utf') for i,v in t.items()})

      

from

    writer.writerows(test_text)

      

+3


source







All Articles