3

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 this in python. I tried

data.append(map(int, line.split()))

that didn't work...any help?

1
  • 2
    map(int, line.split()) applies int to the entire line. What caused you to think this would separate numbers from words? Commented Jan 21, 2012 at 3:02

4 Answers 4

4

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.

def separate(filename):
    all_integers = []
    all_strings = []
    with open(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')

100 20 the birds are flying
200 3 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']
Sign up to request clarification or add additional context in comments.

3 Comments

+1. This use of an exception is perfect. Remove the "I'm not happy..." business. This is good.
Yeah, I read somewhere that exceptions should only be used for exceptional behavior, which isn't really the case here. I removed it.
"exceptions should only be used for exceptional behavior" Not terribly true in Python. True in some languages, but not Python.
3

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']

Comments

2

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 if not x.isdigit()]

Comments

0

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.

3 Comments

your answer is ok. But what about a more generic solution.The case were we dont't know the occurrence of string and integer in file. Like for eg hello 1 2 check in this case your solution will not work
@RanRag, true. I edited in that assumption after reading other answers, the question isn't clear in that regard. If it is always in this format I think the Python 3 one-liner is the way to go though. Assuming he's using Python 3 of course.
Yeah, in that case the one-liner is the best solution.

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.