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']