I'm trying to iterate over a list of rows in a table and do modify a string in one of the columns:
# python 2.7
import csv
import re
with open('root_diff.txt', 'rU') as dmr:
coordinates_tsv = csv.reader(dmr, delimiter='\t')
coordinates_list = [row for row in coordinates_tsv]
for row in coordinates_list:
cut = re.split(':|-|r', row[3])
print cut[1]
But I get the following error:
IndexError: list index out of range
The string in row[3] looks something like this: chr1:594572-598657.
I want to split it so it looks like this: ['ch', '1', '594572', '598657'], and do something with the second and third numbers.
coordinates_list; just loop directly overcoordinates_tsv:for row in coordinates_tsv.row[3]doesn't match your pattern, you won't get an error - I suspect this is your actual problem.