将单行格式化为CSV [英] Formatting a single row as CSV

查看:130
本文介绍了将单行格式化为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个脚本,将大量数据转换为CSV格式。它使用mapreduce API在Google AppEngine上运行,这只是相关的,因为它意味着每个数据行在回调函数中被格式化并单独输出。

I'm creating a script to convert a whole lot of data into CSV format. It runs on Google AppEngine using the mapreduce API, which is only relevant in that it means each row of data is formatted and output separately, in a callback function.

利用 csv 模块中已经存在的逻辑将我的数据转换为正确的格式,但因为CSV写入器需要一个类似文件的对象,必须为每行实例化 StringIO ,将该行写入对象,然后每次返回对象的内容。

I want to take advantage of the logic that already exists in the csv module to convert my data into the correct format, but because the CSV writer expects a file-like object, I'm having to instantiate a StringIO for each row, write the row to the object, then return the content of the object, each time.

这似乎很蠢,我想知道是否有任何方法访问 csv 模块的内部CSV格式化逻辑没有写入部分。 p>

This seems silly, and I'm wondering if there is any way to access the internal CSV formatting logic of the csv module without the writing part.

推荐答案

csv模块封装了以c语言编写的_csv模块。您可以获取它的源代码,并将其修改为不需要类似文件的对象,但是在模块中,我没有看到任何明确的方式来做它没有重新编译。

The csv module wraps the _csv module, which is written in C. You could grab the source for it and modify it to not require the file-like object, but poking around in the module, I don't see any clear way to do it without recompiling.

这篇关于将单行格式化为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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