The solution to the error of importing CSV from Python to Python

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 “)

Read More: