为什么我的代码会过滤掉我不要求它过滤的字符?(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.
更多推荐
发布评论