简体   繁体   English

使用不同的输入多次运行 Python 脚本

[英]Run Python script multiple times with different inputs

I am new to programming.我是编程新手。

My script does the following:我的脚本执行以下操作:

  1. Converts single Excel sheet into single CSV file将单个 Excel 表转换为单个 CSV 文件

  2. Opens that CSV file and adds new column with number and saves it打开 CSV 文件并添加带有数字的新列并保存

import pandas as pd

data_xls = pd.read_excel('excel_file.xlsx', ' tab_name ', index_col=None)
data_xls.to_csv('tab_name.csv', encoding='utf-8', index=False)
data_csv = pd.read_csv('tab_name.csv')
data_csv.insert(0, 'new_column_header', range(1, 1 + len(df)))
data_csv.to_csv('tab_name.csv', index=False )

My challenge:我的挑战:

Excel document consists of multiple sheets (tabs) let's say: "tab1, tab2, tab3". Excel 文档由多个工作表(选项卡)组成,假设:“tab1,tab2,tab3”。

I need guidance how to run the same script in a loop over multiple tabs (ie list).我需要指导如何在多个选项卡(即列表)上循环运行相同的脚本。

One way is to hard-code your tab names in a list and loop over them.一种方法是将您的选项卡名称硬编码在列表中并循环遍历它们。

for tab in ["tab1", "tab2", "tab3"]:
    data_xls = pd.read_excel('excel_file.xlsx', tab, index_col=None)
    data_xls.to_csv(tab + '.csv', encoding='utf-8', index=False)
    data_csv = pd.read_csv(tab + '.csv')
    data_csv.insert(0, 'new_column_header', range(1, 1 + len(df)))
    data_csv.to_csv(tab + '.csv', index=False )

Here is what you need:这是您需要的:

dict_dfs = pd.read_excel("file.xlsx", sheet_name=None)

This will return you an OrderedDict whose keys will be the sheet names and values will be dataframes.这将返回一个 OrderedDict,其键将是工作表名称,值将是数据框。

All you need is to loop over and do your manipulations and then save the individual dataframes as csv.您只需要循环并进行操作,然后将各个数据帧保存为 csv。

for k in dict_dfs:
    # your processing here
    dict_dfs[k].to_csv("custom_filename_for_each_dataframe.csv")

Hope this helps.希望这可以帮助。

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

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