Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为什么可以';我不能更换新的线路分离器吗?_Python 3.x_Replace_Utf 8_Newline_Telegram - Fatal编程技术网

Python 3.x 为什么可以';我不能更换新的线路分离器吗?

Python 3.x 为什么可以';我不能更换新的线路分离器吗?,python-3.x,replace,utf-8,newline,telegram,Python 3.x,Replace,Utf 8,Newline,Telegram,我正在开发Python电报客户端,它将消息从应用程序发送到我们的API,我想排除一些单词。在这种情况下,应删除一些@login和#标记: 这是我的密码: for w in app.config['EXCLUDED_WORDS']: if w in data: data = data.replace(w, '') 很简单,对吧?以及我得到的结果(许多新的产品线): 我尝试过非常不同的NL分隔符,例如#YoCrypto\n#YoCrypto\r#YoCrypto\r\n

我正在开发Python电报客户端,它将消息从应用程序发送到我们的API,我想排除一些单词。在这种情况下,应删除一些@login和#标记:

这是我的密码:

for w in app.config['EXCLUDED_WORDS']:
    if w in data:
        data = data.replace(w, '')
很简单,对吧?以及我得到的结果(许多新的产品线):

我尝试过非常不同的NL分隔符,例如
#YoCrypto\n#YoCrypto\r#YoCrypto\r\n
,但它不起作用。下面是我的
打印(data.encode('utf-8'))
输出:

#TAG\n#YoCrypto\xd0\xa0laced \xd0\xb0dditional signal for Bitmex. I will remember to include both exchanges on the same signal for btcusd now on. My apologies for inconvenience.\xef\xbb\xbf@grandcchat\n@grandcsign\n@grandcmargin
我做错了什么

UPD 01.01.2020 有一些被排除在外的词:
['@grandcmargin\n'、'@grandcsign\n'、'@grandcchat\n'、'#YoCrypto\n'、''ПŠŠŠŠŠŠСŠŠŠŠŠСŠŠСŠС

我们应该在替换区域的开始和结束时留下一个中断,因此预期输出应该如下所示:

#TAG\n\nPlaced additional signal for Bitmex. I will remember to include both exchanges on the same signal for btcusd now on. My apologies for inconvenience.\n\n[Picture from message]

一种可能的解决方案是使用
re
模块,用空字符串替换单词和任何额外的新行字符。例如:

import re

data = b'''#TAG\n#YoCrypto\xd0\xa0laced \xd0\xb0dditional signal for Bitmex. I will remember to include both exchanges on the same signal for btcusd now on. My apologies for inconvenience.\xef\xbb\xbf@grandcchat\n@grandcsign\n@grandcmargin'''

words_to_remove = {'@grandcmargin', '@grandcsign', '@grandcchat', '#YoCrypto', 'По всем вопросам (For all questions, please contact): @NickolchenkoGCS'}

# decode the data (if not decoded already)
data = data.decode('utf-8')

# replace the words plus any aditional new-line character afterwards:
data = re.sub('|'.join(r'(?:[\ufeff]*{}\n*)'.format(re.escape(w)) for w in words_to_remove) , '\n', data)
data = re.sub(r'\n{3,}', r'\n\n', data) # remove excessive new-lines

print(data)
印刷品:

#TAG

Рlaced аdditional signal for Bitmex. I will remember to include both exchanges on the same signal for btcusd now on. My apologies for inconvenience.

一种可能的解决方案是使用
re
模块,用空字符串替换单词和任何额外的新行字符。例如:

import re

data = b'''#TAG\n#YoCrypto\xd0\xa0laced \xd0\xb0dditional signal for Bitmex. I will remember to include both exchanges on the same signal for btcusd now on. My apologies for inconvenience.\xef\xbb\xbf@grandcchat\n@grandcsign\n@grandcmargin'''

words_to_remove = {'@grandcmargin', '@grandcsign', '@grandcchat', '#YoCrypto', 'По всем вопросам (For all questions, please contact): @NickolchenkoGCS'}

# decode the data (if not decoded already)
data = data.decode('utf-8')

# replace the words plus any aditional new-line character afterwards:
data = re.sub('|'.join(r'(?:[\ufeff]*{}\n*)'.format(re.escape(w)) for w in words_to_remove) , '\n', data)
data = re.sub(r'\n{3,}', r'\n\n', data) # remove excessive new-lines

print(data)
印刷品:

#TAG

Рlaced аdditional signal for Bitmex. I will remember to include both exchanges on the same signal for btcusd now on. My apologies for inconvenience.

“…WAIDW代表什么?@ZF007,我做错了什么?”。。我想这是20岁以下的应用程序行话;-)。。WAIDW代表什么?@ZF007,我做错了什么。。我想这是<20岁的应用程序行话;-)这并不是那么简单:我们需要删除一些空格和NL,但保留所有rest@marperia你能编辑你的问题并把一些输入和期望的输出放在那里吗?够了吗?我添加了一些内容,但并不是那么简单:我们需要删除一些空格和NLs,但保留所有rest@marperia你能编辑你的问题并把一些输入和期望的输出放在那里吗?够了吗?我加了一些