Python NumPy 的 loadtxt() 的文件路径名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16445311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:38:01  来源:igfitidea点击:

File path name for NumPy's loadtxt()

pythonfilenumpy

提问by astromax

I was wondering if somebody had some information on how to load a CSV file using NumPy's loadtxt(). For some reason it claims that there is no such file or directory, when clearly there is. I've even copy/pasted the full path (with and without the leading / for root), but to no avail.

我想知道是否有人有一些关于如何使用 NumPy 的 loadtxt() 加载 CSV 文件的信息。出于某种原因,它声称没有这样的文件或目录,但显然存在。我什至复制/粘贴了完整路径(有和没有前导 / 为 root),但无济于事。

from numpy import *
FH = loadtxt("/Users/groenera/Desktop/file.csv")

or

或者

from numpy import *
FH = loadtxt("Users/groenera/Desktop/file.csv")

The documentation for loadtxtis very unhelpful about this.

loadtxt文档对此非常没有帮助。

采纳答案by askewchan

This is probably not a loadtxtproblem. Try simply

这可能不是loadtxt问题。简单尝试

f = open("/Users/groenera/Desktop/file.csv")

to make sure it is loadtxt's fault. Also, try using a Unicode string:

以确保它是loadtxt错的。另外,尝试使用 Unicode 字符串:

f = open(u"/Users/groenera/Desktop/file.csv")

回答by repos

You might have forgot the double slash, "//". Some machines require this.

您可能忘记了双斜杠“//”。有些机器需要这个。

So instead of

所以代替

FH = loadtxt("/Users/groenera/Desktop/file.csv")

do this:

做这个:

FH = loadtxt("C:\Users\groenera\Desktop\file.csv")

回答by SUNITHA K

I am using PyCharm, Python 3.5.2.

我正在使用PyCharm,Python 3.5.2。

Right-click on your project and open a file with 'planet.csv' and paste your text.

右键单击您的项目并打开一个包含“planet.csv”的文件并粘贴您的文本。

Add a header to each column.

为每列添加一个标题。

Code:

代码:

import pandas as pd
data = pd.read_csv('planet.csv',sep = "\n")
print (data)