Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Reading a data text file with different delimiters

I have a file in which fields are separated by a ':', subfields are separated by a ';', and items within subfields are separated by a ','.

I would like to read it Python. After a bit of time, I could probably read it line by line, and then splitting everything, but I believe something already exist for that kind of thing ?

A line of the file :

   0 :   16,  250 :  1 :  0.053 :RIG : DIS :  1 :   48, 220;  2 :   42, 241;  2 :   43, 251;  3 :   25, 266;  1 :   36, 287;  2 

I actually tried :

Dat = np.genfromtxt(path, delimiter= ':', dtype = None, skip_header = 4,  skip_footer = 5, encoding = None)

For a reason I do not understand, it only gives me back the first column of the file. However, it works if i change to delimiter= ','.

That gives me 7 fields, that I can actually split myself.

Then: 1) How would you do to read that file ? 2) Using np.genfromtxt, why do I obtain only the first column using ':' as delimiter ?

like image 948
Liris Avatar asked Jun 17 '26 19:06

Liris


1 Answers

Solution using pandas:

data = pd.read_csv('data.txt',
                   sep=";|:|,",
                   header=None,
                   engine='python')

This will write every value in a new column. Hope this could be helpful.

like image 143
dl.meteo Avatar answered Jun 19 '26 08:06

dl.meteo