Search code examples
pythonfile-iostrip

Python - Ignore FIRST character (tab) every line when reading


This is a continuation of my former questions (check them if you are curious).
I can already see the light at the end of the tunnel, but I've got a last problem.

For some reason, every line starts with a TAB character.
How can I ignore that first character ("tab" (\t) in my case)?

filename = "terem.txt"

OraRend = collections.namedtuple('OraRend', 'Nap, OraKezdese, OraBefejezese, Azonosito, Terem, OraNeve, Emelet')


csv.list_dialects()
for line in csv.reader(open(filename, "rb"), delimiter='\t', lineterminator='\t\t', doublequote=False, skipinitialspace=True):
    print line  
    orar = OraRend._make(line) # Here comes the trouble!

The text file:
http://pastebin.com/UYg4P4J1
(Can't really paste it here with all the tabs.)

I have found lstrip, strip and other methods, all of them would eat all the chars, so the filling of the tuple would fail.


Solution

  • You could do line = line[1:] to just strip the first character. But if you do this, you should add an assertion that the first character is indeed a tab, to avoid mangling data without leading tab.

    There is an easier alternative that also handles several other cases and doesn't break things if the things to be removed aren't there. You can strip all leading and trailing whitespace with line = line.strip(). Alternatively, use .lstrip() to strip only leading whitespace, and add '\t' as argument to either method call if you want to leave other whitespace in place and just remove tabs.