Python在输出文件的末尾添加了一个双条目

Python在输出文件的末尾添加了一个双条目,python,Python,这是我创建hashtag文件的代码。问题是,它没有在第一个hashtag中添加第一个hashtag,并且在最后添加了一个双hashtag,如下所示 passiveincome, #onlinemarketing, #wahmlife, #cash, #entrepreneurlifestyle, #makemoneyonline, #makemoneyfast, #entrepreneurlifestyle, #mlm, #mlm 如何让代码删除双输出并将双输出放在开头 import rand

这是我创建hashtag文件的代码。问题是,它没有在第一个hashtag中添加第一个hashtag,并且在最后添加了一个双hashtag,如下所示

passiveincome, #onlinemarketing, #wahmlife, #cash, #entrepreneurlifestyle, #makemoneyonline, #makemoneyfast, #entrepreneurlifestyle, #mlm, #mlm
如何让代码删除双输出并将双输出放在开头

import random, os, sys
basepath = os.path.dirname(sys.argv[0]) + "/"

outputpath =  "C:/Users/matth/OneDrive/Desktop/Create hashtags/"
paragraphsmin = 9
paragraphsmax = 9
sentencemin = 1
sentencemax = 1

keywords = []
for line in open(basepath + "/base.txt", "r"):
    keywords.append(line.replace("\n",""))

keywordlist = []

keyword = open(basepath + "/text-original.txt", "r")

for line in keyword:
        keywordlist.append(line.replace("\n", "\n"))

def type(name):
    value = name[random.randint(0,len(name)-1)]
    return value

"""
def xyz(num):
    s1 = '' + type(keywordlist).strip()
    return eval('s' + str(num))
"""

def s1():
    return '' + type(keywordlist).strip()

def randomSentence():
    sent = eval("s" + str(random.randint(1,1)) + "()")
    return sent

for keyword in keywords:
    outputfile = open(outputpath + keyword.replace(" ", " ") + ".txt", "w")
    outputfile.write('')
    for p in range(1,random.randint(paragraphsmin,paragraphsmax) + 1):

        outputfile.write('')
        for s in range(1,random.randint(sentencemin,sentencemax) + 1):

            sentence = randomSentence()

            if str(sentence)[0] == "\"":
                outputfile.write("" + str(sentence)[0] + str(sentence)[1] + str(sentence)[2:] + " ")
            else:
                outputfile.write("" + str(sentence)[0] + str(sentence)[1:] + ", #")

        outputfile.write('')
    outputfile.write(sentence.replace("", "") + "")

    outputfile.close()
试着替换

outputfile.write("" + str(sentence)[0] + str(sentence)[1:] + ", #")


代码中是否存在格式问题?我看到很多东西都不起作用:keyword.replace、outputfile.write和句子.replace+,它们在开始时用于放置,但在结束时仍然有一个双条目。请尝试删除outputfile.writecontence.replace+。但是说真的,清理你的代码。它完全不可读。可读代码有助于防止这些类型的错误
outputfile.write("#" + str(sentence)[0] + str(sentence)[1:] + ", ")