0

I have a .txt file with info on just about each line. I'd like to create a list of everything, and have each list item be a list with each line of info separated by a br. visually, im given this:

234
CS434
3.00
M
09/32/394/23 - 232/32/34

435
eS234
4.01
G
09/44/346/01 - 123/23/54

979
js443
3.03
E
09/23/111/24 - 344/23/22

and I would like the pprint to look something like this:

[
[[234],[CS434],[3.00],[M],[09/32/394/23 - 232/32/34]],
[[435],[eS234],[4.01],[G],[09/44/346/01 - 123/23/54]],
[[979],[js443],[3.03],[E],[09/23/111/24 - 344/23/22]]
]

1 Answer 1

3
>>> [x.rstrip('\n').split('\n') for x in open('file').read().split('\n\n')]
[['234', 'CS434', '3.00', 'M', '09/32/394/23 - 232/32/34'],
 ['435', 'eS234', '4.01', 'G', '09/44/346/01 - 123/23/54'],
 ['979', 'js443', '3.03', 'E', '09/23/111/24 - 344/23/22']]

Or, making sure that the file is closed:

with open('file') as f:
    r = [x.rstrip('\n').split('\n') for x in f.read().split('\n\n')]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.