1

I have a text file with a data structure like this:

01/May/1998:15:28:53    test123 0   383L    281L    399
01/May/1998:14:23:28    doe821  62C 621L    379
01/May/1998:22:10:11    testABC 0   635R    407R    671R    671N    407N    407Q    407L    496L    569

Every data begins with date and time formatted like this: 01/May/1998:15:28:53.

I started with reading the text file, but now I want to convert it into a list. How can I do this? Do I need regex?

Any help would be greatly appreciated.

EDIT: I want this output:

    [
      ['01/May/1998:15:28:53', 'test123', '0', '383L', '281L', '399'],
      ['01/May/1998:14:23:28', 'doe821', '62C', '621L', '379'],
      ['01/May/1998:22:10:11', 'testABC', '0', '635R', '407R', '671R', '671N', '407N', '407Q', '407L', '496L', '569']
    ]

2 Answers 2

2

Calling str.split() on each line will give you:

 ['01/May/1998:15:28:53', 'test123', '0', '383L', '281L', '399']

As in:

with open('textfile') as f:
    for line in f:
        print line.split()

['01/May/1998:15:28:53', 'test123', '0', '383L', '281L', '399']
['01/May/1998:14:23:28', 'doe821', '62C', '621L', '379']
['01/May/1998:22:10:11', 'testABC', '0', '635R', '407R', '671R', '671N', '407N', '407Q', '407L', '496L', '569']

To get each line as one list item:

with open('textfile') as f:
    print f.readlines() # note the newline chars(\n) that may need slicing off

['01/May/1998:15:28:53    test123 0   383L    281L    399\n', '01/May/1998:14:23:28    doe821  62C 621L    379\n', '01/May/1998:22:10:11    testABC 0   635R    407R    671R    671N    407N    407Q    407L    496L    569\n']

To get each line split and within one big list:

with open('textfile') as f:
    print [line.split() for line in f]

[['01/May/1998:15:28:53', 'test123', '0', '383L', '281L', '399'], ['01/May/1998:14:23:28', 'doe821', '62C', '621L', '379'], ['01/May/1998:22:10:11', 'testABC', '0', '635R', '407R', '671R', '671N', '407N', '407Q', '407L', '496L', '569']]
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help. I wanted to have one data entry as one list entry. However, this actually is a better idea, but I need multiple data entries within the list, should I loop over and add the list entry to an array?
can you add an example of the output you want to your post?
Thank you so much! I love you!
0

Assuming your file is called test.data

>>> with open('test.data') as f:
>>>     [x for x in [y.split() for y in f.read().split('\n')]]

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.