python : Split string separated by a pipe symbol "|"
pipe, python, split
Solution
The
parts = line.split['|']
should be
parts = line.split('|')
(i.e. with parentheses instead of square brackets.)
Problem
Have a look at the following string: ``` E|1256280||2014-01-05 17:54:00|1|2014-01-05 18:59:53|True ``` 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?