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

后端 未结 6 721
野趣味
野趣味 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 02:56

    Thanks for @PePr excellent solution. In addition, you can try to print the .txt file with the built-in method String.join(data). For example:

    with open(filename) as f:
        data = f.readlines()
    print(''.join(data))
    
    0 讨论(0)
  • 2021-02-06 03:00

    Try storing it in an array

    f = open( "file.txt", "r" )
    a = []
    for line in f:
        a.append(line)
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-02-06 03:19
    with open('test.txt') as o:
        for i,t in enumerate(o.readlines(), 1):
            print ("%s. %s"% (i, t))
    
    0 讨论(0)
  • 2021-02-06 03:20

    I know it is already answered :) To summarize the above:

    # It is a good idea to store the filename into a variable.
    # The variable can later become a function argument when the
    # code is converted to a function body.
    filename = 'data.txt'
    
    # Using the newer with construct to close the file automatically.
    with open(filename) as f:
        data = f.readlines()
    
    # Or using the older approach and closing the filea explicitly.
    # Here the data is re-read again, do not use both ;)
    f = open(filename)
    data = f.readlines()
    f.close()
    
    
    # The data is of the list type.  The Python list type is actually
    # a dynamic array. The lines contain also the \n; hence the .rstrip()
    for n, line in enumerate(data, 1):
        print '{:2}.'.format(n), line.rstrip()
    
    print '-----------------'
    
    # You can later iterate through the list for other purpose, for
    # example to read them via the csv.reader.
    import csv
    
    reader = csv.reader(data)
    for row in reader:
        print row
    

    It prints on my console:

     1. 12,12,12
     2. 12,31,12
     3. 1,5,3
    -----------------
    ['12', '12', '12']
    ['12', '31', '12']
    ['1', '5', '3']
    
    0 讨论(0)
提交回复
热议问题