开发者

How to read a header from a CSV file using python

开发者 https://www.devze.com 2023-03-07 03:09 出处:网络
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\"

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 scr开发者_JAVA技巧ipt is not reading the column header.


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"
0

精彩评论

暂无评论...
验证码 换一张
取 消