现在正在进行一项长期项目,我至少相信我必须利用数百种不同的类似功能。至少我已经至少四次问过这个问题,到目前为止还没有收到任何可行的信息。
这是我最后一次发布这篇文章,以防万一有人厌倦了看到它,但我在帖子中注意到的最多的是人们无法理解为什么我可能需要"数百"类似的功能。所以我会尽力解释我的目标。
我正在编写一个程序来读取服务的csv文件。目前有#6; 6服务'在我的程序中。理想情况下,程序会为这些服务制作发票,并将它们显示为:
Service 1
Service 2
Service 3
Service 4
Service 5
Service 6
然而......在我的程序中,在我提出解决方案之前,我会告诉你下一个,如果服务是空白或空的输出,将是这样的:
Service 1
Service 2
Service 4
Service 6
然而,我希望列表看起来像这样
Service 1
Service 2
Service 4
Service 6
现在,要使用Python ReportLabs完成此功能,普通函数将如下所示:
def print1():
c.setFont('Deja', 12, leading=None)
c.drawString(100, YdrawLocationSVC1, Service1)
c.drawString(100, YdrawLocationSVC2, Service2)
c.drawString(100, YdrawLocationSVC3, Service3)
c.drawString(100, YdrawLocationSVC4, Service4)
c.drawString(100, YdrawLocationSVC5, Service5)
c.drawString(100, YdrawLocationSVC6, Service6)
但是,如果包含该服务的变量为空,则print1()函数将像第一个示例一样留下空白行。 感谢您阅读所有这些内容,如果您还在这里并愿意提供帮助,我现在就会遇到真正的问题。
如果我们测试字符串/服务是空还是满,并创建一个布尔值来表示该字符串/服务,那么可能是空或满,3个服务有8种可能的组合,可以放置字符串
例如,如果字符串1和3为空,但字符串2不为,则我们测试的布尔值将为0& 1& 0。现在你可以看到3个服务,有8个独特的组合
000
001
010
011
100
101
110
111
从这个事实判断,我们可以告诉6或8或10个服务,可能导致多达1024个可能的十位数布尔值的组合,(即000000000000-1111111111,0为空,1为满)
现在我已经解释了存在这么多可能组合的问题,这取决于你需要多少服务,我的最后一个问题就是这个问题。
你们会怎样做这样的事情? 目前我的程序处理了6个服务,64个可能的组合写入64个单独的函数,在csv文件测试完所有字符串后,由字典迭代。
我将在此处包含我当前的解决方案,并且我想补充一点,我现在唯一可以看到这样做的方法是编写我已经完成的更长版本。但我真的不想这样做一两千次大声笑。无论如何,这是我的解决方案>
#BitRep= 000000
def print1():
c.setFont('Deja', 12, leading=None)
c.drawString(100, YdrawLocationSVC1, " ")
c.drawString(100, YdrawLocationSVC2, " ")
c.drawString(100, YdrawLocationSVC3, " ")
c.drawString(100, YdrawLocationSVC4, " ")
c.drawString(100, YdrawLocationSVC5, " ")
c.drawString(100, YdrawLocationSVC6, " ")
#BitRep=000001
def print2():
c.setFont('Deja', 12, leading=None)
c.drawString(100, YdrawLocationSVC1, "Service 6")
c.drawString(250, YdrawLocationSVC1, str(service6))
c.drawString(330, YdrawLocationSVC1, "13 %")
c.drawString(400, YdrawLocationSVC1, str(singleTAXamountSVC6))
c.drawString(470, YdrawLocationSVC1, str(fullTAXamountSVC6))
c.drawString(100, YdrawLocationSVC2, " ")
c.drawString(100, YdrawLocationSVC3, " ")
c.drawString(100, YdrawLocationSVC4, " ")
c.drawString(100, YdrawLocationSVC5, " ")
c.drawString(100, YdrawLocationSVC6, " ")
ETC.
ETC.
#BitRep=111111
def print64():
c.setFont('Deja', 12, leading=None)
c.drawString(100, YdrawLocationSVC1, "Service 1 ")
c.drawString(250, YdrawLocationSVC1, str(service1))
c.drawString(330, YdrawLocationSVC1, "13 %")
c.drawString(400, YdrawLocationSVC1, str(singleTAXamountSVC1))
c.drawString(470, YdrawLocationSVC1, str(fullTAXamountSVC1))
c.drawString(100, YdrawLocationSVC2, "Service 2 ")
c.drawString(250, YdrawLocationSVC2, str(service2))
c.drawString(330, YdrawLocationSVC2, "13 %")
c.drawString(400, YdrawLocationSVC2, str(singleTAXamountSVC2))
c.drawString(470, YdrawLocationSVC2, str(fullTAXamountSVC2))
c.drawString(100, YdrawLocationSVC3, "Service 3 ")
c.drawString(250, YdrawLocationSVC3, str(service3))
c.drawString(330, YdrawLocationSVC3, "13 %")
c.drawString(400, YdrawLocationSVC3, str(singleTAXamountSVC3))
c.drawString(470, YdrawLocationSVC3, str(fullTAXamountSVC3))
c.drawString(100, YdrawLocationSVC4, "Service 4 ")
c.drawString(250, YdrawLocationSVC4, str(service4))
c.drawString(330, YdrawLocationSVC4, "13 %")
c.drawString(400, YdrawLocationSVC4, str(singleTAXamountSVC4))
c.drawString(470, YdrawLocationSVC4, str(fullTAXamountSVC4))
c.drawString(100, YdrawLocationSVC5, "Service 5 ")
c.drawString(250, YdrawLocationSVC5, str(service5))
c.drawString(330, YdrawLocationSVC5, "13 %")
c.drawString(400, YdrawLocationSVC5, str(singleTAXamountSVC5))
c.drawString(470, YdrawLocationSVC5, str(fullTAXamountSVC5))
c.drawString(100, YdrawLocationSVC6, "Service 6 ")
c.drawString(250, YdrawLocationSVC6, str(service6))
c.drawString(330, YdrawLocationSVC6, "13 %")
c.drawString(400, YdrawLocationSVC6, str(singleTAXamountSVC6))
c.drawString(470, YdrawLocationSVC6, str(fullTAXamountSVC6))
我很想看到你们的一些解决方案,你可以看到,我的绝对不是正确的方法。
我一直在尝试一个小脚本的想法,它将为我编写所有这些1024或许多功能。如果有人对如何使其发挥作用有任何想法,我想与大家分享。
这些只是我编写的脚本中的两个函数,一个创建case {} Dictionary,另一个创建所有的功能 PRINT1-print1024
def testMAKECASEDICTIONARYfromFIle():
# This function can be used to create a Case Dictionary in the format
案例#:功能。功能可以输入任何带衬里的文本文件。
print "cases = {"
# filepath = '/home/smiley/Desktop/sampletenbools'
filepath = '/home/smiley/Desktop/10boolsALL'
with open(filepath) as fp:
num = 1
for cnt, line in enumerate(fp):
var = line
# print var
print("{} : {}".format(str(line).strip("\n"), "Print"+ str(num))+",")
num+=1
print "}"
上面的函数创建了案例1-1024,基于我使用wordlist操纵器生成的文本文件。
我将要添加的下一个也是最后一个函数,负责逐个编写函数,尽管它还不能很好地工作。我再次赞赏反馈/建议。
def testFIle(): #此函数将用于枚举您用于创建切换字典的同一文件, #但会为每一行定义一个函数。
# filepath = '/home/smiley/Desktop/sampletenbools'
filepath = '/home/smiley/Desktop/10boolsALL'
with open(filepath) as fp:
num = 1
for cnt, line in enumerate(fp):
var = line
b1 = str(var)[0]
b2 = str(var)[1]
b3 = str(var)[2]
b4 = str(var)[3]
b5 = str(var)[4]
b6 = str(var)[5]
b7 = str(var)[6]
b8 = str(var)[7]
b9 = str(var)[8]
b10 = str(var)[9]
# YdrawLocationSVC1=490
# YdrawLocationSVC2=475
# YdrawLocationSVC3=460
# YdrawLocationSVC4=445
# YdrawLocationSVC5=430
# YdrawLocationSVC6=415
# YdrawLocationSVC7=400
# YdrawLocationSVC8=375
# YdrawLocationSVC9=360
# YdrawLocationSVC10=345
if b1 == "0":
YdrawLocationSVC1=1111
YdrawLocationSVC2=490
if b1 == "1":
YdrawLocationSVC1 = 490
if b2 == "0":
YdrawLocationSVC2=1111
YdrawLocationSVC3=475
if b2 == "1":
YdrawLocationSVC2 = 475
if b3 == "0":
YdrawLocationSVC3=1111
YdrawLocationSVC4=1111
if b3 == "1":
YdrawLocationSVC3 = 460
if b4 == "0":
YdrawLocationSVC4=1111
YdrawLocationSVC5=1111
if b4 == "1":
YdrawLocationSVC4 = 445
if b5 == "0":
YdrawLocationSVC5=1111
YdrawLocationSVC6=1111
if b5 == "1":
YdrawLocationSVC5 = 430
if b6 == "0":
YdrawLocationSVC6=1111
YdrawLocationSVC7=1111
if b6 == "1":
YdrawLocationSVC6 = 415
if b7 == "0":
YdrawLocationSVC7=1111
YdrawLocationSVC8=1111
if b7 == "1":
YdrawLocationSVC7 = 400
if b8 == "0":
YdrawLocationSVC8=1111
YdrawLocationSVC9=1111
if b8 == "1":
YdrawLocationSVC8 = 385
if b9 == "0":
YdrawLocationSVC9=1111
YdrawLocationSVC10=1111
if b9 == "1":
YdrawLocationSVC9 = 370
if b10 == "0":
YdrawLocationSVC10=1111
if b10 == "1":
YdrawLocationSVC10 = 355
print "# Bitrep ="+str(var).strip("\n")
print "def Print"+str(num)+"():"
print"\tc.setFont('Deja', 12, leading=None)"
print "\t# SERVICE NAME"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC1)+", stringn1)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC2)+", stringn2)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC3)+", stringn3)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC4)+", stringn4)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC5)+", stringn5)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC6)+", stringn6)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC7)+", stringn7)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC8)+", stringn8)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC9)+", stringn9)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC10)+", stringn10)"
# print("{} : {}".format(str(line).strip("\n"), "Print" + str(num)))
num += 1
# print "\n"
再次,非常感谢那些真正阅读过所有这些内容的人,对于那些有任何意见或建议的人来说,更是如此。
为了更清楚地按照下面的要求,我的目的是完全删除等式中的空白行。根据你的想法,我仍然希望如何做到这一点。
用户@bendl要求我提供额外的一些信息,对于这4个例子,00 01 10 11,布尔字符串将像这样制作。
for n in line:
var = n
if str(var)==""
bool1=0
and draw string 2 at location 1
if str(var)=!""
bool1=1
draw string 1 at location 1
但是,这些布尔集继续的时间越长,if语句得到的越高级。我已经尝试过为六种服务制作其中一种,并且它已经变得复杂,字典更易于管理。
答案 0 :(得分:1)
我认为你需要这样的打印功能:(我会在需要的代码中发表评论)
def ultimateprint(bitlist, numberOfBools):
if len(bitlist)!=numberOfBools: #check if bitlist is long enough
break
index = 1 #will count the n-th bit that is read
index2 = 1 # will count the n-th service that was requested
for bit in bitlist:
if bit: #bit is 1, service n was requested
printWithLineNeeded("Service " + str(index2), index) # I used this printWithLineNeeded to simplify
#the code you used to get the combined string where you need it.
index2 +=1 #count up the number of services that were requested
else: #bit is not one, empty line will be printed
printEmptyLineInYourWay(index) #index gives which line is empty in your code
index += 1 #end of iteration, one more bit was proceeded
bitlist必须类似于[1, 0, 1, 0, 0, 1]
,一个int列表。第一个服务是第一个服务,第二个是服务,第二个是服务,第三个是,依此类推。这样做的好处是python将它们解释为int或bool,具体取决于你如何形成代码。
实际上不需要numberOfBools,但为您提供安全性,使得位列表的长度与您拥有的服务数量相同。
for循环是动态的,我想知道在你之前的问题中没有人有这个想法。
我真的希望我能帮助你。如果需要,请随时询问澄清!
答案 1 :(得分:1)
我非常同意monamona的答案,但是这是另一个,使用字典,如OP建议的那样。
# service1 = 'a'
# service2 = 'b'
# service3 = 'c'
# service4 = 'd'
# service5 = 'e'
# service6 = 'f'
# service7 = 'g'
# service8 = 'h'
serviceList = [service1, service2, service3, service4, service5, service6, service7, service8]
locationList = [l1, l2, l3, l4, l5, l6, l7, l8]
bitlist = [1, 0, 1, 0, 0, 1, 1, 0]
serviceDict = {(not bit, enumeration[0]) : enumeration[1] for bit, enumeration in zip(bitlist, enumerate(serviceList))}
for key, location in zip(sorted(serviceDict.keys()), locationList):
if not key[0]:
print(serviceDict[key])
print(location)
else:
print()
使用此解决方案,只需替换print
语句,但最初打印解决方案。如果您想在没有定义服务方法的情况下运行,那么顶部的注释行就在那里。
这是有效的,因为False
可以解释为0
而True
可以解释为1
,所以我们首先用not
翻转进来的位使用生成的布尔值和服务号创建一个元组,这样当它被排序时,它将首先按与服务相关的位排序,然后排序服务的数量。
这看起来和听起来都非常复杂,我绝对认为monamona的解决方案更具可读性,但如果你单步执行它并且应该使用字典来解决这个问题,那真的很简单。