输出的表情符号在Windows和Linux上是不同的

2024-09-29 19:19:12 发布

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

在Windows上:

import re
import subprocess

output = subprocess.run(['python', '-m', 'black'], stderr=subprocess.PIPE)
stderr = output.stderr.decode()
assert re.match(r'No Path provided. Nothing to do \\U0001f634\r\n', stderr)

但是,在Linux上,我需要在两个地方更改正则表达式:

import re
import subprocess

output = subprocess.run(['python', '-m', 'black'], stderr=subprocess.PIPE)
stderr = output.stderr.decode()
assert re.match(r'No Path provided. Nothing to do \U0001f634\n', stderr)

我理解为什么我需要将\r\n更改为\n——这是因为carriage return

但是,为什么我需要将'\\U0001f634'更改为'\U0001f634'?有没有一种跨平台编写上述断言的方法


Tags: pathnorunimportreoutputmatchstderr
1条回答
网友
1楼 · 发布于 2024-09-29 19:19:12

这里出现差异的原因可能是因为表情符号在Windows中显示为十六进制值,在Linux中显示为文字表情符号

这里的解决方案是通过在\r之后添加?量词,将\r作为可选字符进行匹配,并使用替换来匹配转义表情符号或文字表情符号:

r'No Path provided. Nothing to do (?:\\U0001f634|\U0001f634)\r?\n'

如果点是文字点,请考虑转义,否则,^ {CD3>}匹配任何字符,但行断线:

r'No Path provided\. Nothing to do (?:\\U0001f634|\U0001f634)\r?\n'

详细信息

  • No Path provided. Nothing to do -literalNo Path provided. Nothing to do 文本
  • (?:\\U0001f634|\U0001f634)-要么是一个\后跟U0001f634要么是一个😀表情符号
  • \r?-可选CR
  • \n-一个LF字符

相关问题 更多 >

    热门问题