I have to get a graphic from this code, but it seems that something is not working with it.
我必须从这段代码中获得一个图形,但似乎有些东西不能使用它。
When I run the code I get this:
当我运行代码时,我得到:
ValueError: invalid literal for int() with base 10: ''
This is the code:
这是代码:
import matplotlib.pyplot as plt
x=[]
y=[]
readFile = open("C:/Users/Martinez/Documents/Diego/Python/SampleData.txt","r")
for linea in readFile:
print linea
sepFile = readFile.read().split("\n")
readFile.close()
for plotPair in sepFile:
xAndY = plotPair.split(',')
x.append(int(xAndY[0]))
y.append(int(xAndY[1]))
print x
print y
1 个解决方案
#1
3
Your problem is that you are reading each line of the input file in the first for linea in readFile
loop. When you try to read the contents again you are only going to get an empty string. Either eliminate the first for loop, or add readFile.seek(0)
before the line sepFile = readFile.read().split("\n")
您的问题是您正在读取readFile循环中linea的第一行输入文件的每一行。当您试图再次读取内容时,您将得到一个空字符串。或者消除第一个for循环,或者在换行符= readFile.read().split(“\n”)之前添加readFile.seek(0)
A working version of your program would be
您的程序的一个工作版本将是。
x = []
y = []
with open("C:/Users/Martinez/Documents/Diego/Python/SampleData.txt") as read_file:
for line in read_file:
print line
a, b = line.split(',')
x.append(int(a))
y.append(int(b))
print x
print y
To demonstrate the problem a bit further:
为了进一步证明这个问题:
>>> read_file = open('inp.txt')
>>> for line in read_file: # reads entire contents of file
... print line
...
3,4
5,6
7,8
9,10
>>> read_file.read() # trying to read again gives an empty string
''
>>> out = read_file.read()
>>> int(out) # empty string cannot be converted to an int
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>> read_file.seek(0) # moves to beginning of file
>>> read_file.read() # now the content can be read again
'3,4\n5,6\n7,8\n9,10\n'
#1
3
Your problem is that you are reading each line of the input file in the first for linea in readFile
loop. When you try to read the contents again you are only going to get an empty string. Either eliminate the first for loop, or add readFile.seek(0)
before the line sepFile = readFile.read().split("\n")
您的问题是您正在读取readFile循环中linea的第一行输入文件的每一行。当您试图再次读取内容时,您将得到一个空字符串。或者消除第一个for循环,或者在换行符= readFile.read().split(“\n”)之前添加readFile.seek(0)
A working version of your program would be
您的程序的一个工作版本将是。
x = []
y = []
with open("C:/Users/Martinez/Documents/Diego/Python/SampleData.txt") as read_file:
for line in read_file:
print line
a, b = line.split(',')
x.append(int(a))
y.append(int(b))
print x
print y
To demonstrate the problem a bit further:
为了进一步证明这个问题:
>>> read_file = open('inp.txt')
>>> for line in read_file: # reads entire contents of file
... print line
...
3,4
5,6
7,8
9,10
>>> read_file.read() # trying to read again gives an empty string
''
>>> out = read_file.read()
>>> int(out) # empty string cannot be converted to an int
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>> read_file.seek(0) # moves to beginning of file
>>> read_file.read() # now the content can be read again
'3,4\n5,6\n7,8\n9,10\n'