Python脚本无法获取单元格值

2024-09-28 22:30:04 发布

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

我正在尝试用Python完成一个看似简单的任务。这是我这几天来第三次这么做,我感到很尴尬。你知道吗

我希望打开一个csv文件。如果值不是0或1(它是“?”),我想遍历每一行和每一行中的每一列,用最后一列的值重写该值。最后一列永远不能是0或1。永远不会是“?”。与其说我关心打印语句,不如说我在替换“?”(或非0/1)最后一列中的值。你知道吗

我在这里附上了一个示例csv文件: http://www.sharecsv.com/s/7bef636c33054cae624928297146bae1/house.csv

如果您无法查看上面的链接,我已将数据集的示例粘贴到下面:

0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,?,1.00,1.00,1.00,0.00,1.00,1.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,?,1.00
?,1.00,1.00,?,1.00,1.00,0.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,0.00,0.00,0.00
0.00,1.00,1.00,0.00,?,1.00,0.00,0.00,0.00,0.00,1.00,0.00,1.00,0.00,0.00,1.00,0.00
1.00,1.00,1.00,0.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,?,1.00,1.00,1.00,1.00,0.00
0.00,1.00,1.00,0.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,0.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,?,1.00,1.00,1.00,0.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,?,1.00,1.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,?,?,0.00
0.00,1.00,0.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,?,?,1.00,1.00,0.00,0.00,1.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,?,1.00,1.00,?,?,1.00
0.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,?,?,0.00
1.00,1.00,1.00,0.00,0.00,1.00,1.00,1.00,?,1.00,1.00,?,0.00,0.00,1.00,?,0.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,?,?,0.00,?,1.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,?,0.00,?,1.00
1.00,0.00,1.00,0.00,0.00,1.00,0.00,1.00,?,1.00,1.00,1.00,?,0.00,0.00,1.00,0.00
1.00,?,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,0.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,?,1.00,1.00,0.00,0.00,1.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.00
1.00,1.00,1.00,0.00,0.00,?,1.00,1.00,0.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,?,?,1.00,1.00,0.00
1.00,?,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,?,0.00,0.00,1.00,1.00,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.00
1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,?,0.00
1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,1.00,0.00
1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.00
1.00,0.00,0.00,1.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,1.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,?,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,0.00,0.00,1.00,0.00,1.00,1.00,0.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,1.00
1.00,?,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,?,1.00,0.00,1.00,1.00
1.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.00,1.00,1.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,1.00
1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,1.00,0.00,1.00,1.00,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,?,0.00,0.00,0.00,0.00,?,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,?,0.00
1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,0.00
1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,0.00,?,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,?,0.00,1.00,0.00,0.00,0.00,1.00,?,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,0.00
1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,?,0.00,0.00,0.00,0.00,0.00,0.00,?,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,0.00,1.00,0.00
0.00,?,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,1.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,?,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,?,?,0.00
1.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,1.00
1.00,1.00,1.00,0.00,0.00,1.00,?,1.00,0.00,0.00,1.00,1.00,0.00,1.00,0.00,?,0.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,0.00,1.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,1.00,1.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,1.00,1.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,1.00,1.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,?,1.00
1.00,1.00,1.00,0.00,0.00,?,1.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,?,0.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,1.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,?,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,0.00,1.00,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,?,0.00,0.00,0.00,1.00,0.00
1.00,1.00,0.00,1.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.00
0.00,1.00,0.00,1.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,0.00,1.00,1.00
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,1.00
1.00,?,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,0.00
1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.00
1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,?,0.00
1.00,1.00,1.00,1.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,0.00,1.00,0.00,1.00,1.00

这是我目前的代码:

import csv

reader = csv.reader(open('house1.csv'), delimiter = ',')
counter = 0

for row in reader:
    # print("Opened Reader")
    currVal = row[:-1]
    counter = counter + 1
    # print("set values")
    for column in row:
        questioncount = 0
        # print("Looping columns")
        if (column != 0 or column != 1):
            questioncount = questioncount + 1
            # This is where I should overwrite the value
        print("Row " + str(counter) + " has " + str(questioncount) + " question marks ")

我不明白为什么我有这么多困难。目前,Pycharm的输出是,它说每一行和每一列都有一个问号,这是不正确的。你知道吗

Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 1 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 2 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 3 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 4 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 5 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 6 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 7 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 8 has 1 question marks 
Row 9 has 1 question marks 
Row 9 has 1 question marks 
Row 9 has 1 question marks 
Row 9 has 1 question marks 
Row 9 has 1 question marks 
Row 9 has 1 question marks 
Row 9 has 1 question marks 
Row 9 has 1 question marks 
Row 9 has 1 question marks 
...
Row 435 has 1 question marks

我希望给出一个像
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,?,1.00,1.00,1.00,0.00,1.00,1.00
这个python脚本将把这一行变成
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,1.00,1.00

如有任何建议,将不胜感激。你知道吗


Tags: 文件csvin示例forcountercolumnreader
3条回答

解决问题的最快方法:

if not (column == '1.00' or column == '0.00'):

您正在检查字符串值(来自csv文件)'1.00'或'0.00'是否等于整数值(在if语句中)1或0。你知道吗

此外,您应该同时对这两个检查应用“not”,否则您的逻辑将失败。你知道吗

您的代码有许多问题:

  1. 您正在读取.csv,但内容将被读取为字符串,而不是浮点数,因此您的比较if (column != 0)将始终为真。你知道吗
  2. 因此,您总是计算1个问号,然后继续为每列打印一次,因为print语句在列循环中,而不是行循环中。你知道吗
  3. 您正在将最后一个值赋给currVal,但从未使用它。你知道吗

因为您的代码是在寻找问号,所以也许您不应该检查列是否不是1或0,而是检查它是否是“?”相反呢?你知道吗

您的代码和这些修复程序:

import csv

reader = csv.reader(open('house1.csv'), delimiter=',')
counter = 0

for row in reader:
    counter = counter + 1
    questioncount = 0
    for column in row:
        if column == '?':
            questioncount += 1
    print("Row " + str(counter) + " has " + str(questioncount) + " question marks ")

要编写您要查找的输出,请执行以下操作:

import csv

reader = csv.reader(open('simple.csv'), delimiter=',')
writer = csv.writer(open('output.csv', 'w', newline=''), delimiter=',')

for row in reader:
    writer.writerow([column if column != '?' else row[-1:] for column in row])

你的代码有两个错误。第一个是检查column是否不等于整数01,但column包含字符串值。第二个,初始化questioncount变量和调用print函数应该在for循环外进行。以下是工作代码:

import csv


rows = []

with open('house.csv', 'r') as f:
    reader = csv.reader(f, delimiter=',')
    counter = 0

    for row in reader:
        counter = counter + 1
        questioncount = 0

        for i, column in enumerate(row):

            if column == '?':
                questioncount = questioncount + 1
                row[i] = row[-1]

        rows.append(row)
        print('Row {i} has {q} question marks'.format(i=counter, q=questioncount))


with open('house1.csv', 'w') as f:
    writer = csv.writer(f, delimiter=',')

    for row in rows:
        writer.writerow(row)

注:我已经更新了代码,所以现在它保存了另一个被替换为问号的文件。你知道吗

相关问题 更多 >