2
with open("data3.txt") as f:
    data = f.read()

data = data.split('\n')
x = [row.split('    ')[0] for row in data]
y = [row.split('    ')[1] for row in data]

I need to read the data file (which is a text file, with columns separated by tab), but I am not able to read it using the above code, where I have copied the 'tab' spacing from the .txt file and pasted it in the code directly. What is the reason this is not happening?

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

Perhaps your text editor or IDE "helpfully" converted your tab character into spaces for you.

Consider using row.split('\t') instead.

Kevin
  • 74,910
  • 12
  • 133
  • 166
0

You need to split it on the tabs, not on the spaces, something like:

In [1]: x = "a\tb\tc"

In [2]: print x
a       b       c

In [3]: x.split('\t')
Out[3]: ['a', 'b', 'c']

The \t is the tab character.

AlG
  • 14,697
  • 4
  • 41
  • 54