8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

每次我用 Python 写入内容时,Excel 文件都会损坏

Ferruccio 2月前

28 0

我正在尝试使用 pandas 更新我的 excel 文件,但每次文件都会损坏,我无法再打开它 1234.xlsx -> Sheet1 gammagammasatE50refEoedRefEurRefcΦEAf.s 17.6718.6...

我正在尝试使用 pandas ,但每次文件都会损坏,我无法再打开它

1234.xlsx->工作表1

伽马 伽玛卫星 E50参考 参考文献 欧洲参考文献 F EA 文件系统
17.67 18.6 10500 10500 36000 4 24 4800
17.67 18.6 10500 10500 36000 4 三十六 7200

所以我想用 python 代码填充 fs 列,并在计算后立即写入每行的答案

主程序

import pandas as pd
import os
file_name = "1234.xlsx"
file_path =os.path.join(os.getcwd(),file_name)

df = pd.read_excel(file_path)

for index, row in df.iterrows():
    gammaUnsat = row['gammaUnsat']
    gammaSat = row['gammasat']
    E50ref = row['E50ref']
    EoedRef = row['EoedRef']
    EurRef = row['EurRef']
    c = row['c']
    phi = row['Φ']
    EA = row['EA']

    # some calculation

    #code to write in file
    df.loc[index , "f.s"] = sf_i

    writer = pd.ExcelWriter(path=file_path,engine="xlsxwriter")
    df.to_excel(writer,"Sheet1",index=False)

我没有遇到任何错误,程序运行正常,但当我点击 Excel 文件时,它告诉我文件已损坏

enter image description here

帖子版权声明 1、本帖标题:每次我用 Python 写入内容时,Excel 文件都会损坏
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Ferruccio在本站《python》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 你必须关闭 writer,这样你的代码就会像这样

    import pandas as pd
    import os
    file_name = "1234.xlsx"
    file_path =os.path.join(os.getcwd(),file_name)
    
    df = pd.read_excel(file_path)
    
    for index, row in df.iterrows():
        gammaUnsat = row['gammaUnsat']
        gammaSat = row['gammasat']
        E50ref = row['E50ref']
        EoedRef = row['EoedRef']
        EurRef = row['EurRef']
        c = row['c']
        phi = row['Φ']
        EA = row['EA']
    
        # some calculation
    
        #code to write in file
        df.loc[index , "f.s"] = sf_i
    
        writer = pd.ExcelWriter(path=file_path,engine="xlsxwriter")
        df.to_excel(writer,"Sheet1",index=False)
        writer.close()
    

    或将 pandas 导入为 pdimport osfile_name = \'1234.xlsx\'file_path =os.path.join(os.getcwd(),file_name)

    df = pd.read_excel(file_path)
    
    for index, row in df.iterrows():
        gammaUnsat = row['gammaUnsat']
        gammaSat = row['gammasat']
        E50ref = row['E50ref']
        EoedRef = row['EoedRef']
        EurRef = row['EurRef']
        c = row['c']
        phi = row['Φ']
        EA = row['EA']
    
        # some calculation
    
        #code to write in file
        df.loc[index , "f.s"] = sf_i
    
    
        with pd.ExcelWriter(file_path,engine='xlsxwriter')  as writer:
            df.to_excel(excel_writer=writer,sheet_name="Sheet1",index=False)
    
  • 我认为您可以创建描述符并在循环中随时打开/关闭文件(ps 我没有测试代码)但可能有效

    import pandas as pd
    import os
    file_name = "1234.xlsx"
    file_path = os.path.join(os.getcwd(), file_name)
    
    df = pd.read_excel(file_path)
    
    for index, row in df.iterrows():
        gammaUnsat = row['gammaUnsat']
        gammaSat = row['gammasat']
        E50ref = row['E50ref']
        EoedRef = row['EoedRef']
        EurRef = row['EurRef']
        c = row['c']
        phi = row['Φ']
        EA = row['EA']
    
        # some calculation
    
        #code to write in file
        df.loc[index, "f.s"] = sf_i
    
    writer = pd.ExcelWriter(file_path, engine="xlsxwriter")
    df.to_excel(writer, "Sheet1", index=False)
    writer.save()
    
返回
作者最近主题: