0

I have a huge text file, each line has a tab-delimited string. I need to keep all tabs apart from those at the end of each line. I need to keep the carriage return. Any ideas?

I've tried everything on these answers:

as well as others I've now closed the tabs on.

Community
  • 1
  • 1
Tortex
  • 163
  • 1
  • 1
  • 12
  • 1
    So where's your code, and what exactly is the problem with it? We don't do *"urgent"* here; this isn't a helpdesk. Also, are you sure removing the tabs isn't going to cause you problems when you later parse the file again? – jonrsharpe Nov 29 '14 at 20:11
  • 1
    can u provide input data and expected output – Hackaholic Nov 29 '14 at 20:13
  • If all of your lines contain `\t` at the last apart from new line, then why don't you just use splicing. E.g. `line[:-2]+line[-1]` – Bhargav Rao Nov 29 '14 at 21:08

1 Answers1

1

Just use a regular expression

>>> import re
>>> s="1\t2\t3\t\t\n"
>>> s2=re.sub('\t+\n','\n',s)
>>> s2
'1\t2\t3\n'
kdopen
  • 8,032
  • 7
  • 44
  • 52