看看下面的字符串:
E|1256280||2014-01-05 17:54:00|1|2014-01-05 18:59:53|True
我想把它分开。管道符号“|”。因此,我使用以下 python 代码(其中 line 是包含上述字符串的字符串):
print line
print str(type(line))
print str(line[1])
parts = line.split['|']
print str(parts)
但是,当使用这段代码时,出现以下错误:
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
但是,我不明白我在这里做错了什么。有什么建议吗?
最佳答案
parts = line.split['|']
应该是
parts = line.split('|')
(即用圆括号代替方括号。)
关于 python : Split string separated by a pipe symbol "|",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20937533/