简体   繁体   English

PANDAS:成功执行to_excel,但没有输出文件

[英]PANDAS: Execute to_excel successfully,but no output file

When I use to_excel to generate excel file, no error occurred , but no output file available.当我使用 to_excel 生成 excel 文件时,没有发生错误,但没有可用的输出文件。 can't find anything,any file in my supposed location... And I don't know why..在我假定的位置找不到任何文件,任何文件......我不知道为什么......

import pandas as pd
import os
import xlrd
import openpyxl
pd.set_option('display.width',None)
DIR = 'E:\Process'
datapath =os.path.join(DIR, 'Data.xlsx')
formatpath = os.path.join(DIR, 'Format.xlsx')
df = pd.read_excel(datapath)
df1=pd.read_excel(formatpath)
for i in range(0, len(df)):
    target = (df.iloc[i,17])
    df2 = df1
    df2.iat[3,3] = target
    print(df2)
    filename = df.iloc[i,2]
    filename = str(filename) + ".xlsx"
    sourcepath = os.path.join(DIR, filename)
    writer = pd.ExcelWriter(sourcepath)
    df2.to_excel(writer)
    print(sourcepath)

Expanding on the comment:扩展评论:

Use writer.save() after calling to_excel .调用to_excel后使用writer.save()

Alternatively you can use the with statement as suggested in the docs: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.ExcelWriter.html或者,您可以按照文档中的建议使用with语句: https : //pandas.pydata.org/pandas-docs/stable/reference/api/pandas.ExcelWriter.html

When I'm trying to re-run this code on my local machine, I'm running into an error with setting DIR to 'E:\\Process' because of the escaping of "\\" for windows machines.当我尝试在我的本地机器上重新运行此代码时,我遇到了将 DIR 设置为'E:\\Process'的错误,因为 Windows 机器转义了“\\”。 Have you tried "E:\\\\Process" ?你试过"E:\\\\Process"吗? or try it as a raw string literal r'E:\\Process' ?或尝试将其作为原始字符串文字r'E:\\Process'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM