Daily analysis of data sources into Python, sometimes there will be errors and display garbled code problems, today to review the common errors.
Python code
import pandas as pd
import numpy as np
Df = pd read_csv invest_record_2018. CSV (” “)
Error message
UnicodeDecodeError Traceback (most recent call last)
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._convert_tokens
(pandas/_libs/parsers.c:14858)()
pandas/_libs/parsers.pyx in
pandas._libs.parsers.TextReader._convert_with_dtype
(pandas/_libs/parsers.c:17119)()
Error message shows the file encoding error, so adjust the code
import pandas as pd
import numpy as np
df=pd.read_csv(“invest_record_2018.csv”,encoding=”utf-8″)
Error report still, change the code
import pandas as pd
import numpy as np
df=pd.read_csv(“invest_record_2018.csv”,encoding=”gbk”)
Encoding =” iso-8859-1 “can also be used to stop the error
import pandas as pd
import numpy as np
df=pd.read_csv(“invest_record_2018.csv”,encoding=”ISO-8859-1″)
Neither code will display an error. You can continue writing.
Chinese garbled code problem
df.head()
Preview the first 5 lines, Chinese display garbled code
Continue to change the code
import pandas as pd
import numpy as np
df=pd.read_csv(“invest_record_2018.csv”,encoding=”gbk”)
df.head()
After execution, display is normal, problem solved.
Coding classification
Encoding =” UTF-8 “(encoding=” UTF-8″, encoding=” UTF-8 “, encoding=” UTF-8 “)
Python code
import pandas as pd
import numpy as np
Df = pd read_csv invest_record_2018. CSV (” “)
Error message
UnicodeDecodeError Traceback (most recent call last)
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._convert_tokens
(pandas/_libs/parsers.c:14858)()
pandas/_libs/parsers.pyx in
pandas._libs.parsers.TextReader._convert_with_dtype
(pandas/_libs/parsers.c:17119)()
Error message shows the file encoding error, so adjust the code
import pandas as pd
import numpy as np
df=pd.read_csv(“invest_record_2018.csv”,encoding=”utf-8″)
Error report still, change the code
import pandas as pd
import numpy as np
df=pd.read_csv(“invest_record_2018.csv”,encoding=”gbk”)
Encoding =” iso-8859-1 “can also be used to stop the error
import pandas as pd
import numpy as np
df=pd.read_csv(“invest_record_2018.csv”,encoding=”ISO-8859-1″)
Neither code will display an error. You can continue writing.
Chinese garbled code problem
df.head()
Preview the first 5 lines, Chinese display garbled code
Continue to change the code
import pandas as pd
import numpy as np
df=pd.read_csv(“invest_record_2018.csv”,encoding=”gbk”)
df.head()
After execution, display is normal, problem solved.
Coding classification
Encoding =” UTF-8 “(encoding=” UTF-8″, encoding=” UTF-8 “, encoding=” UTF-8 “)
Read More:
- No code, a solution to the error in the path of reading CSV file by Python: filenotfounderror: [errno 2] no such file or directory: ‘XX. CSV‘
- Solutions to the failure of importing Python 3.7 SSL module
- python reads csv file is an error _csv.Error: iterator should return strings, not bytes (did you open the file in text)
- Python – [encoding] in Python os.system Solution to Chinese garbled code when calling CMD command
- Solve the problem of unable to locate package python3.6 when upgrading from python3.5 to python3.6 in ubantu16.04
- Python export data (CSV format)
- Can’t find Python executable “D:\python3\python.exe”, you can set the PYTHON env variable.
- When reading the CSV file with Python 3, the Unicode decodeerror: ‘UTF-8’ codec can’t decode byte 0xd0 in position 0: invalid con appears
- Python has a bus error while importing tensorflow
- Solution to garbled code problem after importing project in eclipse
- Python error: importerror: DLL load failed: unable to find the specified module solution
- Python module learning-Paramiko-Use python to throw an exception: Authentication failed.
- Pychar reported an error. Unable to set the python SDK in Python 3.8. This SDK appears to be invalid.
- Python — a solution to [error 24: too many open files] under Ubuntu
- Anaconda returns Python 3.7 to Python 3.6
- Encountered a problem — Python — Python 3 uses Sqlalchemy to report an error‘
- Solution to abnormal errors during startup of Python’s Anaconda
- Error in pyinstall package Python program: jinja2.exceptions.templatenotfound: Chart_ Solution to component.html
- Importerror: no module named typing error reporting solution (python2 PIP needs to be backed back from 21)
- An import error is reported in the python. The solution to setting. Pylintrc is invalid