简体   繁体   中英

Create a function which reads all lines of code from text file and put each line into tuples. -Python 3

So far I have this:

f = open('studs.txt')

with open('studs.txt') as inputFile:
    tupless = [tuple(line.split()) for line in inputFile.readlines()]
    print(tupless)

The text file format I'm opening is like this:
12345 5 G421 Hello Sir
12345 2 G782 Byeee Sir 

So with this code it prints out something like this:
[('12345', '5', 'G421', 'Hello', 'Sir'), ('12345', '2', 'G782', 'Byeee' ,'Sir']

Now I want an output like this:
['12345','5','G421', 'Hello', 'Sir'] \n
['12345','2','G782', 'Byeee' ,'Sir']
etc.

Can anyone steer me in the right direction please?

with open("studs.txt") as f:
    for line in f:
        print(line.split())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM