Skip to content Skip to sidebar Skip to footer

How To Read A Text File Into Separate Lists Python

Say I have a text file formatted like this: 100 20 the birds are flying and I wanted to read the int(s) into their own lists and the string into its own list...how would I go about

Solution 1:

Essentially, I'm reading the file line by line, and splitting them. I first check to see if I can turn them into an integer, and if I fail, treat them as strings.

defseparate(filename):
    all_integers = []
    all_strings = []
    withopen(filename) as myfile:
        for line in myfile:
            for item in line.split(' '):
                try:
                    # Try converting the item to an integer
                    value = int(item, 10)
                    all_integers.append(value)
                except ValueError:
                    # if it fails, it's a string.
                    all_strings.append(item)
    return all_integers, all_strings

Then, given the file ('mytext.txt')

10020 the birds are flying
2003 banana
hello 4

...doing the following on the command line returns...

>>> myints, mystrings = separate(r'myfile.txt')
>>> print myints
[100, 20, 200, 3, 4]
>>> print mystrings
['the', 'birds', 'are', 'flying', 'banana', 'hello']

Solution 2:

If i understand your question correctly:

import re

def splitList(list):
    ints = []
    words = []
    for item in list:
        if re.match('^\d+$', item):
           ints.append(int(item))
        else:
           words.append(item)
    return ints, words

intList, wordList = splitList(line.split())

Will give you two lists: [100, 20] and ['the', 'birds', 'are', 'flying']

Solution 3:

Here's a simple solution. Note it might not be as efficient as others for very large files, because it iterates over word two times for each line.

words = line.split()
intList = [int(x) for x in words if x.isdigit()]
strList = [x for x in words ifnot x.isdigit()]

Solution 4:

pop removes the element from the list and returns it:

words = line.split()
first = int(words.pop(0))
second = int(words.pop(0))

This is of course assuming your format is always int int word word word ....

And then join the rest of the string:

words = ' '.join(words)

And in Python 3 you can even do this:

first, second, *words = line.split()

Which is pretty neat. Although you would still have to convert first and second to int's.

Post a Comment for "How To Read A Text File Into Separate Lists Python"