1

The solution below is working but I wanted to know if the code can be improved or if there is a more effective method of achieving the same results. I need to insert a "prefix" in the beginning of my list and I am using an iterator to do so. The prefix is 'a' for line 1, 'b' for line 2 and 'c' for line 3 and then restart at 'a' for line 4 etc..

test file:

this,is,line,one
this,is,line,two
this,is,line,three
this,is,line,four
this,is,line,five
this,is,line,six
this,is,line,seven
this,is,line,eight
this,is,line,nine

Code:

l = ['a','b','c']
it = iter(l)

with open('C:\\Users\\user\\Documents\\test_my_it.csv', 'rU') as c:
    rows = csv.reader(c)
    for row in rows:
        try:
            i = it.next()
            newrow = [i] + row
        except StopIteration:
            it = iter(l)
            i = it.next()
            newrow = [i] + row
        print(newrow)

Results are:

['a', 'this', 'is', 'line', 'one']
['b', 'this', 'is', 'line', 'two']
['c', 'this', 'is', 'line', 'three']
['a', 'this', 'is', 'line', 'four']
['b', 'this', 'is', 'line', 'five']
['c', 'this', 'is', 'line', 'six']
['a', 'this', 'is', 'line', 'seven']
['b', 'this', 'is', 'line', 'eight']
['c', 'this', 'is', 'line', 'nine']
1
  • 1
    "The solution below is working but I wanted to know if the code can be improved or if there is a more effective method of achieving the same results." - This is a good sign that you might be better off posting on CodeReview, read through that how to ask page and see if it might be relevant there. Commented Aug 11, 2015 at 14:33

2 Answers 2

6

This could be much simpler with itertools.cycle, which will handle endlessly repeating l for you:

from itertools import cycle, izip

l = ['a','b','c']

with open('C:\\Users\\user\\Documents\\test_my_it.csv', 'rU') as c:
    rows = csv.reader(c)
    for prefix, row in izip(cycle(l), rows):
        newrow = [prefix] + row
Sign up to request clarification or add additional context in comments.

3 Comments

@PadraicCunningham the people have spoken - they prefer puppies to booze!
It was non-alcoholic, unlike the very alcoholic wine I had last night!
@PadraicCunningham clearly you've convinced the OP, at least ;o)
4

Just cycle the list l using itertools.cycle, zipping the cycle object and your rows with itertools.izip:

from itertools import cycle, izip

l = ['a','b','c']
it = iter(l)
import csv
with open('in.csv', 'rU') as c:
    rows = csv.reader(c)
    for a, row in izip(cycle(l), rows):
        print([a]+ row)

Output:

['a', 'this', 'is', 'line', 'one']
['b', 'this', 'is', 'line', 'two']
['c', 'this', 'is', 'line', 'three']
['a', 'this', 'is', 'line', 'four']
['b', 'this', 'is', 'line', 'five']
['c', 'this', 'is', 'line', 'six']
['a', 'this', 'is', 'line', 'seven']
['b', 'this', 'is', 'line', 'eight']
['c', 'this', 'is', 'line', 'nine']

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.