Character Code Error Reading CSV Data in Python

Asked 2 years ago, Updated 2 years ago, 103 views

I am having trouble loading CSV files in Python (Flask).

Some CSV data is stored in Shift_JIS and the data is enclosed in double quotation.
The error occurs when opening the file in two patterns and retrieving the CSV by comma-separating them.
I always get the following error whenever I see a line, but there is nothing wrong with the data in that line.
Also, if I do the same with Python IDLE, there will be no error, but if I do it with Flask, it will be an error.
If there is a cause, please take care of it.

(1)

 fo=codecs.open(f, "rb", 'shift_jis')
For line info:
    d=line.split(",")

US>Error statement
UnicodeDecodeError: 'shift_jis' codec can't decode bytes in position 298-299:illegal multibyte sequence

(2)

fo=open(f, "rb")
For line info:
    d=line.split(",")

US>Error statement
ProgrammingError: You must not use 8-bit bytes untests you use a text_factory that can intercept 8-bit bytes (like text_factory=str).It is highly recommended that you installed just switch your application to Unicode strings.

python flask

2022-09-30 19:43

1 Answers

If it is an output file such as Excel, the line feed code may be different, so

fo=open(f, 'rU')
For line info:
    d=line.decode('utf-8').split(',')

Why don't you try ?
The option rU can be read in universal line feed text mode (with the line feed code that the file uses).


2022-09-30 19:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.