convert .txt file to two-dimensional array with python

0

I have a file .txt with a lot of information, I want to read it with python but at the same time I read it I need to fill a two-dimensional array taking the word or the data separated by the comma as a position and not each character as a position . Each data separated by a comma corresponds to a different data type. The information in the file is sorted like this:

"600900", "", "GROUP", 1, 0, 0, 4, "", 0:00, 0:00, 09JAN, 09JAN, 09JAN, 09JAN, false, 0,

In my txt file I have too much information separated by rows and each row contains 16 columns in total. What I need should look something like this:

[600900][ ][GROUP][1][0][0][4][ ][0:00][0:00][09JAN][09JAN][09JAN][09JAN][false][0]
[600800][ ][GROUP][1][0][0][4][ ][0:00][0:00][09JAN][09JAN][09JAN][09JAN][false][0]
[600700][ ][GROUP][1][0][0][4][ ][0:00][0:00][09JAN][09JAN][09JAN][09JAN][false][0]

My Python code

file=open('escrito.txt','r') 
data=file.readlines() 
file.close() 
print (data)

Just read and print the file information

    
asked by Rafael 27.03.2018 в 21:29
source

1 answer

1

For the version of Python 2 you can use the csv module for text files whose delimiter is , . You can read as follows

import csv

with open('datos.txt', 'rb') as csvfile:
    rows = csv.reader(csvfile, delimiter=',', quotechar='|')
        for row in rows:
            print ', '.join(row)

This example what you basically do is an impression of each row, for your case instead of print you can create the logic that builds what you need. You can have more reference by reviewing the official CSV module documentation for Python 2

    
answered by 27.03.2018 / 23:06
source