在实际字段中导入具有换行符的CSV [英] Importing CSV that has line breaks within the actual fields

查看:174
本文介绍了在实际字段中导入具有换行符的CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP导入源自excel电子表格的CSV文件。一些字段在其中包含换行符,因此当我在excel / open office电子表格中重新打开csv时,它会误解换行的位置。

I am using PHP to import a CSV file, which originates from an excel spreadsheet. Some of the fields contain line breaks in them, so when I reopen the csv in excel / open office spreadsheet, it misinterprets where the line breaks should happen.

脚本,使用fgetcsv通过每一行,它是不正确的换行不应该是。

Also in my script, using fgetcsv to go through each line, it is incorrectly line breaking where it shouldn't be.

我可以手动清理数据,但a)作为其10k行文件,b)数据从客户端现有的软件中导出

I could manually cleanse the data but a) that would take ages as its a 10k line file, and b) the data is exported from a clients existing piece of software

有关如何在导入过程中自动解决这个问题的任何想法?

Any ideas on how to automatically solve this on the import process? I would have thought delimiting the fields would have sorted it but it does not.

推荐答案

我也有这个问题,没有找到一种正确读取数据的方法。

I had that problem too and did not find an way to read the data correctly.

在我的例子中,这是一次性导入,所以我制作了一个脚本,搜索列中的所有换行符并替换为 ##### 。然后我导入数据并替换为换行符。

In my case it was an one-time-import, so i made an script that searched for all line-breaks within an column and replaced it with something like #####. Then I imported the data and replaced that by linebreaks.

如果你需要一个常规导入,你可以写自己的CSV解析器,处理这个问题。如果文本列在中,则可以将两个之间的一切视为一列在内容中转义)。

If you need an regular import you could write you own CSV-Parser, that handles the problem. If the text-columns are within "" you could treat everything between two "" as one columns (with check for escaped " within the content).

这篇关于在实际字段中导入具有换行符的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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