为什么我的python代码不打印我的列表?

2024-04-19 13:27:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个函数,它应该返回一个列号列表,这些列号对应于我稍后要从csv文件中提取的特定变量。变量名由state_aggregate_vars指定。你知道吗

import csv
import numpy

def compute_countyStats(state_aggregate, year, state_aggregate_vars, listCounties, county_var, int_obese, int_healthy, int_refuse): : 
    f = open(state_aggregate, 'r')
    readit = csv.reader(f)
    headers = readit.next()
    use_cols = []
    for name in state_aggregate_vars:
        use_cols.append(headers.index(name))
    return use_cols
    county_data = numpy.genfromtxt(f, dtype=float, delimiter=',', names = state_aggregate_vars, filling_values= -1, 
                usecols= use_cols, usemask=False)   
    sorted_array = county_data(numpy.argsort(county_data, axis= headers.index(county_var))
    for code in listCounties: 
    temp =  []
    for entry in sorted_array: 
        if entry[0] == code: 
            temp.append(float(entry[1]))
        else:
            continue
    percent_healthy = numpy.true_divide(temp.count(int_healthy),num_obs)
    percent_obese = numpy.true_divide(temp.count(int_obese),num_obs)
    percent_refused = numpy.true_divide(temp.count(int_refuse),num_obs)
    county_stats[code] = year, code, percent_healthy, percent_obese, percent_refused, 
return county_stats

接下来,我使用以下方法调用此函数:

state_aggregate = "Aggregate_test90.csv"
year = 1990
state_aggregate_vars = ['_BMI90', 'AGE90', 'CTYCODE90', 'IYEAR90', 'SEX90', '_RFOBESE90']
listCounties = [31, 43, 163, 32, 167, 97]
county_var = 'CTYCODE90'
int_obese = 2
int_healthy = 1
int_refuse = 0  

test = compute_countyStats(state_aggregate, year, state_aggregate_vars, listCounties, county_var, int_obese, int_healthy, int_refuse)

这是我的错误:

SyntaxError: invalid syntax in line 6: 
 for name in state_aggregate_vars:

会出什么问题?你知道吗

这是我的python -V

Python 2.7.3 -- EPD 7.3-2 (32-bit)

Tags: csvinnumpyvarvarsyeartempint
2条回答

您的代码中有一些错误:

第四行:一个冒号到很多

第14行:缺少大括号

第17行:缺少缩进

第27行:缺少缩进

看起来我安装的Python不知怎么被破坏了。我重新安装了它,这个功能现在运行得非常好。你知道吗

相关问题 更多 >