如何比较现有 excel 文件中的字典数据
how to compare dict data within existing excel file
我怎样才能将我以字典格式获得的数据与现有的 xlsx 文件进行比较。
字典键与 excel header 相同。比较简单的方法是什么?
这是示例:
字典中的数据:
{'Sheet1': [{'ID': 1.0, 'NAME': 'hostname1', 'IP_ADDRESS': '192.168.1.1', 'STATUS': 'completed'}, {'ID': 2.0, 'NAME': 'hostname2', 'IP_ADDRESS': '192.168.1.2', 'STATUS': 'completed'}, {'ID': 3.0, 'NAME': 'hostname3', 'IP_ADDRESS': '192.168.1.3', 'STATUS': 'in_progress'}, {'ID': 4.0, 'NAME': 'hostname4', 'IP_ADDRESS': '192.168.1.4', 'STATUS': 'completed'}]}
Excel 文件:
我想将字典值与 excel 文件进行比较(通过 sheet),如果字典中的值不同,请更新 excel 文件。如果 excel 中不存在该条目,只需在 excel 中添加新条目即可。
我应该如何开始?比较、更新和扩展 excel 文件 sheet 中的新条目。谢谢
##### 更新
def writexlsx_multi_sheets(filename, data, sheet_name, **to_excel_kwargs):
from openpyxl import load_workbook
orginalDF = pd.read_excel(filename, sheet_name=sheet_name, index=False)
newDF = pd.DataFrame.from_dict(data)
if 'engine' in to_excel_kwargs:
to_excel_kwargs.pop('engine')
writer = pd.ExcelWriter(filename, engine='openpyxl')
writer.book = load_workbook(filename)
writer.sheets = {ws.title:ws for ws in writer.book.worksheets}
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
if not orginalDF.equals(newDF):
result = orginalDF.combine_first(newDF)
result.to_excel(writer, sheet_name=sheet_name, index=False)
writer.save()
else:
return False
首先,将您的字典转换为数据框。
import pandas as pd
data = {'ID': 1.0, 'NAME': 'hostname1', 'IP_ADDRESS': '192.168.1.1', 'STATUS': 'completed'}, {'ID': 2.0, 'NAME': 'hostname2', 'IP_ADDRESS': '192.168.1.2', 'STATUS': 'completed'}, {'ID': 3.0, 'NAME': 'hostname3', 'IP_ADDRESS': '192.168.1.3', 'STATUS': 'in_progress'}, {'ID': 4.0, 'NAME': 'hostname4', 'IP_ADDRESS': '192.168.1.4', 'STATUS': 'completed'}
df1 = pd.DataFrame.from_dict(data)
df1
# output
ID NAME IP_ADDRESS STATUS
0 1 hostname1 192.168.1.1 completed
1 2 hostname2 192.168.1.2 completed
2 3 hostname3 192.168.1.3 in_progress
3 4 hostname4 192.168.1.4 completed
然后将您的 xlsx 文件读取到另一个数据框。假设您在 excel 文件
中少了一个数据
df2 = pd.read_csv('filename')
df2
# output
ID NAME IP_ADDRESS STATUS
0 1 hostname1 192.168.1.1 completed
1 2 hostname2 192.168.1.2 completed
2 3 hostname3 192.168.1.3 NaN
现在使用combine_first()
功能检查
df3 = df2.combine_first(df1)
df3
# output
ID NAME IP_ADDRESS STATUS
0 1 hostname1 192.168.1.1 completed
1 2 hostname2 192.168.1.2 completed
2 3 hostname3 192.168.1.3 in_progress
3 4 hostname4 192.168.1.4 completed
比较所有内容后,您可以将其保存到 excel 文件
df3.to_csv('filename')
我怎样才能将我以字典格式获得的数据与现有的 xlsx 文件进行比较。 字典键与 excel header 相同。比较简单的方法是什么? 这是示例: 字典中的数据:
{'Sheet1': [{'ID': 1.0, 'NAME': 'hostname1', 'IP_ADDRESS': '192.168.1.1', 'STATUS': 'completed'}, {'ID': 2.0, 'NAME': 'hostname2', 'IP_ADDRESS': '192.168.1.2', 'STATUS': 'completed'}, {'ID': 3.0, 'NAME': 'hostname3', 'IP_ADDRESS': '192.168.1.3', 'STATUS': 'in_progress'}, {'ID': 4.0, 'NAME': 'hostname4', 'IP_ADDRESS': '192.168.1.4', 'STATUS': 'completed'}]}
Excel 文件:
我想将字典值与 excel 文件进行比较(通过 sheet),如果字典中的值不同,请更新 excel 文件。如果 excel 中不存在该条目,只需在 excel 中添加新条目即可。
我应该如何开始?比较、更新和扩展 excel 文件 sheet 中的新条目。谢谢
##### 更新def writexlsx_multi_sheets(filename, data, sheet_name, **to_excel_kwargs):
from openpyxl import load_workbook
orginalDF = pd.read_excel(filename, sheet_name=sheet_name, index=False)
newDF = pd.DataFrame.from_dict(data)
if 'engine' in to_excel_kwargs:
to_excel_kwargs.pop('engine')
writer = pd.ExcelWriter(filename, engine='openpyxl')
writer.book = load_workbook(filename)
writer.sheets = {ws.title:ws for ws in writer.book.worksheets}
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
if not orginalDF.equals(newDF):
result = orginalDF.combine_first(newDF)
result.to_excel(writer, sheet_name=sheet_name, index=False)
writer.save()
else:
return False
首先,将您的字典转换为数据框。
import pandas as pd
data = {'ID': 1.0, 'NAME': 'hostname1', 'IP_ADDRESS': '192.168.1.1', 'STATUS': 'completed'}, {'ID': 2.0, 'NAME': 'hostname2', 'IP_ADDRESS': '192.168.1.2', 'STATUS': 'completed'}, {'ID': 3.0, 'NAME': 'hostname3', 'IP_ADDRESS': '192.168.1.3', 'STATUS': 'in_progress'}, {'ID': 4.0, 'NAME': 'hostname4', 'IP_ADDRESS': '192.168.1.4', 'STATUS': 'completed'}
df1 = pd.DataFrame.from_dict(data)
df1
# output
ID NAME IP_ADDRESS STATUS
0 1 hostname1 192.168.1.1 completed
1 2 hostname2 192.168.1.2 completed
2 3 hostname3 192.168.1.3 in_progress
3 4 hostname4 192.168.1.4 completed
然后将您的 xlsx 文件读取到另一个数据框。假设您在 excel 文件
中少了一个数据df2 = pd.read_csv('filename')
df2
# output
ID NAME IP_ADDRESS STATUS
0 1 hostname1 192.168.1.1 completed
1 2 hostname2 192.168.1.2 completed
2 3 hostname3 192.168.1.3 NaN
现在使用combine_first()
功能检查
df3 = df2.combine_first(df1)
df3
# output
ID NAME IP_ADDRESS STATUS
0 1 hostname1 192.168.1.1 completed
1 2 hostname2 192.168.1.2 completed
2 3 hostname3 192.168.1.3 in_progress
3 4 hostname4 192.168.1.4 completed
比较所有内容后,您可以将其保存到 excel 文件
df3.to_csv('filename')