为什么我的代码会过滤掉我不要求它过滤的字符?(Why does my code filter out the characters I'm not asking it to filter?

编程入门 行业动态 更新时间:2024-10-25 15:31:49
为什么我的代码会过滤掉我不要求它过滤的字符?(Why does my code filter out the characters I'm not asking it to filter? (Python))

我只是要求我的代码过滤掉' - '和'+',但它也过滤了'//'它不应该过滤掉。

任何人都知道它为什么这样做?

with open("some_file_1.txt") as f, open("some_file_2.txt") as g: flines = f.readlines() glines = g.readlines() d = difflib.Differ() diff = d.compare(flines, glines) with open('merged_tp.txt', 'w') as file_out: for line in diff: print line new_line = re.sub('[+-?]', '', line) file_out.write(new_line)

I've only asked my code to filter out '-' and '+', but it also filters out '//' which it should not.

Anyone have any idea why it does this?

with open("some_file_1.txt") as f, open("some_file_2.txt") as g: flines = f.readlines() glines = g.readlines() d = difflib.Differ() diff = d.compare(flines, glines) with open('merged_tp.txt', 'w') as file_out: for line in diff: print line new_line = re.sub('[+-?]', '', line) file_out.write(new_line)

最满意答案

字符集[+-?]表示“ +和?之间的任何字符”。

+有整数序数43. ? 具有整数序数63.范围43-63包括以下字符: +,-./0123456789:;<=>? 。/ +,-./0123456789:;<=>? <=> +,-./0123456789:;<=>?

字符集[+-]表示“要么+或- ”,我认为这就是你想要的。

The character set [+-?] means "any character between + and ?".

+ has the integer ordinal 43. ? has the integer ordinal 63. The range 43–63 includes the following characters: +,-./0123456789:;<=>?

The character set [+-] means "either + or -", which I think is what you want.

更多推荐

本文发布于:2023-07-30 07:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我不   字符   过滤掉   代码   characters

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!