正则表达式解析CSV [英] regex to parse csv

查看:638
本文介绍了正则表达式解析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.

或有更简单的方法来做到这一点?

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!

这篇关于正则表达式解析CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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