解析引号内文本中包含逗号的 csv 数据
我正在构建一个应用程序,该应用程序从 Excel .csv 文件中提取数据并应用各种级别的格式化、移动和映射。 除了清理数据方面的一个小问题外,大多数事情都已经弄清楚了。 以下是 Excel 中的数据示例:
- GREAT PERFORMANCES,GREAT PERFORMANCES,57744 ROUND LAKE RD,NEW YORK,NY
- "GUASTAVINO'S, INC",GUASTAVINO'S,8250 WESTHEIMER RD,NEW YORK,NY
- THE CLARKES GROUP LLC,HUGO'S FROG BAR, 915 3RD AVE,CHICAGO,IL
- TRIOMPHE RESTAURANT CORP,"GEORGE'S,JEAN",1309 E PUTNAM AVE,NEW YORK,NY
由于第 3 行的 2 和 4,我无法进行直接爆炸()。有一个“字符串,这两行上的“string”,我无法控制数据导出,只能在导入时进行清理。
我尝试了一些不优雅的选项,但它会浪费处理时间。 有人能想到一个优雅的解决方案吗?
我无法使用 MySQL IMPORT 功能,不幸的是它必须通过 PHP 处理。
I'm building an app that pulls data in from an excel .csv file and applying various levels of formatting, moving and mapping. Most everything is figured out except for one hitch with cleaning the data. Here is an example of the data from Excel:
- GREAT PERFORMANCES,GREAT PERFORMANCES,57744 ROUND LAKE RD,NEW YORK,NY
- "GUASTAVINO'S, INC",GUASTAVINO'S,8250 WESTHEIMER RD,NEW YORK,NY
- THE CLARKES GROUP LLC,HUGO'S FROG BAR,915 3RD AVE,CHICAGO,IL
- TRIOMPHE RESTAURANT CORP,"GEORGE'S,JEAN",1309 E PUTNAM AVE,NEW YORK,NY
I can't do a straight explode() because of line 3's 2 and 4. There is a "string, string" on both of those lines and I have no control over the data exporting, just cleaning on import.
I've tried a number of non-graceful options and it's killing the processing time. Can anyone think of an elegant solution?
I can't use the MySQL IMPORT functionality, it has to be handled via PHP unfortunately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以尝试执行 SQL 语句:
我听说您使用 PHP,但您可以从 PHP 运行上面的语句。 或者您的意思是输入是读取 CSV 产生的流,而不是 CSV 文件? 不幸的是,LOAD DATA INFILE 需要文件名,它不能接受标准输入。
You can also try executing a SQL statement:
I hear you about using PHP, but you can run the statement above from PHP. Or do you mean that the input is a stream resulting from reading CSV, not a CSV file? Unfortunately LOAD DATA INFILE requires a filename, it can't take standard input.
我看到你的问题已经得到解答。 但如果你想将文件读入 PHP 中使用,你可以使用 fgetcsv。
I see your question has been answered. But if you wanted to read the file into PHP for use, you could just have used fgetcsv.