如何打开文件并另存为cs

2024-04-25 18:56:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前从一个网站导出报告作为一个'csv'文件。但是,当我打开它们时,我看到一条弹出消息:

The file you are trying to open, 'Jul2015.csv', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?

我单击“是”,文件将以某种“xls”格式打开。所以我保存为“csv”格式,当我报告新保存的文件时,它遵循“csv”格式。在

所以我的问题是,如何在python中打开最初导出的报表并将其另存为“csv”以便避免格式不一致?在

谢谢


Tags: 文件csvthetoyou消息is网站
2条回答

这应该也可以,而且不使用外部库。在

with open("exported.csv", "r") as f:
    lines = f.readlines()
with open("xlstocsv.csv", "r") as g:
    g.writelines(lines)

尝试:

import pandas as pd
data_xls = pd.read_excel('export.csv', 'Sheet1', index_col=None)
data_xls.to_csv('exceltocsv.csv', encoding='utf-8')

相关问题 更多 >