13

I'm trying to read in the following tab separated data into pandas:
test.txt:

col_a\tcol_b\tcol_c\tcol_d
4\t3\t2\t1  
4\t3\t2\t1 

I import test.txt as follows:

pd.read_csv('test.txt',sep='\t')

The resulting dataframe has 1 column. The \t is not recognized as tab.

If I replace \t with a 'keyboard tab' the file is parsed correctly. I also tried replacing '\t with \t and /t and didn't have any luck.

Thanks in advance for your help.
Omar

PS: Screenshot https://i.stack.imgur.com/AykQl.jpg

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Omar
  • 329
  • 1
  • 3
  • 10
  • 1
    Please provide a *reproducible example*. Using the exact data you gave, and the exact same code, I'm getting a data-frame with a shape `(2,4)`. Do you actually have *tabs* in your csv? Or is it delimited by literally the characters `"\t"`? – juanpa.arrivillaga Aug 01 '17 at 16:52
  • So you just pasted the snippet above into a text file called 'test.txt' and ran: **import pandas as pd pd.read_csv('test.txt',sep='\t')** Maybe the problem is with my text editor? – Omar Aug 01 '17 at 16:55
  • Does your text-editor actually *show the characters `'\t'`*? Because then you **don't have tabs**. – juanpa.arrivillaga Aug 01 '17 at 16:56
  • No, the text editor doesn't actually display the \t as a tab – Omar Aug 01 '17 at 17:01
  • 2
    Then they aren't tabs. If you want to put tabs in your file, you should use the tab button. `\t` is a Python (and many other languages) escape sequence. Your text editor is going to assume you mean the actual characters `"\"` followed by `"t"` – juanpa.arrivillaga Aug 01 '17 at 17:02
  • No, it doesn't. It is an [**escape sequence**](https://en.wikipedia.org/wiki/String_literal#Escape_sequences). This is for your convenience for writing source code, it's *Python that interprets it as a tab*. The same way in Python `[]` mean a list, but in your text-editor it is just square brackets... – juanpa.arrivillaga Aug 01 '17 at 17:07
  • Ah I see. The reason I used \t because I was trying to find and replace the old delimiter with a tab. Thanks! – Omar Aug 01 '17 at 17:07

1 Answers1

23

The \t in your file is an actual backslash followed by a t. It is not a tab. You're going to have to use some escape characters on your sep parameter.

pd.read_csv('test.txt', sep=r'\\t', engine='python')

   col_a  col_b  col_c  col_d
0      4      3      2      1
1      4      3      2      1

Or

pd.read_csv('test.txt', sep='\\\\t', engine='python')

   col_a  col_b  col_c  col_d
0      4      3      2      1
1      4      3      2      1

response to comment

The r is indicating that it is a raw string and special characters should be interpreted the raw character. That is why in one solution I indicated that the string was raw and only had two backslashes. In the other, I had to escape each backslash with another backslash, leaving four backslashes.

piRSquared
  • 285,575
  • 57
  • 475
  • 624