无法在 Plone 4 中解析 CSV

发布于 2024-11-27 03:00:31 字数 1919 浏览 0 评论 0 原文

我正在尝试将 CSV 文件中的一些虚拟内容批量加载到开发站点中以进行一些测试。我在 Mac OS X 10.6.6 上使用 Plone 4.0.1 和 Python 2.6.5。

1)我想我应该创建一个快速脚本,该脚本将迭代 CSV 文件,然后创建一些自定义内容类型:(类似于 http://plone.org/documentation/kb/batch-adding-users)。在 Plone 3 中,我已经能够以这种方式解析 CSV 文件。

但是,我在分割时遇到了 AttributeError 。我正在从我的 ipython (ipzope) 测试中复制:

>>> portal
<PloneSite at /Plone>
>>> portal['Scripts']['dummydata.csv']
<File at /Plone/Scripts/dummydata.csv>
>>> dummy = portal['Scripts']['dummydata.csv']
>>> dummy
<File at /Plone/Scripts/dummydata.csv>
>>> dummy.data.split('\n')
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
AttributeError: split

>>> dummy.split('\n')                                               
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
AttributeError: split

2) 最终,我想使用标准库中的 csv,但这也不起作用。

>>> import csv
>>> csv
<module 'csv' from '/Applications/Plone/Python-2.6/lib/python2.6/csv.pyc'>
>>> spamReader = csv.reader(dummy, delimiter=',', quotechar='"')
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
TypeError: argument 1 must be an iterator

>>> spamReader = csv.reader(dummy.data, delimiter=',', quotechar='"') 
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
TypeError: argument 1 must be an iterator

有什么想法吗?

最好的, 帕特里克

I am trying to batch load some dummy content from a CSV file into a development site to do some testing. I'm using Plone 4.0.1, with Python 2.6.5, on a Mac OS X 10.6.6.

1) I thought I would create a quick script that would iterate through a CSV file and then create some of my custom contenttypes: (Similar to http://plone.org/documentation/kb/batch-adding-users). In Plone 3, I had been able to parse CSV files in this fasion.

However, I got an AttributeError on split. I'm copying from my ipython (ipzope) testing:

>>> portal
<PloneSite at /Plone>
>>> portal['Scripts']['dummydata.csv']
<File at /Plone/Scripts/dummydata.csv>
>>> dummy = portal['Scripts']['dummydata.csv']
>>> dummy
<File at /Plone/Scripts/dummydata.csv>
>>> dummy.data.split('\n')
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
AttributeError: split

>>> dummy.split('\n')                                               
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
AttributeError: split

2) Ultimately, I'd like to use csv from the standard library, which also did not work.

>>> import csv
>>> csv
<module 'csv' from '/Applications/Plone/Python-2.6/lib/python2.6/csv.pyc'>
>>> spamReader = csv.reader(dummy, delimiter=',', quotechar='"')
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
TypeError: argument 1 must be an iterator

>>> spamReader = csv.reader(dummy.data, delimiter=',', quotechar='"') 
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
TypeError: argument 1 must be an iterator

Any ideas?

Best,
Patrick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上青苔 2024-12-04 03:00:31

您可以尝试这样的操作:

>>> from StringIO import StringIO
>>> csv_io = StringIO(dummy.data)
>>> csv_reader = csv.reader(csv_io, delimiter=',', quotechar='"')
>>> for i in csv_reader: print i
['a','b','c']
['d','e','f']
...

有关 python 和 csv 的更多信息可以在这里找到: http://docs .python.org/library/csv.html

再见,
贾科莫

You could try something like this:

>>> from StringIO import StringIO
>>> csv_io = StringIO(dummy.data)
>>> csv_reader = csv.reader(csv_io, delimiter=',', quotechar='"')
>>> for i in csv_reader: print i
['a','b','c']
['d','e','f']
...

More info about python and csv can be found here: http://docs.python.org/library/csv.html

Bye,
Giacomo

小瓶盖 2024-12-04 03:00:31

这里的核心问题是依赖于 File 对象的内部。例如,文件实现可以使用链接数据结构,其中顶级“File.data”对象本身具有到“File.data.data”对象的链接,以分解数据库中的对象,从而允许读取文件的一部分无需从数据库加载整个内容。在这种情况下,您只能直接访问属性来获取部分文件数据。但这只是依赖内部结构时可能遇到的陷阱的一个示例。

相反,使用 File 对象显式支持的接口访问文件内容。假设 File 对象是 OFS.Image.File 实例,我认为它唯一支持的是 str()。因此,如果您唯一的选择是在 ZODB 中使用 OFS.Image.File 实例,那么最好的选择是执行 str(dummy).split('\n')。

也就是说,对于大文件来说,这可能会占用大量内存。最好从文件系统上的文件加载 CSV,Python CSV 模块将有效地访问该文件,而无需将其全部加载到内存中。

The core problem here is relying on the internals of the File object. For example, the File implementation may use a linked data structure where the top level "File.data" object itself has a link to a "File.data.data" object to break up the objects in the database allowing reading part of the file without loading the whole thing from the database. In this case, you'd only get part of the file data accessing the attribute directly. But this is just an example of the kind of gotchas you can run into when relying on internals.

Instead, access the file contents using an interface explicitly supported by the File object. Assuming the File object is a OFS.Image.File instance, the only thing I see it supporting is str(). So your best bet if your only option is to use a OFS.Image.File instance in the ZODB is to do str(dummy).split('\n').

That said, this will likely be very memory intensive for large files. You'd be much better off loading your CSV from a file on the filesystem which the Python CSV module will access efficiently without loading it all into memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文