简体   繁体   English

Jupyter Notebook编码错误?

[英]Jupyter Notebook encoding error?

I started to learn pandas by following this tutorial: 我按照此教程开始学习熊猫:

https://github.com/jvns/pandas-cookbook https://github.com/jvns/pandas-cookbook

Right in the first chapter I try very elementary example of reading a csv file. 在第一章中,我尝试了一个非常简单的读取csv文件的示例。 The example goes like this: 该示例如下所示:

import pandas as pd
broken_df = pd.read_csv("..\data\bikes.csv")

I get a lengthy error message, which ends with a line: 我收到一条冗长的错误消息,其结尾为一行:

FileNotFoundError: File b'..\\data\x08ikes.csv' does not exist

So although I write 'bikes.csv', which I have in the correct folder, the program seems to be searching for a file called 'x08ikes.csv'. 因此,尽管我在正确的文件夹中编写了“ bikes.csv”,但该程序似乎正在搜索名为“ x08ikes.csv”的文件。 Could this be an encoding error? 这可能是编码错误吗? sys.getdefaultencoding() returns 'utf-8'. sys.getdefaultencoding()返回'utf-8'。

I am using Anaconda3 for 64bit Windows, version 4.4.0. 我正在为64位Windows版本4.4.0使用Anaconda3。 My browser is Brave. 我的浏览器是Brave。 Any ideas what is going wrong here? 任何想法出什么问题了吗?

The backslash character '\\' has special meaning; 反斜杠字符'\\'具有特殊含义; it tries to "escape" the next character. 它试图“转义”下一个字符。 In this case '\\b' is an escape character that does have a meaning. 在这种情况下, '\\b'是确实具有含义的转义字符。 There are three ways around this: 有三种解决方法:

Escape the escapes: 逃脱逃生:

You can use the backslash to escape the next backslash, telling Python "this is just another character" 您可以使用反斜杠转义下一个反斜杠,告诉Python“这只是另一个字符”

broken_df = pd.read_csv("..\\data\\bikes.csv")

Use a raw string: 使用原始字符串:

Placing r at the beginning of a string tells Python to interpret everything in the string as-is r放在字符串的开头告诉Python按照原样解释字符串中的所有内容

broken_df = pd.read_csv(r"..\data\bikes.csv")

Use forward slashes: 使用正斜杠:

This is specific to file paths. 这特定于文件路径。 You can trace the directory to you file using forward slashes instead of backslashes. 您可以使用正斜杠而不是反斜杠将目录跟踪到文件。

broken_df = pd.read_csv("../data/bikes.csv")

What you can do is, upload the bikes.csv in to the Jupyter Home "Files" tab. 您可以做的是,将bikes.csv上传到Jupyter主页的“文件”选项卡。 Open it and you may still see the message. 打开它,您可能仍然会看到该消息。 Then go to File->New, and you may get a new blank file. 然后转到“文件”->“新建”,您可能会得到一个新的空白文件。 Open the original bikes.csv in notepad, copy and paste the content in to the file in jupyter notebook. 在记事本中打开原始的bikes.csv ,将内容复制并粘贴到jupyter笔记本中的文件中。 This may help to resolve it. 这可能有助于解决它。

Then you can run the following code. 然后,您可以运行以下代码。

import pandas as pd
broken_df = pd.read_csv("..\data\bikes.csv")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM