Reputation: 1
I am having trouble storing each line as a separate variable. I understand how to read a file and parse each line but I am having trouble storing them separately.
def setOffsets():
data = urllib2.urlopen("http://blahblahblah.net/file.txt")
for line in data:
The file would contain data like this:
1234
4321
1234
Is there anyway possible to have line 1 stored as x, line 2 stored as y and so on? I looked at other methods and they store the data as lists and arrays which I am simply not interested in. I am only dealing with a few lines.
Upvotes: 0
Views: 2308
Reputation: 73460
According to the docs, urlopen()
returns a file-like object. Hence, methods such as readline()
(returns the next line) and readlines()
(returns a list of all lines as bytestrings) are available:
x, y, z = data.readlines() # as pointed out in the comments by @zondo
# equivalent, but more robust if more lines than wanted are present
x = data.readline()
y = data.readline()
z = data.readline()
Upvotes: 1