在Python中编写.CSV文件,适用于Windows 2.7中的Python 2.7+和Python 3.3+ [英] Writing a .CSV file in Python that works for both Python 2.7+ and Python 3.3+ in Windows

查看:1664
本文介绍了在Python中编写.CSV文件,适用于Windows 2.7中的Python 2.7+和Python 3.3+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我把它放在标题,但只是意识到我没有在身体提到它。这似乎是特定于Windows。



我有一个很难写输出使用 csv Python模块



第一次尝试在Python 2.7中正常工作:



< $ c pre> with open('test.csv','wb')as csv_file:
writer = csv.DictWriter(csv_file,['header1','header2'])
writer.writeheader()
项目中的项目:
writer.writerow(item)


b $ b

然而,当同样的事情在Python 3.3中运行时,你会遇到:

  TypeError:'str '不支持缓冲区接口

所以我更改'wb' code>到'wt'并运行,但现在我有一个额外的空白行每隔一行在文件中。



为了解决这个问题,我改变:

  with open('test.csv','wt') as csv_file:

到:

  with open('test.csv','wt',newline ='')as csv_file:

但现在,它打破了Python 2.7:

  TypeError:'newline'对于此函数

我知道我可以做类似的操作:

  try:
with open('test.csv','wt',newline ='')as csv_file:
writer = csv。 DictWriter(csv_file,['header1','header2'])
writer.writeheader()
项目中的项目:
writer.writerow(item)
TypeError:
with open('test.csv','wb')as csv_file:
writer = csv.DictWriter(csv_file,['header1','header2'])
writer.writeheader
for item in items:
writer.writerow(item)



有没有人有一个更干净的方式这样做?



测试数据很简单,没有换行符或任何东西:

  items = [{'header1':'value','header2' :'value2'},
{'header1':'blah1','header2':'blah2'}]


解决方案

我试过几种方法。就我所见,简单使用'w'可能是一个解决方案:

 <$ csv_file,fieldnames = ['header1','header2'],lineterminator ='\\\
(csv_file,cn) ')
#write something


EDIT: I put it in the title, but just realized I didn't mention it in the body. This seems to be specific to Windows.

I'm having a hard time writing output using the csv Python module in a script that works with both Python 2.7 and 3.3.

First try which works as expected in Python 2.7:

with open('test.csv', 'wb') as csv_file:
    writer = csv.DictWriter(csv_file, ['header1', 'header2'])
    writer.writeheader()
    for item in items:
        writer.writerow(item)

However, when that same thing is run in Python 3.3 you wind up with:

TypeError: 'str' does not support the buffer interface

So I change 'wb' to 'wt' and it runs, but now I have an extra blank row every other line in the file.

To fix that, I change:

with open('test.csv', 'wt') as csv_file:

to:

with open('test.csv', 'wt', newline='') as csv_file:

But now, it breaks Python 2.7:

TypeError: 'newline' is an invalid keyword argument for this function

I know I could just do something like:

try:
    with open('test.csv', 'wt', newline='') as csv_file:
        writer = csv.DictWriter(csv_file, ['header1', 'header2'])
        writer.writeheader()
        for item in items:
            writer.writerow(item)
except TypeError:
    with open('test.csv', 'wb') as csv_file:
        writer = csv.DictWriter(csv_file, ['header1', 'header2'])
        writer.writeheader()
        for item in items:
            writer.writerow(item)

However, that has some seriously bad duplication.

Does anyone have a cleaner way of doing this?

EDIT: The test data is simple and has no newlines or anything:

items = [{'header1': 'value', 'header2': 'value2'},
         {'header1': 'blah1', 'header2': 'blah2'}]

解决方案

I've tried a few ways. As far as I can see, simple using 'w' could be a solution:

with open('test.csv', 'w') as csv_file:
    writer = csv.DictWriter(csv_file, fieldnames=['header1', 'header2'], lineterminator='\n')
    # write something

这篇关于在Python中编写.CSV文件,适用于Windows 2.7中的Python 2.7+和Python 3.3+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆