0

I have a 3 column file, an example posted below

P1\tTitle\tName
P2\tTitle\Name

etc.

The file itself does not have any leading spaces, or spaces between the columns. But, when I use python's f.read().splitlines() and then print each line, I see each line printed with a leading and trailing space for each column/split. If I then try to strip each of these variables using strip(), the spaces don't go away (which makes me think they are not spaces, but another hidden char). I even tried regex to remove the space

re.sub(r'\W+', '', string)

But that did not work either. Am I missing something here?

Hrvoje
  • 13,566
  • 7
  • 90
  • 104
PS376
  • 539
  • 8
  • 13
  • `splitlines()` doesn't introduce whitespace. Post some code and someone might be able to tell what you're doing wrong. – khelwood Dec 03 '14 at 20:09
  • The tabs will expand, is that what you mean? As a test, print `repr(string)` to see the tabs as `\t`. – tdelaney Dec 03 '14 at 20:25

2 Answers2

2

Possibly you are expecting the string to be modified in place. You need to assign the result back to the string.

foo = foo.strip()

Here is an example of how you could split your data

>>> s = """P1\tTitle\tName
... P2\tTitle\tName"""
>>> for row in s.splitlines():
...     print row.split("\t")
... 
['P1', 'Title', 'Name']
['P2', 'Title', 'Name']

The csv module can be used to process tvs files too, so maybe it's more suitable for your file.

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • Yes, I've tried that as well, but it still seems to be adding the extra space like characters. – PS376 Dec 03 '14 at 21:30
0

The \r character is the carriage return, and the carriage return-newline (\n) pair is both needed for newline in a network virtual terminal session. See here.

So you can add to the end of your strings in your code something like this:

.strip().replace('\n', ' ').replace('\r', '')

so above solution would look like this

for row in s.splitlines():
    print row.split("\t").strip().replace('\n', ' ').replace('\r', '')
Hrvoje
  • 13,566
  • 7
  • 90
  • 104