【发布时间】:2016-09-05 07:38:54
【问题描述】:
我需要更新库存清单,我该如何以这种方式写入 CSV 文件。请问您如何写入 CSV 文件?我试过搜索这个,但没有什么能真正解释他们为什么这样做。
【问题讨论】:
-
@Tonechas 不,您引用的帖子来自没有得到他们想要的东西的人。但他们知道如何写入 csv 文件。
我需要更新库存清单,我该如何以这种方式写入 CSV 文件。请问您如何写入 CSV 文件?我试过搜索这个,但没有什么能真正解释他们为什么这样做。
【问题讨论】:
https://docs.python.org/2/library/csv.html
import csv
with open('some.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(someiterable)
代码说明
一个可迭代对象可以是一个简单的列表,例如
my_iterable = ["Printed to line 1", "Printed to line 2"]
【讨论】:
正如您所说,您有一个股票“列表”,我更愿意使用 pandas 写入 .csv。考虑到您有一个名为“stock_list”的股票列表
stock_list = [list of elements]
import pandas as pd
df = pd.DataFrame(stock_list, columns=["whatever name you want to give"])
df.to_csv("path to which you need to save", index=True/False (Depends on whether you need the index or not))
【讨论】: