How to convert string values to integer values while reading a CSV file?

后端 未结 3 1919
你的背包
你的背包 2021-02-19 23:03

When opening a CSV file, the column of integers is being converted to a string value (\'1\', \'23\', etc.). What\'s the best way to loop through to convert these back to intege

3条回答
  •  [愿得一人]
    2021-02-19 23:27

    If the CSV has headers, I would suggest using csv.DictReader. With this you can do:

     with open('C:/Python27/testweight.csv', 'rb') as f:
        reader = csv.DictReader(f)
        for row in reader:
            integer = int(row['Name of Column'])
    

提交回复
热议问题