【发布时间】:2023-03-05 02:33:01
【问题描述】:
我正在尝试使用 Jupyter 从 csv 文件中导入数据。我是 Python 新手。
它在 IDLE 中运行良好,但在 Jupyter 中不行。
import csv
with open("mpg.txt","r") as mpgFile:
mpgFileReader = csv.reader(mpgFile)
mpgList = []
for row in mpgFileReader:
if len (row)!=0:
mpgList = mpgList + [row]
mpgFile.close()
print(mpgList)
这是我收到的错误
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-60-42823a11b1d0> in <module>()
1 import csv
----> 2 with open("mpg.txt","r") as mpgFile:
3 mpgFileReader = csv.reader(mpgFile)
4 mpgList = []
5 for row in mpgFileReader:
FileNotFoundError: [Errno 2] No such file or directory: 'mpg.txt'
我也试过指定文件的完整路径:
"C:\Users\serdi\Documents\Test\mpg.txt"
但是它给了我另一个错误:
File "<ipython-input-61-fed3bca4876a>", line 2
with open("C:\Users\serdi\Documents\Test\mpg.txt","r") as mpgFile:
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
【问题讨论】:
标签: python csv jupyter python-idle