如何使用 Python 将表格数据转换为 Excel 文件
使用 Python 的 pandas 库可以轻松地将类似于 HTML 表格格式的数据转换为 Excel 文件。
首先,你需要将数据转换为一个列表或字典。然后,使用 pandas 库中的 DataFrame 函数将数据转换为 DataFrame 对象。最后,使用 to_excel 方法将 DataFrame 对象保存为 Excel 文件。
以下是一个示例代码:
import pandas as pd
data = [
['1', 'Alert 1', '2021', '2021-01-01', 'Closed', 'https://example.com/1', 'JIRA-1', 'Resolved'],
['2', 'Alert 2', '2020', '2020-01-01', 'Open', 'https://example.com/2', 'JIRA-2', 'In Progress'],
# 添加更多数据行...
]
# 创建 DataFrame 对象
df = pd.DataFrame(data, columns=['Phantom Ticket ID', 'Alert Name', 'Year Created', 'Full Date', 'Case Closed Category', 'Phantom Ticket Link', 'Jira Ticket ID', 'Status'])
# 保存为 Excel 文件
df.to_excel('output.xlsx', index=False)
运行此代码后,会生成一个名为 output.xlsx 的 Excel 文件,其中包含转换后的数据。你可以根据需要修改列名和文件名。
原文地址: https://www.cveoy.top/t/topic/piQp 著作权归作者所有。请勿转载和采集!