1

For some reason I am getting this error when trying to run the following code

#!/usr/bin/python
import matplotlib.pyplot as plt
x = []
y = []
readFile = open('Out_0_0.txt','r')
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

When I remove the x.append and y.append lines and just put a print statement to print out what is in xandy it prints out each pair of values in the arrays fine. The text file has this

 1 2
 3 4
 5 6
 7 8

all I want it to do is store the first column in the x array and the second column in the y array

4
  • Why are you splitting the read on new line? .read will automatically read the lines one at a time. Do you maybe mean to do .readlines? Commented Jul 1, 2015 at 14:07
  • 1
    What error are you getting? When I run the code, I get x = [1, 3, 5, 7] and y = [2, 4, 6, 8] Commented Jul 1, 2015 at 14:07
  • I'm guessing (not sure) that you have an empty line at the bottom of the file. Commented Jul 1, 2015 at 14:10
  • Is it possible there is an empty line in the file? a carriage return? Commented Jul 1, 2015 at 14:10

4 Answers 4

4

The issue was in how you're reading in the file.

This will work.

x = []
y = []
with open('test.txt','r') as data_file:
    for plot_pair in data_file:
        xandy = plot_pair.split()
        x.append(int(xandy[0]))
        y.append(int(xandy[1]))
print(x)
print(y)
Sign up to request clarification or add additional context in comments.

Comments

3

You can read this in a bit more efficiently (and safely). The first change I made was to use with when reading in the file. This will automatically handle closing the file when you are done with it.

Next, I've removed the split('\n') as it isn't required. We are, instead, going to loop over the file line by line and just split it at white spaces. Since this is a two column file, we'll do that with this line:

fx,fy = line.split()

I've stuck that in a try/except in case there is an empty line at the end of the file. That will produce a ValueError and the assumption is that this is the end of the file.

Then we append our columns for the row to the final array.

x = []
y = []
with open('test.txt') as f:
    for line in f:
        try:
            fx,fy = line.split()
        except ValueError:  
            break       
        x.append(int(fx))
        y.append(int(fy))

print x
print y

This prints out:

[1, 3, 5, 7]
[2, 4, 6, 8]

Finally, I've removed the matplotlib import. It wasn't required for this example.

Comments

3

Open the file up in an editor like Notepad++ and make sure there are no extraneous lines at the beginning or end of the file you are reading.

Comments

1

something quick... should work (not exactly tested :P)

x = []
y = []
with open(inputFile, 'r') as f:
    for line in f:
        nextX, nextY = map(int, line.split(' '))
        x.append(nextX)
        y.append(nextY)

Another quick thought: make sure the txt file has no empty lines at the end. Perhaps add a if line: check as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.