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

后端 未结 6 720
野趣味
野趣味 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:18

    You may also be interested in the csv module. It lets you parse, read and write to files in the comma-separated values( csv) format...which your example appears to be in.

    Example:

    import csv
    reader = csv.reader( open( 'file.txt', 'rb'), delimiter=',' )
    #Iterate over each row
    for idx,row in enumerate(reader):
        print "%s: %s"%(idx+1,row)
    

提交回复
热议问题