简体   繁体   English

如何打开文件并另存为csv

[英]How to open file and save as csv

I currently export reports from a website as a 'csv' file. 我目前从网站将报告导出为“ csv”文件。 But, when I open them I get this pop up message: 但是,当我打开它们时,我得到以下弹出消息:

The file you are trying to open, 'Jul2015.csv', is in a different format than specified by the file extension. 您尝试打开的文件'Jul2015.csv'的格式与文件扩展名指定的格式不同。 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? 您要立即打开文件吗?

I click 'yes' and the file opens in some sort of 'xls' format. 我单击“是”,文件以某种“ xls”格式打开。 So I save as a 'csv' format and when I report the newly saved file it follows the 'csv' format. 因此,我将其另存为“ csv”格式,并在报告新保存的文件时遵循“ csv”格式。

So my question is, how can I in python open the initial exported report and save as a 'csv' so I can avoid the inconsistent formatting? 所以我的问题是,我该如何在python中打开初始导出的报告并另存为“ csv”,从而避免格式不一致?

Thanks 谢谢

try: 尝试:

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

this should work too, and doesn't use an external library. 这也应该工作,并且不使用外部库。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM