11

I have a text document that contains a list of numbers and I want to convert it to a list. Right now I can only get the entire list in the 0th entry of the list, but I want each number to be an element of a list. Does anyone know of an easy way to do this in Python?

1000
2000
3000
4000

to

['1000','2000','3000','4000']
2
  • 4
    Do you really want ['1000','2000','3000','4000']? Maybe [1000,2000,3000,4000] would be better? Commented Mar 30, 2010 at 13:15
  • Your question admits of too many possibilities. Does the text file only contain a list of numbers, or is that list in a larger context. Do you get to control how you read from the document or are you stuck with having read a string that contains a bunch of newline or whitespace separated numbers? Are the numbers in the list always separated by newlines, or are they sometimes separated by other whitespace? Do you really want a list of strings as your result, or would a list of integers be better? Commented Mar 30, 2010 at 13:17

7 Answers 7

26

To convert a Python string into a list use the str.split method:

>>> '1000 2000 3000 4000'.split()
['1000', '2000', '3000', '4000']

split has some options: look them up for advanced uses.

You can also read the file into a list with the readlines() method of a file object - it returns a list of lines. For example, to get a list of integers from that file, you can do:

lst = map(int, open('filename.txt').readlines())

P.S: See some other methods for doing the same in the comments. Some of those methods are nicer (more Pythonic) than mine

Sign up to request clarification or add additional context in comments.

5 Comments

you're using str.split, not string.split, the latter is obsolete anyway.
File objects are iterable, so there is seldom a reason to use the readlines method. For example, you could get the same results as your last snippet with map(int, open('filename.txt')).
List comprehension is usually faster than map built-in function plus if the file is big enough iteration over file will be faster and more memory efficient than readlines: lst = [int(line) for line in open('filename.txt')]
lst = [int(line) for line in open('filename.txt')] is much better (and more efficient) idiomatic Python for your second example. chuckle I should read the other comments before I post one.
This will not close the file. The correct way should be: with open('filename.txt') as f: lst = map(int, f.readlines())
1
    $ cat > t.txt
    1
    2
    3
    4
    ^D
    $ python
    Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51) 
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> l = [l.strip() for l in open('t.txt')]
    >>> l
    ['1', '2', '3', '4']
    >>> 

Comments

1
>>> open("myfile.txt").readlines()
>>> lines = open("myfile.txt").readlines()
>>> lines
['1000\n', '2000\n', '3000\n', '4000\n']
>>> clean_lines = [x.strip() for x in lines]
>>> clean_lines
['1000', '2000', '3000', '4000']

Or, if you have a string already, use str.split:

>>> myfile
'1000\n2000\n3000\n4000\n'
>>> myfile.splitlines()
['1000', '2000', '3000', '4000', '']

You can remove the empty element with a list comprehension (or just a regular for loop)

>>> [x for x in myfile.splitlines() if x != ""]
['1000', '2000', '3000', '4000']

1 Comment

use s.splitlines(), not s.split("\n")
1
   with open('file.txt', 'rb') as f:
       data = f.read()
   lines = [s.strip() for s in data.split('\n') if s]

Comments

1

You can achieve this using the splitlines() method in python

data = """1000
2000
3000
4000"""

number_list = data.splitlines()

Comments

0

You might need to strip newlines.

# list of strings
[number for number in open("file.txt")]

# list of integers
[int(number) for number in open("file.txt")]

3 Comments

And, if the OP were to need to strip newlines, how would that code look?
You can also just use the list builtin instead of a list comprehension - list(open("myfile.txt")) -> ['1000\n', '2000\n', '3000\n', '4000\n']
@SilentGhost True, it makes it clearer what is happening, but list(open("myfile")) is the same as open("myfile").readlines() - by default iterating over a file with use the readlines method
0

If you already have a list that has been converted to a string by the str() method--like str(my_list)-- I have found that the easiest way to convert it back into a list is just to use eval(file_contents).

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.