删除数据流中的分号

2024-05-26 11:55:37 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我想删除我的数据文件中的一些分号,如下所示:

a;bfcse;g
g;qdq;d;u
q;g;sd;;o
c;dd;ea;b
w;;rz;z;m

我想用正则表达式来实现这个(因为我试图选择一列并进行替换,但它不起作用notepad++中的按钮在我的选择中是灰色的):

a;bfcse;g
g;qdqd;u
q;gsd;o
c;ddea;b
w;rzz;m

我想像子字符串这样的东西可以工作,但用记事本++和升华文本是很难的。。。你知道吗

你有什么想法吗?你知道吗

非常感谢!你知道吗

编辑:但我想我要做的很清楚,就是在这一区域不使用任何分号:

  -------
a;|bfcse|;g
g;|qdq;d|;u
q;|g;sd;|;o
c;|dd;ea|;b
w;|;rz;z|;m
  -------

Tags: 字符串数据文件sd按钮ddea灰色gsd
2条回答

使用记事本++

  • Ctrl+H
  • 查找内容:(^.+?;|\G)(.*?);?(?=.*?;.+?$)
  • 替换为:$1$2
  • 检查环绕
  • 检查正则表达式
  • 取消选中. matches newline
  • 全部替换

说明:

(               # group 1
  ^             # beginning of line
  .+?           # 1 or more any character but newline, not greedy
  ;             # a semicolon
 |              # OR
  \G            # restart from last match position
)               # end group 1
(.*?)           # group 2, 0 or more any character but newline, not greedy
;?              # optional semicolon
(?=             # start lookahead, make sure we have after:
  .*?           # 0 or more any character but newline, not greedy
  ;             # a semicolon
  .+?           # 1 or more any character but newline, not greedy
  $             # end of line
)               # end of lookahead

替换:

$1          # content of group 1
$2          # content of group 2

给定示例的结果:

a;bfcse;g
g;qdqd;u
q;gsd;o
c;ddea;b
w;rzz;m

enter image description here

我将创建一个新变量,并添加所有不是分号的内容,如下所示:

nosemicolons = ('')
somesemicolons = ('''     -
a;|bfcse|;g
g;|qdq;d|;u
q;|g;sd;|;o
c;|dd;ea|;b
w;|;rz;z|;m
     -''')
while len(somesemicolons) != 0:
    if somesemicolons[:1] != ';':
        nosemicolons = nosemicolons + somesemicolons[:1]
        somesemicolons = somesemicolons[1:]
    else:
        somesemicolons = somesemicolons[1:] #placeholder
print(nosemicolons)

输出:

     -
a|bfcse|g
g|qdqd|u
q|gsd|o
c|ddea|b
w|rzz|m
     -

可悲的是,它看起来不像一张桌子。您可以更改它,以便它向输出中添加“”而不是什么都没有(请参见占位符)。希望有帮助。你知道吗

相关问题 更多 >