python:由管道符号“|"分隔的拆分字符串; [英] python : Split string separated by a pipe symbol "|"
本文介绍了python:由管道符号“|"分隔的拆分字符串;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看看下面的字符串:
E|1256280||2014-01-05 17:54:00|1|2014-01-05 18:59:53|True
我想把它分开.管道符号|".因此,我使用以下 python 代码(其中 line 是包含上述字符串的字符串):
I'd like to split it wrt. the pipe symbol "|". Therefore I use the following python code (where line is a string containing the described string above):
print line
print str(type(line))
print str(line[1])
parts = line.split['|']
print str(parts)
但是,当使用这段代码时,我收到以下错误:
However, when using this piece of code i get the following error:
E|1256280||2014-01-05 17:54:00|1|2014-01-05 18:59:53|True
<type 'str'>
|
Traceback (most recent call last):
File "/path/to/my/pythonscritp.py", line 34, in crawl_live_quotes
parts = line.split['|']
TypeError: 'builtin_function_or_method' object is not subscriptable
但是,我不明白我在这里做错了什么.有什么建议吗?
However, I don't understand what I am doing wrong here. Any suggestions?
推荐答案
The
parts = line.split['|']
应该
parts = line.split('|')
(即用括号代替方括号.)
(i.e. with parentheses instead of square brackets.)
这篇关于python:由管道符号“|"分隔的拆分字符串;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文