Python: Split string separated by pipe character "|"

Look at the following line:

E|1256280||2014-01-05 17:54:00|1|2014-01-05 18:59:53|True

I would like to smash it by. pipe symbol "|". Therefore, I use the following python code (where the string is the string containing the string described above):

                print line
                print str(type(line))
                print str(line[1])
                parts = line.split['|']
                print str(parts)

However, when using this part of the 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 do not understand what I am doing wrong here. Any suggestions?

+4
source share
2 answers

parts = line.split['|']

it should be

parts = line.split('|')

(i.e. with parentheses instead of square brackets.)

+9
source

To call a method, use ()around arguments:

parts = line.split('|')

not [], which is the syntax for indexing a sequence.

csv module, | :

import csv

with open(filename, 'rb') as infh:
    reader = csv.reader(infh, delimiter='|')
    for row in reader:
        print row

.

+2

All Articles