4

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!

Ometjabbe
  • 41
  • 4
  • Wouldn't a simple f.readline().replace(";", ",") before the processing do the job? Assuming that the data doesn't contain semicolons in any case. If not maybe provide a version of the code without your solution attempts since it distracts from what you are trying to do. – Denker Apr 22 '16 at 13:02
  • Thank you for your reaction! That would work, however, I can't ensure that all csv files it will receive are delimited by either a semicolon or a comma. Your suggestion would only work in one of the two scenarios. – Ometjabbe Apr 22 '16 at 13:05
  • It would work in both as long as there are no commas or semicolons in the actual data. What are the possible delimiters that the file might contain? – Denker Apr 22 '16 at 13:08
  • Ok, I'll edit the code in the original post so that it just has the basic functionality. It works with a comma delimited csv file but not with a semicolon delimited csv file. – Ometjabbe Apr 22 '16 at 13:24
  • [http://stackoverflow.com/questions/16312104/python-import-csv-file-delimiter-or](http://stackoverflow.com/questions/16312104/python-import-csv-file-delimiter-or) This should help you do what you want. The `csv.Sniffer()` might be the best option for your case. – digitalbyte Apr 22 '16 at 13:41

1 Answers1

3

The csv.reader function accepts a delimiter argument, see the documentation for more.

rdr = csv.reader(csvfile, delimiter=';')

To handle quoted fields, you need to look at values for the quoting argument.

In fact, you can also deduce the file attributes using the csv.Sniffer class. This is useful when you don't know the delimiter. Again, directly from the documentation:

with open('example.csv') as csvfile:
    dialect = csv.Sniffer().sniff(csvfile.read(1024))
    csvfile.seek(0)
    reader = csv.reader(csvfile, dialect)
    # ... process CSV file contents here ...
ChrisP
  • 5,812
  • 1
  • 33
  • 36