python - Python将整个列表作为参数传递而不对其进行迭代?
问题描述
我是 Python 和 Pandas 的新手。下面是部分代码。当下面被硬编码时,它可以正常工作并生成预期的 json 文件。但是在函数中作为参数传递时,它不会生成任何文件。没有抛出错误。即使我将 i_indexList 硬编码为 [3,5,6,18,19] 这也不起作用,这意味着即使 i_fname 和 i_code 也没有正确传递。
需要将此作为函数,因为需要创建多个 json 文件,其名称和内容将根据用户输入而变化。因此它不能被硬编码。
请帮助以下:
- 当输出是 .json 文件时,下面标记的 X 行中要提到的返回值应该是什么。
- 作为参数传递的 List 的语法是否有任何问题。(Y 线和 Z 线)
- 这是由于压痕缺失吗?
def json_file(i_fname, i_indexList, i_code):
PathLink = os.path.join(path, i_fname + '.json') #Path Name Formation = os.path.join(path, 'test' + '.json') =C:/.../test.json
excel_data_df = pandas.read_excel('input.xlsx',
sheet_name='input_sheet1',
usecols=i_indexList , #starts from 0 ,i_indexList= [3,5,6,18,19]
dtype={'codenum': str}) # datatype is string for codenum column
df_filtered = excel_data_df.loc[excel_data_df["codenum"]==i_code] # Row Filtering = ["codenum"]=='AB123'
print(df_filtered)
df_filtered.columns = ['Code', 'Student Name', 'Class', 'School Name','City Name'] #renaming
cols_to_keep = ['Student Name', 'Class', 'School Name','City Name'] # columns to keep
df_filtered = df_filtered[cols_to_keep] # columns to keep
df_filtered # columns to keep
json_str = df_filtered.to_json(PathLink,orient='records',indent=2) #json converted file
#? return what? return json_str? -- Line X
#1. Mapping as per user chooses
if Priendpoint.get() == ' Code 1':
#Function call to create json file
indexList=[3,5,6,18,19] --Line Y
json_file('test', *indexList, 'codenum') --Line Z
解决方案
推荐阅读
- google-cloud-dataflow - 谷歌数据流可以使用现有的虚拟机而不是临时创建的虚拟机吗?
- typescript - 是否可以在 rxjs6 和 Angular 中共享订阅事件?
- json - jq,拆分一个巨大的json数组并保存到以值命名的文件中
- git - 詹金斯在哪里克隆 git repo
- google-sheets - 如何从另一个工作表获取多个 VLOOKUP 到 ARRAYFORMULA 内的同一单元格?
- asp.net - 如何将 JS 对象传递给 ASP.net 控制器?
- go - 如何在 mux.Vars(request) 中设置值
- spring-boot - 完全尝试在 Spring Boot 中理解 kotlin DI
- python - sqlalchemy 编译错误:无法为 NullType() 生成 DDL
- c# - 使用 Udi Dahan 事件示例在 ASP.NET Core 中注册事件处理程序