找到你要的答案

Q:Trying to use data from file in mathematical equations

Q:尝试使用来自数学公式的文件中的数据

I'm not sure what's wrong with this code. It's supposed to allow me to use data from fitnessfile.csv in mathematical equations. This is my code:

 import csv 
 csvfile = file.open("fitness file.csv")
 readcsv = csv.reader(csvfile, delimiter=",")

 genders = []

 for row in readcsv:
     print (row)
     gender = row[3]

    genders.append(gender)

If you have a better or easier way of using data from a file in mathematical equations then please tell me. When I run this code it says

NameError: name 'file' is not defined

file is in the second line of code

我不知道这个代码有什么问题。它应该允许我用数学方程中fitnessfile.csv数据。这是我的密码:

 import csv 
 csvfile = file.open("fitness file.csv")
 readcsv = csv.reader(csvfile, delimiter=",")

 genders = []

 for row in readcsv:
     print (row)
     gender = row[3]

    genders.append(gender)

如果你有一个更好或更简单的方式使用数据的文件在数学方程,那么请告诉我。当我运行这个代码,它说

NameError: name 'file' is not defined

文件位于第二行代码中

answer1: 回答1:

I have not tested, but consider:

import csv 
with open("fitness file.csv") as csvfile:
    readcsv = csv.reader(csvfile, delimiter=",")
    genders = []
    for row in readcsv:
        print (row)
        gender = row[3]
        # you will probably need to convert 'gender' to float 
        # with float(gender) to use in arithmetic, which I *think* is the question
        genders.append(gender)

我没有测试,但考虑:

import csv 
with open("fitness file.csv") as csvfile:
    readcsv = csv.reader(csvfile, delimiter=",")
    genders = []
    for row in readcsv:
        print (row)
        gender = row[3]
        # you will probably need to convert 'gender' to float 
        # with float(gender) to use in arithmetic, which I *think* is the question
        genders.append(gender)
python  file  csv  python-3.x