Match string:
1. Match a CSV record and capture the field in column 1 to backreference 1
(?m)^([^",\r\n]+|"(?:[^"]|"")*")?(?:,(?:[^",\r\n]+|"(?:[^"]|"")*")?)*
2. Match a CSV record and capture the field in column 2 to backreference 1
(?m)^(?:[^",\r\n]+|"(?:[^"]|"")*")?,([^",\r\n]+|"(?:[^"]|"")*")?(?:,(?:[^",\r\n]+|"(?:[^"]|"")*")?)*
3. Match a CSV record and capture the field in column 3 or higher to backreference 1
(?m)^(?:[^",\r\n]+|"(?:[^"]|"")*")?(?:,(?:[^",\r\n]+|"(?:[^"]|"")*")?){1},([^",\r\n]+|"(?:[^"]|"")*")?(?:,(?:[^",\r\n]+|"(?:[^"]|"")*")?)*
Increment the number within the ?{1}? quantifier to make this last regex work for any-
thing higher than column 3. For example, change it to ?{2}? to capture fields from
column 4, ?{3}? for column 5, and so on. If you’re working with column 3, you can
simply remove the ?{1}? if you prefer, since it has no effect here.
Replace string:
\1
Python 正则表达式从CSV文件提取特定列
最新推荐文章于 2024-08-12 13:11:09 发布