我试图读取存储在数据文件中的字符串的文件名。那里没问题。如果我将它传递给genfromtxt,就会得到错误"IOError: Z:\Python input.txt not“。如果我显式地将文件名放入genfromtxt中,它就能工作。
这在错误"IOError: z:\Python input.txt not“中失败。
import numpy
modat = open('z:\python\mot1 input.txt') # open file with names in
rbfile = modat.readline() # read data file name
print rbfile # print the file name
rb = numpy.genfromtxt(rbfile, delimiter =',')
print rb但他的作品
import numpy
modat = open('z:\python\mot1 input.txt') # open file with names in
rbfile = modat.readline() # read data file name
print rbfile
rb = numpy.genfromtxt('z:\python\Rb input.txt', delimiter =',')
print rb两个打印语句
%run "c:\users\ian\appdata\local\temp\tmpkiz1n0.py"
Z:\Python\Rb input.txt
[[ 2. 10.]
[ 3. 11.]
[ 5. 13.]
[ 10. 15.]
[ 15. 16.]
[ 20. 16.]
[ 30. 22.]]这似乎与字符串的传递有关--有什么建议吗?
发布于 2014-01-29 13:11:59
rbfile在末尾有一个行尾字符(例如\r\n) .剥去它:
rb = numpy.genfromtxt(rbfile.strip(), delimiter =',')顺便说一句,要调试字符串的问题,打印字符串的repr通常比字符串本身更有用:
print(repr(rbfile))因为repr将更清楚地显示字符,如'\r\n'。
file.readline()不删除EOF字符:
f.readline()从文件中读取一行;字符串末尾保留一个换行符(\n),如果文件没有以换行符结尾,则只在文件的最后一行中省略。这使得返回值明确无误;
https://stackoverflow.com/questions/21432023
复制相似问题