将结果追加到现有的excel中,而不是用Python编写新的csv文件

问题描述

我已使用以下代码df1写入excel文件Sheet1

writer = pd.ExcelWriter('data-{}.xlsx'.format(pd.datetime.today().strftime('%Y%m%d-%H%M%s'))) 
df1.to_excel(writer,'Sheet1',index = False)
#df2.to_excel(writer,'Sheet2',index = False) 
writer.save()

我还使用以下代码创建了一个新的数据框:

projects = df['name'].unique().tolist()

processedProjects = []
matchers = []

threshold_ratio = 60

for project in projects:
    if project:
        processedProject = fuzz._process_and_sort(project,True,True)
        processedProjects.append(processedProject)
        matchers.append(fuzz.SequenceMatcher(None,processedProject))

today_string = datetime.today().strftime('%Y%m%d-%H%M%s')

with open('data-' + today_string + '.csv','w') as f1:
    writer = csv.writer(f1,delimiter = ',',lineterminator = '\n',)
    writer.writerow(('name','name_','ratio'))

    for project1,project2 in itertools.combinations(enumerate(processedProjects),2):
        matcher = matchers[project1[0]]
        matcher.set_seq2(project2[1])
        ratio = int(round(100 * matcher.ratio()))
        if ratio >= threshold_ratio:
            #print(projects[project1[0]],projects[project2[0]])
            my_list = projects[project1[0]],projects[project2[0]],ratio
            print(my_list)
            writer.writerow(my_list)

如何将Sheet2附加到data-{}.xlsx中,而不是编写名为'data-' + today_string + '.csv'的csv文件

感谢您的提前帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)