I’ve got some python (3.11) scripts that suddenly started producing an error when reading a text/csv file even though no changes were made to the script.
error:
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xa0 in position 398631: invalid start byte
Here’s the code that reads the file and produces the error:
with open(sDirPath+'master_devices.lst') as csv_file: #error happens here
reader = csv.reader(csv_file, delimiter=':')
for row in reader:
if (strNode == row[1]):
strNodeIP = row[0]
print("Node info: " + strNode + " " + strNodeIP)
I’m able to keep the error from stopping the script if I do either of these methods:
with open(sDirPath+'master_devices.lst',encoding='windows-1252') as csv_file:
with open(sDirPath+'master_devices.lst',errors='backslashreplace') as csv_file:
I can also point it to an older copy of the master_devices.lst file and it works fine even though the file doesnt appear to have changed (no difference in encoding or anything in ‘diff’)
The older copy and current copy both show up as us-ascii
# file -i master_devices.lst
master_devices.lst: text/plain; charset=us-ascii
I’ve tried finding non-ascii characters in the file but nothing appears:
grep --color='auto' -P -n "[x80-xFF]" master_devices.lst
grep --color='auto' -P -n "[^x00-x7F]" master_devices.lst
Any idea what might be going on that caused the normal process to stop working?