3

This is probably an easy fix, but I can't seem to figure it out...

outputting a list to CSV in Python using the following code:

w = csv.writer(file('filename.csv','wb'))
w.writerows(mylist)

One of the list items is a ratio, so it contains values like '23/54', '9/12', etc. Excel is recognizing some of these values (like 9/12) as a date. What's the easiest way to solve this?

Thanks

4

2 回答 2

3

因为csv是纯文本格式,恐怕您无法告诉 Excel 如何解释数据。

您必须生成实际的 Excel 文件(xlwt例如,使用http://www.python-excel.org/上提供的文档和教程)。

于 2012-10-18T21:47:29.663 回答
2

你可以这样做:

# somelist contains data like '12/51','9/43' etc
mylist = ["'" + val + "'" for val in somelist]
w = csv.writer(open('filename.csv','wb'))
for me in mylist:
    w.writerow([me])

这将确保您的数据按原样写入 csv。

于 2013-04-17T12:43:32.667 回答