I'm having an issue with Python2.7 complaining I do not have encoding declared; however, it is infact declared. I'm running this on OS X El Capitan (10.11.3) and python 2.7.11.
I'm attempting to search a data set for specific Chinese and english terms. The report.csv contains the data which I want to search and the raw_terms.txt contains the Chinese and English terms in new line separated. Both files were saved as UTF-8.
I've noticed this code works on different machines, but not mine. I'm assuming there is something I have changed in the year+ I've had this laptop which is causing this issue, but I'm unsure where to start my search.
Script:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import csv
count = 0
with open('./data/report.csv', 'rb') as c:
csv_data = csv.DictReader(c, delimiter=',', quoting=csv.QUOTE_ALL)
for data in csv_data:
with open('./terms/raw_terms.txt', 'r') as f:
for term in f:
term = term.strip()
if term in data['Description']: #or term in '你好!你好吗':
# print 'Found \"%s\" in \"%s\"' % (term, data['Subject'])
count += 1
else:
continue
print count
Error:
File "t.py", line 1
SyntaxError: Non-ASCII character '\xfe' in file t.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
Appreciate any help/direction anyone can provide.
coding:comment applies to your code, not to the file you are opening from the code. Anyway, the CSV module has well-documented trouble with Unicode -- look for the many, many duplicates here.codecs(codecs.open(file_location, 'rb', 'UTF-8') as f:) module and.encode('unicode-escape'). Also, it's the non-csv file which I'm getting the error for.\xef, not\xfe. Probably your file is in UTF-16. Try to save it as UTF-8 without a BOM.