Python-读取时忽略每行的第一个字符(制表符)
这是我之前问题的延续(如果您好奇的话请检查它们)。
我已经看到了隧道尽头的曙光,但我还有最后一个问题。
由于某种原因,每一行都以 TAB 字符开头。
我如何忽略第一个字符(在我的例子中为“制表符”(\t))?
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!
文本文件:
http://pastebin.com/UYg4P4J1
(无法真正将其与所有选项卡一起粘贴到此处。)
我发现了 lstrip、strip 和其他方法,它们都会吃掉所有 字符,因此元组的填充将会失败。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以执行
line = line[1:]
来删除第一个字符。但如果这样做,您应该添加一个断言,表明第一个字符确实是制表符,以避免在没有前导制表符的情况下损坏数据。有一个更简单的替代方案,它还可以处理其他几种情况,并且如果要删除的东西不存在,也不会破坏东西。您可以使用
line = line.strip()
去除所有前导和尾随空格。或者,使用.lstrip()
仅去除前导空格,并添加'\t'
作为任一方法调用的参数(如果您想保留其他空格)删除选项卡。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.要从字符串中删除第一个字符:
To remove the first character from a string:
来自文档:
如果您只想删除行首的制表符,请使用
做的好处是您不必检查以确保第一个字符实际上是制表符。但是,如果存在多个选项卡的情况,并且您希望保留第二个选项卡并打开,则必须使用
str[1:]
。From the docs:
If you want to only remove the tab at the beginning of a line, use
This has the benefit that you don't have to check to make sure the first character is, in fact, a tab. However, if there are cases when there are more than one tab, and you want to keep the second tab and on, you're going to have to use
str[1:]
.考虑一下这一点。您不需要将“文件”传递给 csv.reader。作为字符串值序列的文件行对象可以很好地工作。
Consider this. You don't need to pass a "file" to csv.reader. A file-line object that is a sequence of string values works nicely.