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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With