Unicode error when opening CSV file in Python with Try, Exception statement

Asked 2 years ago, Updated 2 years ago, 96 views

Re-upload with a little more complement from the previous question. Code that reads CSV files from the specified folder and writes columns to a new CSV file. If you open the file with try and get an error, try reading utf-8 with exception.

import csv
import glob
import os
lst=[]
files=glob.glob('C:/dataset/*.csv')
with open('test2.csv','w',encoding='cp949',newline='') as testfile:
    csv_writer=csv.writer(testfile)
    for file in files:
        try:
            with open(file,'r') as infile:
                file=file[file.rfind('\\')+1:]
                reader=csv.reader(infile)
                headers=next(reader) 
                headers=[str for str in headers if str] 
                while len(headers) < 3 :
                    headers=next(reader) 
                    headers=[str for str in headers if str]
                lst=[file]+headers
                csv_writer.writerow(lst)
        except:
             with open(file,'r',encoding='utf8') as infile:
                file=file[file.rfind('\\')+1:]
                reader=csv.reader(infile)
                headers=next(reader)
                headers=[str for str in headers if str]
                while len(headers) < 3 :
                    headers=next(reader) 
                    headers=[str for str in headers if str]
                lst=[file]+headers
                csv_writer.writerow(lst)

This results in the following error:

Traceback (most recent call last):
  File "C:\Python35\2.py", line 15, in <module>
    headers=next(reader)
UnicodeDecodeError: 'cp949' codec can't decode byte 0xec in position 6: illegal multibyte sequence

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python35\2.py", line 23, in <module>
    with open(file,'r',encoding='utf8') as infile:
FileNotFoundError: [Errno 2] Nouch file or directory: As of January 2010.csv'

Is it because the file opened in Try cannot be found in exception because the file opened in Try is not closed? I know that using the With statement doesn't matter, but why does the above error occur?

python csv utf-8 unicode

2022-09-21 21:48

2 Answers

Resolved


2022-09-21 21:48

Try using the chardet module.

https://pypi.python.org/pypi/chardet

import chardet

s = "Ga-da-da-da-

cp949 = s.encode('cp949')
utf8 = s.encode('utf-8')

print(chardet.detect(cp949)['encoding'])
print(chardet.detect(utf8)['encoding'])

EUC-KR
utf-8


2022-09-21 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.