我正在尝试使用python读取CSV文件以用于数据科学,但收到错误消息“未找到年末ail-length-km-km”。
我尝试过将文件直接移动到D驱动器,因此编写路径导向器不会有任何错误。我尝试使用另一个测试文件,它可以工作。
title = "Length of rails"
titlelen = len(title)
print("{:*^{titlelen}}".format(title, titlelen=titlelen+6))
print()
data = np.genfromtxt('d:\rail-length-km-at-end-of-year',
skip_header=1,
dtype=[('year','U10'),('rail_type','U50'),
('length','i8')], delimiter=",",
missing_values=['na','-'],filling_values=[0])
x_public = data[data['rail_type']=='public sector']['year']
y_public = data[data['rail_type']=='public sector']['length']
x_private = data[data['rail_type']=='Private Sector']['year']
y_private = data[data['rail_type']=='Private Sector']['length']
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x_public, y_public, c='b', label='Public Sector')
ax1.plot(x_private,y_private, c='r', label='Private Sector')
plt.ylabel('rail length')
plt.legend(loc='upper left');
plt.show()
Python无法识别文件目标
答案 0 :(得分:0)
\
字符是转义字符。这就是为什么错误消息开头不显示“ r”的原因-它被转义了:
“未找到年底的所有长度。”
\r
是回车符。
要获得正确的路径,您必须通过在其前面添加另一个\
来逃避\
data = np.genfromtxt('d:\\rail-length-km-at-end-of-year',
或仅使用正斜杠
data = np.genfromtxt('d:/rail-length-km-at-end-of-year',