我正在尝试用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
如有任何建议,将不胜感激。你知道吗
解决问题的最快方法:
您正在检查字符串值(来自csv文件)'1.00'或'0.00'是否等于整数值(在if语句中)1或0。你知道吗
此外,您应该同时对这两个检查应用“not”,否则您的逻辑将失败。你知道吗
您的代码有许多问题:
if (column != 0)
将始终为真。你知道吗print
语句在列循环中,而不是行循环中。你知道吗currVal
,但从未使用它。你知道吗因为您的代码是在寻找问号,所以也许您不应该检查列是否不是1或0,而是检查它是否是“?”相反呢?你知道吗
您的代码和这些修复程序:
要编写您要查找的输出,请执行以下操作:
你的代码有两个错误。第一个是检查
column
是否不等于整数0
或1
,但column
包含字符串值。第二个,初始化questioncount
变量和调用print
函数应该在for循环外进行。以下是工作代码:注:我已经更新了代码,所以现在它保存了另一个被替换为问号的文件。你知道吗
相关问题 更多 >
编程相关推荐