【问题标题】:writing files python i think i have issue with else:count写文件 python 我认为我有问题 else:count
【发布时间】:2018-11-27 06:48:30
【问题描述】:

编写一个名为“internet_histogram”的函数,它不带参数也不返回值。在测试环境中将有一个名为“survey.csv”的文件,其中包含如上所述的调查结果(该文件有一个标题行,您的代码可以解决很多问题)。编写一个名为“histogram.csv”的新文件,其中包含代表“internet_use,frequency”的 2 列,并且没有标题行,其中将包含 28 到 29 岁的响应者结果的直方图,包括端点年龄。您的文件将恰好有 6 行 internet_use 值为 1-5 对应于 intfreq 结果和 6 用于回答 2 到 eminuse 的响应者。阅读调查结果文件并跟踪该年龄范围内有多少响应者回答了这 6 个选项中的每一个,并将这些计数按照 internet_use 从 1 开始的顺序写入您的“histogram.csv”文件。 示例 histogram.csv: 1,5 2,7 3,0 4,1 5,2 6,4

我的代码:

import csv
def internet_histogram():
    count_6 = 0
    count_5 = 0
    count_4 = 0
    count_3 = 0
    count_2 = 0
    count_1 = 0
    with open("survey.csv",'r') as f:
        reader = csv.reader(f)
        with open("histogram.csv", 'w') as g:
            writer = csv.writer(g)
            next(reader)
            for line in reader:
                if int(line[3]) >= 28 and int(line[3]) <= 29:
                    if line[2] != '':
                        if int(line[2]) == 1:
                            count_1 += 1
                        if int(line[2]) == 2:
                            count_2 += 1
                        if int(line[2]) == 3:
                            count_3 += 1
                        if int(line[2]) == 4:
                            count_4 += 1
                        if int(line[2]) == 5:
                            count_5 += 1
            else:
                count_6 = count_6 + 1
                arr = [[1, count_1], [2, count_2], [3, count_3], [4, count_4], [5, count_5], [6, count_6]]
                for i in arr:
                    writer.writerow(i)

输出: 写道:“1,26 2,29 3,2 4,3 5,1 6,1 " 预期:“1,26 2,29 3,2 4,3 5,1 6,2 "

我认为这是 else 语句的问题,但我不太确定,任何帮助将不胜感激。

【问题讨论】:

  • 看起来您的else 缺少缩进。所以它指的是for而不是if
  • 我早些时候尝试过,它只是变成了一个循环,有超过 1000 个值循环播放
  • 很难判断我没有看到的东西,但是当您尝试这样做时,您是否取消了第二个 for 循环的缩进?
  • 另外,您的else 只会引用最后一个if。也许使用elifs。
  • 是的,我为 else 语句尝试了多种不同的缩进方法

标签: python file-writing


【解决方案1】:

按照现在的编写方式,6 列的计数始终为 1
您应该缩进else 以便将这种情况与其他情况一起计算,但使用else 只会引用最后一个if,因此它将计算int(line[2]) != 5 的所有情况,这可能不是t 你想要做什么。
为了获得最佳的 python zen 明确性,我将使用以下内容而不是 else

if int(line[2]) not in [1, 2, 3, 4, 5]:
    count_6 += 1

这对你有用吗?
祝你好运!

【讨论】:

  • 写道:“”预期:“1,26 2,29 3,2 4,3 5,1 6,2”
  • 最后得到的是 1 而不是 2?那是因为它在循环之外,所以它除了 1 之外别无他法。
  • 是的,我知道你在说什么,我会试着弄乱 else 语句,看看我是否可以从那里开始。感谢您的帮助。
  • 如果有帮助,您可以通过投票/接受来表达您的感激之情 :-)
  • 美国东部标准时间 2018 年 11 月 27 日上午 10:21 遇到此问题,但仍未弄清楚如何解决此问题。
【解决方案2】:

我自己还不太精通 Python,但我通过在此处移动一些缩进以及其他一些小改动来修复您的程序:

import csv
def internet_histogram():
    count_6 = 0
    count_5 = 0
    count_4 = 0
    count_3 = 0
    count_2 = 0
    count_1 = 0
    with open("survey.csv",'r') as f:
        reader = csv.reader(f)
        with open("histogram.csv", 'w') as g:
            writer = csv.writer(g)
            next(reader)
            for line in reader:
                if int(line[3]) >= 28 and int(line[3]) <= 29:
                    if line[2] != '':
                        if int(line[2]) == 1:
                            count_1 += 1
                        elif int(line[2]) == 2:
                            count_2 += 1
                        elif int(line[2]) == 3:
                            count_3 += 1
                        elif int(line[2]) == 4:
                            count_4 += 1
                        elif int(line[2]) == 5:
                            count_5 += 1
                    else:
                        count_6 = count_6 + 1
                    arr = [[1, count_1], [2, count_2], [3, count_3], [4, count_4], [5, count_5], [6, count_6]]
            for i in arr:
                writer.writerow(i)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 2013-12-26
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多