我如何在python

时间:2015-11-29 01:33:20

标签: python csv variables

例如csv中的变量是:

 a│b │
 5│12│

我想要将csv中a的值解析为变量并打印值+ 5

1 个答案:

答案 0 :(得分:1)

如果a始终位于第一列,您可以按行索引0引用它:

import csv
# assuming file is named 'csvfile.csv' in the current directory
with open('csvfile.csv', newline='') as csvfile:
    # read the csv file and specify the delimiter 
    csvreader = csv.reader(csvfile, delimiter="|")
    #skip the header row
    next(csvreader)
    # for every data row in the file, add 5 to the number in the 
    # first column and print the result 
    for row in csvreader:
        value_of_a = row[0]
        print(float(value_of_a) + 5)