New to Python but finding it very fun to work with! I'm having some trouble with this script I'm writing though.
What the script does: it reads a csv file, loops over all the rows within it and places the information on the second position [1] of each row in a list (a, b, c, d and e). Then it concatenates a string (that is used as a query in another application)
Structure of the csv file:
Leg1,a,TST
Leg2,b,TST
Leg3,c,TST
Leg4,d,TST
Leg5,e,TST
The problem: when I use a csv file that has the comma as delimiter it works fine. When I use a csv file with semicolon as delimiter it results in an error: IndexError: list index out of range. it appears as if the rows in the semicolon delimited file are not perceived as consisting of multiple list items
The code:
#!/usr/bin/python
import csv
f = open('servers.csv')
starttext = '(b.bl = "f") & ('
query = []
q = 0
csv_f = csv.reader(f)
for row in csv_f:
query.append('(b.c.n=\"' + row[1] + '\")')
q += 1
serverlist = ' | '.join(query)
print starttext + serverlist + ')'
f.close()
I hope someone can point me in the right direction?!
thanks in advance!