在Python中解析内存中的CSV数据 [英] Parsing CSV data from memory in Python

查看:148
本文介绍了在Python中解析内存中的CSV数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数据不在文件中时,是否可以用Python解析CSV数据?我正在将CSV数据存储在数据库中,并且希望对其进行解析。我正在寻找类似于Ruby的 CSV.parse 的东西。我知道Python有一个 CSV 类,但是我在文档中看到的所有内容似乎都是在处理文件,而不是内存中的CSV数据。

Is there a way to parse CSV data in Python when the data is not in a file? I'm storing CSV data in my database and I'd like to parse it. I'm looking for something analogous to Ruby's CSV.parse. I know Python has a CSV class but everything I've seen in the docs seems to deal with files as opposed to in-memory CSV data.

(而且在数据进入数据库之前也不是解析的选择。)

(And it's not an option to parse the data before it goes into the database.)

(而且请不要告诉我不要将CSV数据存储在数据库中。我知道我正在做的数据库。)

(And please don't tell me not to store the CSV data in the database. I know what I'm doing as far as the database goes.)

推荐答案

有对于有关python csv 模块的文件没有特殊区别。您可以使用 StringIO 将字符串包装为类似文件的对象。

There is no special distinction for files about the python csv module. You can use StringIO to wrap your strings as file-like objects.

这篇关于在Python中解析内存中的CSV数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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