Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

python - How do you access your csv file that u have upload in google colab?

I follow this website and until the second last line, it works well.

I encounter a error during

sample_df = pd.read_csv(io.StringIO(uploaded['sa.csv'].decode('utf-8')))
sample_df.head()

For sample_df = pd.read_csv(io.StringIO(uploaded['sa.csv'].decode('utf-8'))), it stated this:

UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-44-c79110307396> in <module>()
----> 1 sample_df = pd.read_csv(io.StringIO(uploaded['sa.csv'].decode('utf-8')))

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 2716736: invalid continuation byte

For sample_df.head(), it stated this:

NameError                                 Traceback (most recent call last)
<ipython-input-43-c589eab13420> in <module>()
----> 1 sample_df.head()

NameError: name 'sample_df' is not defined

Can someone help me pls with this problem?

question from:https://stackoverflow.com/questions/65641084/how-do-you-access-your-csv-file-that-u-have-upload-in-google-colab

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your sample_df = pd.read_csv(io.StringIO(uploaded['sa.csv'].decode('utf-8'))) didnot execute it gave encoding error!

So sample_df was not created, that is why you got the error NameError: name 'sample_df' is not defined

You can try: pd.read_csv('file', encoding = "ISO-8859-1")

You can also use one of several?alias?options like?'latin'?instead of?'ISO-8859-1'?(see?python docs, also for numerous other encodings you may encounter).

See?relevant Pandas documentation,?python docs examples on csv files, and plenty of related questions here on SO. A good background resource is?What every developer should know about unicode and character sets.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...