Reputation: 29
Here's what I have in the text file:
1712 Albert
1703 Benny
1799 Henry
1735 Jessica
1722 Lucas
1701 Luke
1757 William
1777 Jenny
1783 Lucia
1764 Venus
1718 Felicitas
Here's what I have done to read the file but I have no idea how to put them into a list of tuples
def readRecords(filepath):
file = open('Students1.txt', 'r')
filecontent = file.readlines()
file.close()
return filecontent
Here's what the output is supposed to be:
Upvotes: 0
Views: 1152
Reputation: 1
Location = ["1712","1703","1799",...]
Name = ["Albert","Benny","Henry",...]
using a zip function:
z = zip(Location, Name)
print z
Output:
[("1712","Albert"),("1703","Benny"),...]
Upvotes: -2
Reputation: 866
If you want a nice short two line method:
lines = [x.rstrip() for x in open('students.txt')]
output = [tuple(pair.split(' ')) for pair in lines]
print(output)
# [('1712', 'Albert'), ('1703', 'Benny'), ('1799', 'Henry')...
Upvotes: 0
Reputation: 369454
def readRecords(filepath):
result = []
with open('Students1.txt', 'r') as f: # close the file outside of `with`
for line in f: # Iterate over lines
num, name = line.split() # split line into two part
num = int(num) # convert number part to `int`
result.append((num, name))
return result
NOTE: Don't use readlines()
unless you need whole lines at once. You just need a line at a time; iterating a file is enough.
Upvotes: 1
Reputation: 7773
Given the lines as you're reading them from the file, the following will work:
def process_lines(lines):
result = []
for line in lines:
parts = line.strip().split(" ")
result.append(tuple(int(parts[0]), parts[1]))
return result
A list comprehension is possible here, but it requires a lot of straining. Use listcomps when they simplify your code, avoid them when they make it more complicated. (or when they require you to calculate the same value twice)
Upvotes: 0
Reputation: 2295
def readRecords(filepath):
rst = []
with open(filepath) as f:
data = f.read().splitlines()
rst = [(int(x.split(' ')[0]), x.split(' ')[1]) for x in data]
return rst
Upvotes: 1
Reputation: 11487
Just open the file, and then split these lines, I realized that the result you need is like (int, string)
,so you can use map
to do this and then return tuple:
with open('out.csv') as f:
print([tuple(map(lambda x: int (x) if x.isdigit() else x,i.split())) for i in f])
Output:
[(1712, 'Albert'), (1703, 'Benny'), (1799, 'Henry'), (1735, 'Jessica'), (1722, 'Lucas'), (1701, 'Luke'), (1757, 'William'), (1777, 'Jenny'), (1783, 'Lucia'), (1764, 'Venus'), (1718, 'Felicitas')]
Upvotes: 1
Reputation: 3012
You must iterate over the lines in the file you just read. Here's what you could do:
def readRecords(filepath):
file = open(filepath, 'r') # You should use the filepath variable you've given
filecontent = file.readlines()
file.close()
my_tuples = []
for line in filecontent: # For each line in the file...
line = line.strip() # Remove the newline character which you likely don't want
my_tuples.append(int((line.split(' ')[0]), line.split(' ')[1])) # Create a tuple of the two words separated by a space
return my_tuples
print(readRecords('Students1.txt'))
Upvotes: 0