Python - read numbers from text file and put into list

前端 未结 6 867
日久生厌
日久生厌 2020-12-03 16:12

So like the title says im starting to learn some python and im having trouble picking up on this technique. What I need to accomplish is to read in some numbers and store t

6条回答
  •  庸人自扰
    2020-12-03 16:45

    with open('data.txt') as f:
        lis=[map(int,x.split()) for x in f if x.strip()]   # if x.strip() to skip blank lines
    
       #use list(map(int,x.split()))  in case of python 3.x
    

    this is how map() works:

    >>> map(int,'1 2 3 4'.split())
    [1, 2, 3, 4]
    

提交回复
热议问题