0

consider this piece of code:

csvreader = csv.reader(filter(lambda row: row[0] != '#', fcsv), delimiter= '\t')
csvrs =[[row[2], row[7]] for row in csvreader]

the element row[7] is a string separated by ;, what i want to do is not put the entire row[7] inside csvrs but only a splitted part seaprated by ; Say for example:

row[7] = '123;457;789;1011'

i want only the second position inside csvrs (in this case 457) and i want this every time filtering out every other piece. I'M trying with split with no result maintaining the list comprehension.

1
  • 1
    How are you using .split(), and what does it do wrong? Commented May 6, 2016 at 14:20

2 Answers 2

1

Just do the split inside the comprehension and get [1] for the second item

csvrs =[[row[2], row[7].split(';')[1]] for row in csvreader]
Sign up to request clarification or add additional context in comments.

Comments

1
def get_second_col(row, delimiter=';'):
    try:
        return row.split(delimeter)[1]
    except IndexError:
        return None

csvrs =[[row[2], get_second_col(row[7])] for row in csvreader]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.