regex来解析csv [英] regex to parse csv
问题描述
我正在寻找一个正则表达式,将从一个csv文件解析一行。基本上,string.readline()的作用,但它将允许换行符,如果他们在双引号内。
I'm looking for a regex that will parse a line at a time from a csv file. basically, what string.readline() does, but it will allow line breaks if they are within double quotes.
还是有更容易的方法? p>
or is there an easier way to do this?
推荐答案
使用正则表达式解析CSV对于受到良好控制的CSV数据的简单应用程序来说是好的,但是通常有这么多< ,例如在引号字符串中隐藏嵌入的引号和逗号等。这通常会使正则表达式变得棘手并且有风险。
Using regex to parse CSV is fine for simple applications in well-controlled CSV data, but there are often so many gotchas, such as escaping for embedded quotes and commas in quoted strings, etc. This often makes regex tricky and risky for this task.
我建议您出于您的目的使用经过良好测试的CSV模块。
I recommend a well-tested CSV module for your purpose.
- 编辑: - 见这篇优秀的文章停止滚动您自己的CSV解析器!
---- See this excellent article, Stop Rolling Your Own CSV Parser!
这篇关于regex来解析csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!