正则表达式替换缺失值期间 [英] Regex for replacing missing value period

查看:96
本文介绍了正则表达式替换缺失值期间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用逗号作为分隔符解析csv数据文件。该文件主要包含整数,但也包含一些十进制数/浮点数。


它有一些缺失值,用句点(。)标记。为了插入到数据库中,我想要替换缺少的值期间,而不是小数期间。


使用python的正则表达式和re.sub构造应用于每一行读作一个字符串,有人可以帮助这个模式吗?


最好,

Aksel

解决方案

没有必要为此使用正则表达式。

展开 | 选择 | Wrap | 行号


就像这样的工作,你可以用一个只需一个''来替换''逗号句号逗号'' ''逗号'':

展开 | 选择 | Wrap | 行号

这是我最初希望的,但如果几个句点相邻则无法替换所有句点,因为最后一个引号似乎被第一个匹配使用,并作为下一个匹配中的第一个引号跳过。因此它每隔一段时间跳过一次。

展开 | 选择 | Wrap | 行号


I am parsing a csv data file with comma as the delimiter. The file mostly contains integers but also some decimal numbers/floats.

It has some missing values, which are marked by a period ("."). In order to insert into a database, I want to replace the missing value periods, but NOT the decimal periods.

With python''s regex flavor and the re.sub construct applied to each line read as a string, can someone help what the pattern might be for this?

Best,
Aksel

解决方案

There is no need to use regex for this.

Expand|Select|Wrap|Line Numbers


Wouldn''t something like this work where you could replace the ''comma period comma'' with just a ''comma'':

Expand|Select|Wrap|Line Numbers


That''s what I initially hoped, but it fails to replace all periods if several periods are adjacent because the last quote is seemingly used by the first match and skipped as the first quote in the next match. It accordingly skips every second period.

Expand|Select|Wrap|Line Numbers


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

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