简体   繁体   English

如何在Perl中处理CSV文件中的嵌入式换行符?

[英]How do I handle embedded newlines in CSV files in Perl?

I'm reading a .csv file that was created in Excel with the first line being column headings. 我正在读取在Excel中创建的.csv文件,第一行是列标题。 One column heading contains an embedded newline. 一个列标题包含一个嵌入式换行符。 I want to ignore that newline but reading it line-by-line like: 我想忽略该换行符,但逐行阅读,例如:

while ( <IN> ) { 
    ...
    }

will treat it as a new line which will break my code (which I haven't written yet). 会将其视为换行,这将破坏我的代码(我尚未编写)。 My approach was to read the first line into an array of column headings and process the rest of the lines differently. 我的方法是将第一行读入列标题数组,然后以不同方式处理其余各行。

Is there maybe a regex I can use somewhere in the while that ignores the newline unless it's the last new line? 有没有可能是正则表达式我可以在什么地方使用while忽略换行,除非它是最后一个新行?

Or should I be approaching this differently? 还是我应该以不同的方式来对待?

Use one of the Perl modules that handle CSV, such as Text::CSV_XS . 使用处理CSV的Perl模块之一,例如Text :: CSV_XS Its documentation shows you how to handle embedded newlines. 它的文档显示了如何处理嵌入式换行符。 In general, you don't want to spend your time writing another CSV parser; 通常,您不想花费时间编写另一个CSV解析器。 get on with the more important parts of your task! 继续执行任务中更重要的部分!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM