ValueError:无法将0转换为Excel

问题描述 投票:0回答:1

我有一段代码可以将Downloaded csv中的数据复制/粘贴到预定义的Excel模板中。某些csv文件必须在粘贴之前进行转置。这在我的电脑上没有问题,但在Mac上它给出了一个无法将{0!r}转换为Excel“.format(value)错误。

这是我正在使用的代码:

    def read_transpose(account_id):


    excel_template = load_workbook('Cost_Optimization_Template.xlsx')

    ec2_utilization = excel_template['EC2 RI Utilization ']
    rds_utilization = excel_template['RDS RI Utilization ']
    elasticity = excel_template['Elasticty']
    ec2_newri = excel_template['EC2 New RI']
    coverage = excel_template['RI Coverage']
    accounts = excel_template['Accounts']   
    services = excel_template['Services']
    spot_by_account = excel_template['Spot Usage by Account']
    spot = excel_template['Spot Usage']
    storage = excel_template['Storage']
    storage_by_account = excel_template['Storage by Account']
    cloudwatch_accounts = excel_template['CloudWatch Accounts']
    instance_types = excel_template['Instance Types']
    ri_graph = excel_template['RI Graph']
    instructions = excel_template['Instructions']

    instructions['C5'] = account_id


    tab_list = [ec2_utilization, rds_utilization, elasticity, ec2_newri, coverage, accounts, services, spot_by_account, spot, storage, storage_by_account, cloudwatch_accounts, instance_types, ri_graph]

    #print excel_template.sheetnames    

    file_list = ['ri-subscriptions.csv', 'ri-subscriptions (1).csv', 'costs.csv', 'ec2-recommendations.csv', 'ri-instanceTypes.csv', 'costs_(1).csv', 'costs_(2).csv', 'costs_(3).csv', 'costs_(4).csv', 'costs (5).csv', 'costs (6).csv', 'costs (7).csv', 'costs (8).csv', 'ri-utilization.csv']

    i = 0
    for file in file_list:

        # catching empty file excpetions in case there is no data to read and/or transpose so this tab needs to be skipped


        print (file)


        wb = openpyxl.Workbook()
        ws = wb.active

        # for transposed files
        if file == 'costs_(1).csv' or file == 'costs_(2).csv' or file == 'costs_(3).csv' or file == 'costs_(4).csv' or file == 'costs_(5).csv' or file == 'costs (6).csv' or file == 'costs (7).csv' or file == 'costs (8).csv':

            df = pd.read_csv(file)
            df = df.transpose()

            print ('hit transpose')

            for r in dataframe_to_rows(df, index=False, header=True):
                ws.append(r)

            ws.delete_rows(1) # necessary to delete the index row that gets created when transposing

        else:

            df = pd.read_csv(file)

            print ('hit non transpose')

            for r in dataframe_to_rows(df, index=False, header=True):
                ws.append(r)
    excel_template.save('cost_optimization_template_{0}.xlsx'.format(account_id))

问题似乎只发生在粘贴之前需要转置的文件,因为其他文件实际上粘贴没有问题。

我在这里阅读了其他答案,但没有任何帮助我解决这个问题。任何帮助将非常感激。

以下是完整的回溯错误:

Traceback (most recent call last):
  File "cost_optimization.py", line 701, in <module>
    read_transpose('111')
  File "cost_optimization.py", line 612, in read_transpose
    ws.append(r)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/worksheet/worksheet.py", line 654, in append
    cell = Cell(self, row=row_idx, column=col_idx, value=content)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/cell/cell.py", line 120, in __init__
    self.value = value
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/cell/cell.py", line 252, in value
    self._bind_value(value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/cell/cell.py", line 218, in _bind_value
    raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert 0 to Excel
python excel openpyxl
1个回答
0
投票

原来是在转置时创建的标题,而openpyxl似乎不喜欢那里的0,在dataframe_to_rows(df,index = False,header = True)中将r更改为header = False解决了它。

© www.soinside.com 2019 - 2024. All rights reserved.