0

i tried the following script as per ur suggestion and i get an error import csv

def main():
    col_name = "\\xyz-abc\Procz(abcd)\Digital"
    with open('C:\\read.csv', 'rb') as inf:
        reader = csv.reader(inf)
        col_index = next(reader).index(col_name)
        highest = max(rec[col_index] for rec in reader)
        print "test"
main()     

I get an error ValueError: '\\\\xyz-abc\Procz(abcd)\Digital' is not in list please help me.. I think the script is not reading the column header.

3
  • 1
    Can you show us your csv file Commented May 18, 2011 at 7:21
  • It's very possible that this code is working fine, but the string you provided for col_name isn't actually in the header. We need to see the first few lines of your csv file to determine if this is the case. I would also suggest trying to print next(reader) to see what it's giving you. Commented May 18, 2011 at 7:25
  • actually i need the script to read from the column header \\xyz-abc\Procz(abcd)\Digital and this is just an example. should I use next(reader) instead of col_name ???? Commented May 18, 2011 at 7:37

1 Answer 1

2

Give this a go. Prevent the back-slashes being interpreted as escape characters by using a raw string

col_name = r"\\xyz-abc\Procz(abcd)\Digital"
Sign up to request clarification or add additional context in comments.

18 Comments

Thats how it is given in the CSV. The script should identify that column and get the max value from that column.
@Siddharth I understand. Try your script with the lower-case r in front of the col_name string. This makes it a raw string.
@Siddarth. ok. It would definitely help to see the first few lines of your csv file. Could you post them? Perhaps use gist.github.com
I think i figured out the issue... my header name starts with \\DHL-ABC001\ABC\MBB written. Since the header name starts with \\DHL the script is not able to read the header. Is there any solution for this ???
@Siddarth: I'm not sure what you mean. \\DHL should not stop your script from running. It's just a string. I can run your code on a csv file with header row \\DHL-ABC001\ABC\MBB,\\xyz-abc\Procz(abcd)\Digital,col3 and it works fine. Please post the first few lines of your csv
|

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.