Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove more than one space when reading text file

Problem: I cannot seem to parse the information in a text file because python reads it as a full string not individual separate strings. The spaces between each variable is not a \t which is why it does not separate. Is there a way for python to flexibly remove the spaces and put a comma or \t instead?

Example DATA:

MOR125-1   MOR129-1   0.587
MOR125-1   MOR129-3   0.598
MOR129-1   MOR129-3   0.115

The code I am using:

with open("Distance_Data_No_Bootstrap_RAW.txt","rb") as f:
reader = csv.reader(f,delimiter="\t")
d=list(reader)
for i in range(3):
    print d[i]

Output: ['MOR125-1 MOR129-1 0.587'] ['MOR125-1 MOR129-3 0.598'] ['MOR129-1 MOR129-3 0.115']

Desired Output:

['MOR125-1', 'MOR129-1', '0.587']
['MOR125-1', 'MOR129-3', '0.598']
['MOR129-1', 'MOR129-3', '0.115']
like image 733
UberKoolMan Avatar asked Dec 14 '22 05:12

UberKoolMan


2 Answers

You can simply declare the delimiter to be a space, and ask csv to skip initial spaces after a delimiter. That way, your separator is in fact the regular expression ' +', that is one or more spaces.

rd = csv.reader(fd, delimiter=' ', skipinitialspace=True)
for row in rd:
    print row
['MOR125-1', 'MOR129-1', '0.587']
['MOR125-1', 'MOR129-3', '0.598']
['MOR129-1', 'MOR129-3', '0.115']
like image 145
Serge Ballesta Avatar answered Jan 12 '23 22:01

Serge Ballesta


You can instruct csv.reader to use space as delimiter and skip all the extra space:

reader = csv.reader(f, delimiter=" ", skipinitialspace=True)

For detailed information about available parameters check Python docs:

Dialect.delimiter A one-character string used to separate fields. It defaults to ','. Dialect.skipinitialspace When True, whitespace immediately following the delimiter is ignored. The default is False.

like image 30
niemmi Avatar answered Jan 12 '23 22:01

niemmi