Reputation: 81
I want to create a list of lists from a file that contains the following data:
101 Rahul
102 Julie
103 Helena
104 Kally
CODE
lis = []
with open("student_details.txt" , "r+") as f:
for i in range(1,3,1):
for data in f.read().split():
lis.append(data)
print(lis)
Output I Want
[[101,Rahul] ,[102,Julie] ,[103,Helena] ,[104,Kally]]
Output I m getting
['101', 'Rahul', '102', 'Julie', '103', 'Helena', '104', 'Kally']
Upvotes: 4
Views: 1073
Reputation: 898
You can split by lines:
lis = []
with open("student_details.txt" , "r+") as f:
for line in f:
data = line.split()
lis.append(data)
print(lis)
Result :
[['101', 'Rahul'], ['102', 'Julie'], ['103', 'Helena'], ['104', 'Kally']]
Or in a more compact way :
with open("student_details.txt" , "r+") as f:
lis = [line.split() for line in f]
print(lis)
Result :
[['101', 'Rahul'], ['102', 'Julie'], ['103', 'Helena'], ['104', 'Kally']]
Upvotes: 1
Reputation: 131
You could try this alternative too. I'm using splitlines() method.
data = []
f = open('student_details.txt', 'r')
for line in f.read().splitlines():
data.append([line])
print(data)
Upvotes: 0
Reputation: 5613
Easiest way is to do it by list
comprehension as the following:
with open("student_details.txt" , "r+") as f:
lis = [line.split() for line in f]
or if you want to loop
you can loop
through the file without reading it:
with open("student_details.txt" , "r+") as f:
lis = []
for line in f:
lis.append(line.split())
Upvotes: 2
Reputation: 4409
Your code:
lis = []
with open("student_details.txt" , "r+") as f:
for i in range(1,3,1):
for data in f.read().split():
lis.append(data)
print(lis)
Note the two for loops, what you are doing is looping through each line, then through each item on that line. You are then pushing each item individually into your list lis
.
To get the output you want, you want to be pushing a list of the two items on each line into your list.
You are actually already generating this list of items with f.read().split()
, so you just need to append this.
This leaves you with the following:
lis = []
with open("student_details.txt" , "r+") as f:
for i in range(1,3,1):
#no second loop here
data = f.read().split()
lis.append(data)
print(lis)
This still doesn't give us the correct answer, the loop is the problem here, we can fix this by just reading each line ideomatically, without specifying any ranges:
lis = []
with open("student_details.txt" , "r+") as f:
for line in f:
#no second loop here
data = line.split()
lis.append(data)
print(lis)
Upvotes: 1
Reputation: 140287
You could read line by line iterating on the handle or using readlines()
, splitting each line, appending each result, etc... Fixing your code would require to remove the range
loop which just reads the file 3 times (but only effectively reads once the first time), and append the splitted items of the lines, using a list comprehension to be more pythonic:
with open("student_details.txt" , "r+") as f:
lis = [line.split() for line in f]
note that the cool thing about line.split()
(without parameters) is that it gets rid of the line terminator(s) automatically.
but you can make it simpler: csv
module does all the parsing for you:
import csv
with open("student_details.txt" , "r") as f:
cr = csv.reader(f,delimiter=" ")
# iterate on rows and generate list of lists:
result = list(cr)
Upvotes: 3