HenryM HenryM - 1 month ago 10
Python Question

Python3 b'text' instead of u'text'

I've a really simple bit of BeautifulSoup running in Python2.7 and I need to run it in Python3. Output is a csv file. In Python2 I can then import this straight into Excel but in Python3 the fields come across as b'text' and load into cells in that form. I've never touched Python3 so would appreciate some guidance.

Relevant code is:

for d in data:
row = []
name = d.find('a')
addr = d.find('span', attrs = {'class' : 'center_address'})
city = d.find('span', attrs = {'class' : 'center_city'})
state = d.find('span', attrs = {'class' : 'center_state_abbr'})
zip = d.find('span', attrs = {'class' : 'center_zip'})

row.append(str(name.text))
row.append(addr.text)
row.append(city.text)
row.append(state.text)
row.append(zip.text)

list_of_rows.append(row)

with open("./output.csv", "w") as outfile:
writer = csv.writer(outfile)
writer.writerow(["Name","Address", "City","State","Zip"])
writer.writerows([s.strip().encode("utf-8") for s in row ]for row in list_of_rows)
outfile.close()

Answer

You have opened the output file in text mode, which means you should be sending Unicode text strings to it, but you aren't - you are taking the Unicode strings and converting them to encoded byte data.

Removing the encoding step so your code reads

with open("./output.csv", "w") as outfile:
    writer = csv.writer(outfile)
    writer.writerow(["Name","Address", "City","State","Zip"])
    writer.writerows(([s.strip() for s in row ]
                                 for row in list_of_rows))

will work under Python 3.

Comments