我无法使用“doLogicForColumn5”功能来影响我的程序。 CSV输出的第5列应填充各种值,但它们都是相同的数字(12.12)。
第14-27行具体 - 他们根本不工作!
任何帮助都非常感谢!
# -*- coding: utf-8 -*-
import csv
import string # for capwords
date = "7/5/2015"
monthlybudget = 100000
dailybudget = monthlybudget/30
#campaign variables
levels = [["1"], ["2"], ["3"], ["4"], ["5"], ["6"]]
language = [["english"], ["spanish"]]
variables = [["1"], ["2"]]
nouns = [["wordA1"], ["+wordA2"]]
adjectives1 = [["wordB1"], ["wordB2"]]
adjectives2 = [["wordC1"], ["wordC2"]]
def doLogicForColumn5(self): # budget
self.column5 = dailybudget/36
if self.language == ["spanish"]:
self.column5 = self.column5 * .6
if self.level == ["1"]:
self.column5 = self.column5*.8
else:
self.column5 = self.column5*.1
else: #if spanish
self.column5 = self.column5*.4
if self.level == ["1"]:
self.column5 = self.column5*.2
else:
self.column5 = self.column5*.3
class Row(object):
column1 = "column1"
column2 = "column2"
column3 = "column3"
column4 = "column4"
column5 = "budget"
def __init__(self, level, language, noun, adjective1, adjective2, variable):
self.level = level
self.level = str(self.level)
self.language = language
self.language = str(self.language)
self.noun = noun
self.noun = str(self.noun)
self.adjective1 = adjective1
self.adjective1 = str(self.adjective1)
self.adjective2 = adjective2
self.adjective2 = str(self.adjective2)
self.variable = variable
self.variable = str(self.variable)
def rowEntry(self, level, language, noun, adjective1, adjective2, variable):
doLogicForColumn5(self)
lol = [[self.column1], [self.column2], [self.column3], [self.column4], [self.column5]]
lol[0] = self.column1
lol[1] = self.column2
lol[2] = self.column3
lol[3] = self.column4
lol[4] = self.column5
file_writer.writerow([o for o in lol])
with open("test.csv", "wb") as test_file:
file_writer = csv.writer(test_file)
for a in range(0, len(levels)):
for e in range(0, len(language)):
for y in range (0, len(nouns)):
for x in range (0, len(adjectives1)):
for w in range (0, len(adjectives2)):
for n in range(0, len(variables)):
city = "n/a"
stateVersion = "n/a"
food = Row(levels[a], language[e], nouns[y], adjectives1[x], adjectives2[w], variables[n])
food.rowEntry(levels[a], language[e], nouns[y], adjectives1[x], adjectives2[w], variables[n])
答案 0 :(得分:0)
首先:
for a in range(0, len(levels)):
所以a
是一个数字。然后:
food = Row(levels[a],
所以levels[a]
是list
,例如["1"]
。然后,当您__init__()
Row
:
self.level = level
self.level = str(self.level)
所以现在self.level
是一个字符串。然后,在doLogicForColumn5()
:
if self.level == ["1"]
回想一下,self.level
之前被转换为字符串。因此self.level
永远不会等于list
["1"]
。但是,它可能等于"['1']"
的字符串,因此与它进行比较会更有意义。更好的是,将全局levels
变为像list
这样的单["1", "2", "3", "4", "5", "6"]
。最好的选择是levels
简单地'123456'
并重构您的程序以使用更简单的结构。
同样适用于self.language
以及在较大list
内已设置为单个元素list
的任何其他变量。
检查代码中是否有其他不必要复杂的实例,因为这种事情最终会让你感到困惑(另一个例子,doLogicForColumn5()
函数应该只是Row
中的一个方法,因为评论者是提示)。