Python模块csv写入时的自动格式化

2024-09-28 22:21:14 发布

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

我如何防止Python自动将对象写入csv,而不是以原来的格式?例如,我有如下列表对象:

row = ['APR16', '100.00000']

我想按原样写这一行,但是当我使用csv writer的writerow函数时,它写入到csv文件中的值是Apr 16,而只有10。我想保留原来的格式。在

编辑:

代码如下:

^{pr2}$

结果:

{1美元^


Tags: 文件csv对象函数代码编辑列表格式
1条回答
网友
1楼 · 发布于 2024-09-28 22:21:14

使用pandas

import pandas as pd

dates = ['APR16', 'MAY16', 'JUN16']
numbers = [100.00000, 200.00000, 300.00000]

data = zip(dates,numbers) 

fd = pd.DataFrame(data)

fd.to_csv('test.csv', index=False, header=False)  # csv-file
fd.to_excel("test.xls", header=False,index=False) # or xls-file

我的终端结果:

^{pr2}$

产生LibreOffice:

enter image description here

相关问题 更多 >