Search code examples
pythonnumpydata-files

Loading data file with too many commas in Python


I am trying to collect some data from a .txt file into my python script. The problem is that when the data was collected, it could not collect data in one of the columns, which has given me more commas than normally. It looks like this:

0,0,,-2235
1,100,,-2209
2,200,,-2209

All I want is to load the data and remove the commas but when I try with numpy.loadtxt it gives me a value error. What do I do?


Solution

  • You can use regular expression module to split

    In[1]: import re
    In[2]: re.split(',,|,', '0,0,,-2235 1,100,,-2209 2,200,,-2209')
    Out[2]: ['0', '0', '-2235 1', '100', '-2209 2', '200', '-2209']
    

    ',,|,' means it firstly splits at ,, and then in the result, it continues to split at ,.

    So if you want to get -2235 and 1 instead of -2235 1 you can use ',,|,| ' or ',,|,|\s' to ease the eyes (\s means space).