How to delete more than one place when reading a text file

Problem: I cannot parse the information in a text file, because python reads it as a complete line, and not separate separate lines. The gaps between each variable are not \ t, so it is not split. Is there any way for python to flexibly remove spaces and put a comma or \ t instead?

DATA example:

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

The code I'm using is:

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]

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

Output Required:

['MOR125-1', 'MOR129-1', '0.587']
['MOR125-1', 'MOR129-3', '0.598']
['MOR129-1', 'MOR129-3', '0.115']
+4
source share
2 answers

csv . , ' +', .

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']
+7

csv.reader :

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

Python:

Dialect.delimiter , . ",". Dialect.skipinitialspace True, . False.

+2

All Articles