【发布时间】:2020-01-31 05:49:06
【问题描述】:
我有一个 ID 列表,我正在循环访问,我正在获取字段和值列表。
现在我必须将两个列表都放入 Excel 文件中,其中 FIELDS 是列,VALUES 是该列的行正如我所说的,有 ID 列表,这意味着每个 id 都有不同的 VALUES 但字段是相同的。和 VALUES 应该有列表/字典或我们在值列表中找到的任何内容,我们会将其放入 excel 中。
为此我尝试过使用这些代码:
field_nameList = []
field_valueList = []
for id in IDS:
for field in id:
field_value = id[field]
..... some code........
field_name = id[field]
..... some code........
field_nameList.append(field_name)
field_valueList.append(field_value)
# Here after getting all the ID's Fields and Value i'm converting it to dictionary to export it to excel using pandas.
fieldsItems = dict(zip(field_nameList, field_valueList))
df = pd.DataFrame(fieldsItems)
df.to_excel('GXfile.xlsx', index=None,header=True)
对于单个 ID,它可以工作,但是当我有多个 ID,然后我尝试做同样的事情时,我得到了
错误:将字典与非系列混合可能会导致排序不明确。
我的主要动机是为每个 id 获取任何列表,我需要创建一个列,然后从下一个循环开始,继续为 FIELDS 添加行。
我知道我的代码实现可能不正确,但请建议我一个更好的方法。
在此处添加了字段名,即列名,值是示例 excel 数据
Id Resolution subStatus Last Viewed Σ Original Estimate Issue Type CCB Decision Reason For Escalation CCB Comments Issue Service Affecting? Estimated Effort to Fix Reproducibility Problem Description/Systems Impacted Symptoms Release Note Status Release CCB Estimated Effort to Verify Fix
1 None Screening Required 2020-01-30T06:38:45.668+0000 144000 Bug Fix Defer ccb testing Yes test Never test test Unspecified
2 None Screening Required 2020-01-30T06:38:45.668+0000 144000 Bug Fix Defer ccb testing Yes test Never test test Unspecified
@The Guy,我很抱歉没有正确解释我感谢帮助和代码,但这里有一个误解,我有 3 个列表,第一个列表是我从代码中动态获取的另外 2 个列表中的 ID,所以让我再写:
第一个列表:
ID = [1,2,3,4..]
第二个列表:
Field_Name =['A','B','C',...so on] this will be achieved by code
第三个列表:
Field_value =['A_values','B_values','C_values,....so on]
Excel 文件应包含:
ID A B C ....
1 A_values B_values C_values .....
在这里我们可以清楚地看到我将 Fields_name 作为列获取,并将 Filed_value 作为特定列的行......我们如何得到这个。
【问题讨论】:
-
请添加您的 field_nameList 和 field_valueList 的外观。
-
哦,让我添加@TheGuy
-
@TheGuy 请参考我从列表中获取列名和从列表中获取值的excel,当我的循环结束时,我最终将获得相同FIELDS的多个值我需要要将它们全部放入 Excel,我的 col 列表非常大,要粘贴到这里,所以我只复制了几个,如果您有任何其他疑问,请告诉我。谢谢