Is there a way to read a .txt file and store each line to memory?

后端 未结 6 742
野趣味
野趣味 2021-02-06 02:48

I am making a little program that will read and display text from a document. I have got a test file which looks like this:

12,12,12
12,31,12
1,5,3
...
         


        
6条回答
  •  野性不改
    2021-02-06 03:03

    #!/usr/local/bin/python
    
    t=1
    
    with open('sample.txt') as inf:
        for line in inf:
            num = line.strip() # contains current line
            if num:
                fn = '%d.txt' %t # gives the name to files t= 1.txt,2.txt,3.txt .....
                print('%d.txt Files splitted' %t)
                #fn = '%s.txt' %num
                with open(fn, 'w') as outf:
                    outf.write('%s\n' %num) # writes current line in opened fn file
                    t=t+1
    

提交回复
热议问题