在 GAE 上使用 PHP 解析远程 csv 文件
我似乎陷入了第 22 条军规,我正在使用 Quercus 在 Google App Engine 上用 PHP 开发一个小应用程序;
- 我有一个远程 csv 文件,我可以下载并下载它。存储在字符串中
- 要解析该字符串,我最好使用 str_getcsv,但 Quercus 还没有该函数
- Quercus 似乎确实知道 fgetcsv,但该函数需要一个我没有的文件句柄(而且我不能创建一个新的,因为 GAE 不允许创建文件)
有人知道如何解决这个问题,而不必放弃内置的 PHP csv 解析器函数并编写我自己的解析器吗?
I seem to be in a catch-22 with a small app I'm developing in PHP on Google App Engine using Quercus;
- I have a remote csv-file which I can download & store in a string
- To parse that string I'd ideally use str_getcsv, but Quercus doesn't have that function yet
- Quercus does seem to know fgetcsv, but that function expects a file handle which I don't have (and I can't make a new one as GAE doesn't allow files to be created)
Anyone got an idea of how to solve this without having to dismiss the built-in PHP csv-parser functions and write my own parser instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为最简单的解决方案确实是编写自己的解析器。无论如何,这都是小菜一碟,并且会让您学习更多正则表达式 - PHP 中没有用于数组解析器的 csv 字符串是没有意义的,因此完全有理由编写自己的正则表达式。只要确保它不会太慢即可;)
I think the simplest solution really is to write your own parser . it's a piece of cake anyway and will get you to learn more regex- it makes no sense that there is no csv string to array parser in PHP so it's totally justified to write your own. Just make sure it's not too slow ;)
您也许可以使用 stream_wrapper_register。
以下是手册中读取全局变量的示例: http:// www.php.net/manual/en/stream.streamwrapper.example-1.php
然后您可以像普通文件句柄一样使用它:
You might be able to create a new stream wrapper using stream_wrapper_register.
Here's an example from the manual which reads global variables: http://www.php.net/manual/en/stream.streamwrapper.example-1.php
You could then use it like a normal file handle:
这显示了我用带有限定、非限定、转义功能的示例输入编写的一个简单的手动解析器。它可用于标题行和数据行,并包含一个 assoc 数组函数,使您的数据成为 kvp 样式数组。
this shows a simple manual parser i wrote with example input with qualifed, non-qualified, escape feature. it can be used for the header and data rows and included an assoc array function to make your data into a kvp style array.
如果它可以又脏又快的话。我只想用
http://php.net/manual/en/function.exec.php
传递它并使用 sed 和 awk (http://shop.oreilly.com/product/9781565922259.do) 来解析它。我知道你想使用 php 解析器。我以前曾尝试过,但失败了,只是因为它没有直言不讳地表达自己的错误。
希望这有帮助。
祝你好运。
if it can be dirty and quick. I would just use the
http://php.net/manual/en/function.exec.php
to pass it in and use sed and awk (http://shop.oreilly.com/product/9781565922259.do) to parse it. I know you wanted to use the php parser. I've tried before and failed simply because its not vocal about its errors.
Hope this helps.
Good luck.
您也许可以将
fopen
与php://temp
或php://memory
(php.net) 使其正常工作。您要做的就是打开php://temp
或php://memory
,写入内容,然后倒带(php.net),然后将其传递给 fgetcsv。我没有测试这个,但它可能有效。You might be able to use
fopen
withphp://temp
orphp://memory
(php.net) to get it to work. What you would do is open eitherphp://temp
orphp://memory
, write to it, then rewind it (php.net), and then pass it to fgetcsv. I didn't test this, but it might work.