Python: Import a file and convert to a list

后端 未结 8 2016
离开以前
离开以前 2020-12-19 20:02

I need help with importing a file and converting each line into a list.

An example of the file would look like:

p wfgh 1111 11111 111111
287 48 0
656         


        
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 20:47

    If you want all of the values in a flat list, the code would look as follows:

    ls=[]
    for line in open( "input.txt", "r" ).readlines():
        for value in line.split( ' ' ):
            ls.append( value )
    

    If you just want the lines in a list then you can stop at readlines().

提交回复
热议问题