Python Error please help!

Status
Not open for further replies.

redman06

Smack-Fu Master, in training
75
Hey Guys

New to python wrote this script butr getting error on a mac and im stuck, ant help appreciated!

Code:
UnicodeEncodeError: 'ascii' codec can't encode character '\u2019' in position 370: or

Code:
import csv

supplier = "supplier.csv"
newCSV = "newCSV.csv"
productcodes = "productcodes.txt"
column = 13
skucodes = open(productcodes, "r", encoding='utf8').readlines()
codes = []
for val in skucodes:
    codes.append(val.strip("\n").strip())
supplier = open(supplier, "r", encoding='utf8')
new_csv = open(newCSV, "w", newline = "")
csvwriter = csv.writer(new_csv)
csvreader = csv.reader(supplier)
count = 0
for val in csvreader:
    if(count == 0):
        count += 1
    else:
        if(len(val) > 0):
            if((val[column].strip()) in codes):
                csvwriter.writerow(val)
supplier.close()
new_csv.close()
 

ComradeXavier

Ars Scholae Palatinae
881
Subscriptor++
The problem isn't Python or Mac-specific. It's a question of text encoding.

When you open the input file, you specify that the encoding is UTF8. Therefore it's possible for the values you read to contain Unicode code points.

You don't specify the encoding you want to use for the output file. Apparently, the default is ASCII.

Because Unicode code points are a superset of ASCII, it is possible that your program will read some text that it can't write, and that is what the error indicates.
 

Jonathon

Ars Legatus Legionis
17,644
Subscriptor
[url=http:/civis/viewtopic.php?p=30151713#p30151713:3vle2pxp said:
redman06[/url]":3vle2pxp]Thanks for your quick reply, so I need to specify it when writing the file. Can you point out where to?

Much Appreciated
Assuming this is Python 3.x, the same place you specified it when opening the files you're reading:

Code:
new_csv = open(newCSV, "w", newline = "", encoding="utf8")
 
Status
Not open for further replies.