简体   繁体   English

python:在迭代中处理 csv object 时出现问题

[英]python: issue with handling csv object in an iteration

First, sorry if the title is not clear.首先,如果标题不清楚,请见谅。 I (noob) am baffled by this...我(菜鸟)对此感到困惑......

Here's my code:这是我的代码:

import csv
from random import random
from collections import Counter

def rn(dic, p):
    for ptry in parties:
        if p < float(dic[ptry]):
            return ptry
        else:
            p -= float(dic[ptry])


def scotland(r):
    r['SNP'] = 48
    r['Con'] += 5
    r['Lab'] += 1
    r['LibDem'] += 5


def n_ireland(r):
    r['DUP'] = 9
    r['Alliance'] = 1
    # SF = 7


def election():
    results = Counter([rn(row, random()) for row in data])
    scotland(results)
    n_ireland(results)
    return results


parties = ['Con', 'Lab', 'LibDem', 'Green', 'BXP', 'Plaid', 'Other']

with open('/Users/andrew/Downloads/msp.csv', newline='') as f:
    data = csv.DictReader(f)
    for i in range(1000):
        print(election())

What happens is that in every iteration after the first one, the variable data seems to have vanished: the function election() creates a Counter object from a list obtained by processing data , but on every pass after the first one, this object is empty, so the function just returns the hard coded data from scotland() and n_ireland() .发生的情况是,在第一次迭代之后的每次迭代中,变量data似乎都消失了: functionelection election()从通过处理data获得的列表中创建一个 Counter object ,但在第一次之后的每一次迭代中,这个 ZA8CFDE6331BD59EB62AC914B 都是空的,所以 function 只返回来自scotland()n_ireland()的硬编码数据。 ( msp.csv is a csv file containing detailed polling data). msp.csv是一个包含详细轮询数据的 csv 文件)。 I'm sure I'm doing something stupid but would welcome anyone gently pointing out where...我确定我在做一些愚蠢的事情,但欢迎任何人轻轻指出哪里......

I'm going to place a bet on your definition of newline .我将对你对newline的定义打赌。 Are you sure you don't want newline = “\n”?你确定你不想要换行符=“\n”吗? Otherwise it will interpret the entire file as a single line, which explains what you're seeing.否则它将把整个文件解释为一行,这解释了你所看到的。

EDIT编辑

I now see another issue.我现在看到另一个问题。 The file object in python acts as a generator for each line. python 中的文件 object 充当每一行的生成器。 The problem is once the generator is finished (you hit the end of the file), you have no more data generated .问题是一旦生成器完成(你点击文件的末尾),你就没有更多的数据generated了。 To solve this: reset your file pointer to the beginning of the file like so:要解决这个问题:将文件指针重置为文件的开头,如下所示:

with open('/Users/andrew/Downloads/msp.csv') as f:
    data = csv.DictReader(f)
    for i in range(1000):
        print(election())
        f.seek(0)

Here the call to f.seek(0) will reset the file pointer to the beginning of your file.这里对f.seek(0)的调用会将文件指针重置为文件的开头。 You are correct that data is a global object given the way you've defined it at the module level, there's no need to pass it as a parameter.鉴于您在模块级别定义它的方式, data是全局 object 是正确的,无需将其作为参数传递。

I agree with @smassey, you might need to change the code to我同意@smassey,您可能需要将代码更改为

with open('/Users/andrew/Downloads/msp.csv', newline='\n') as f:

or simply try not use that argument或者干脆尝试不使用该论点

with open('/Users/andrew/Downloads/msp.csv') as f:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM