Converting a string into a list in Python

后端 未结 5 872
孤独总比滥情好
孤独总比滥情好 2021-01-12 01:06

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

5条回答
  •  感动是毒
    2021-01-12 01:13

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

提交回复
热议问题