我正在尝试编写一个代码,将十个绿色瓶子的歌曲打印成一个文本文件,找到并打开一个名为"十个绿色瓶子"的记事本文件。虽然我在打开和打印到文本文件中遇到了重大问题但是需要帮助才能实现这一点。如果你能帮助解决这个问题,我将非常感激:
StringList = ['Ten ','Nine ','Eight ','Seven ','Six ','Five ','Four ','Three ','Two ','One ']
StringList2 = ['ten','nine ','eight ','seven ','six ','five ','four ','three ','two ', 'one ','no ']
string1 = ("green bottle \nHanging on the wall\n")
string2 = ("green bottle\nHanging on the wall\nAnd if one green bottle")
string3 = ("\nShould accidently fall\nThere'll be ")
string4 = ("green bottles \nHanging on the wall \n")
string5 = ("green bottles\nHanging on the wall\nAnd if one green bottle")
string6 = ("green bottle\nHanging on the wall\n")
def loopingverse():
verse1 =''
for x in range (0 , 10):
if x > 8: verse1 = verse1 + (StringList[x] + string1 + StringList[x] + string2+string3 + StringList2[x + 1] + string4 + "\n")
if x == 8: verse1 = verse1 + (StringList[x] + string4 + StringList[x] + string5 + string3 + StringList2[x + 1] + string6 + "\n")
if x < 8: verse1 = verse1 + (StringList[x] + string4 + StringList[x] + string5+string3 + StringList2[x + 1] + string4 + "\n")
返回第1节
import subprocess
subprocess.call(['notepad.exe', 'ten green bottles.txt'])
if __name__ == '__loopingverse__':
loopingverse()
答案 0 :(得分:0)
This little tutorial为如何实现您的目标提供了很好的帮助。
您的最终产品应如下所示:
... String lists here
... Strings here
with open("myfile.txt","w") as f:
for i in MyStrings:
f.write(i)
答案 1 :(得分:-1)
好的,这是实际可行的东西
StringList = ['Ten ', 'Nine ', 'Eight ', 'Seven ', 'Six ', 'Five ', 'Four ', 'Three ', 'Two ', 'One ']
StringList2 = ['ten', 'nine ', 'eight ', 'seven ', 'six ', 'five ', 'four ', 'three ', 'two ', 'one ', 'no ']
string1 = ("green bottle \nHanging on the wall\n")
string2 = ("green bottle\nHanging on the wall\nAnd if one green bottle")
string3 = ("\nShould accidently fall\nThere'll be ")
string4 = ("green bottles \nHanging on the wall \n")
string5 = ("green bottles\nHanging on the wall\nAnd if one green bottle")
string6 = ("green bottle\nHanging on the wall\n")
def loopingverse():
verse1 = ''
for x in range (0 , 10):
if x > 8:
verse1 = verse1 + (StringList[x] + string1 + StringList[x] + string2 + string3 + StringList2[x + 1] + string4 + "\n")
if x == 8:
verse1 = verse1 + (StringList[x] + string4 + StringList[x] + string5 + string3 + StringList2[x + 1] + string6 + "\n")
if x < 8:
verse1 = verse1 + (StringList[x] + string4 + StringList[x] + string5 + string3 + StringList2[x + 1] + string4 + "\n")
with open('file.txt', 'w') as wr:
#write the verse to the file
wr.write(verse1)
print("Done")
#launch the application
if __name__ == '__main__':
loopingverse()
祝你好运。