Reputation: 11
I have a text file with the following information:
SHRO
BJRD
ZNJK
GRMI
I want to save them in a way that I can load them line by line. I mean, suppose its name is "name", and I want the 4th column like below:
name[3] = "GRMI"
How can I do this?
Upvotes: 0
Views: 70
Reputation: 153
You could do something like this:
with open('filename.txt') as f:
name = f.read().split()
EDIT
According to that answer you've posted, I understand that you simply need to print the name in a line and so, the modified code will be this:
with open('filename.txt') as f:
print f.read().split('\n')[12][:4] # Assuming that all of the those consist of 4 characters.
# That number 12 was just an example.
Otherwise,
with open('filename.txt') as f:
names = f.read().split('\n')
print names[12].split()[0] # Again, that number is an example.
Upvotes: 3
Reputation: 225
You probably want something like
with open('stationlist', 'r') as f:
station_names = [line.split()[0] for line in f]
If all of the station names are four characters, the list comprehension could be replaced by
[line[:4] for line in f]
Upvotes: 0