0

好吧,我正在尝试清理包含法语口音代码的文件:

#353= IFCPROPERTYSINGLEVALUE('Charge d''\X2\00E9\X0\clairage sp\X2\00E9\X0\cifi\X2\00E9\X0\e par surface',$,IFCREAL(10.7639104167097),$);

我创建了这个小功能:

def CleanSpace(sp):
    sp.replace("\X2\00F4\X0\","ô")
    sp.replace("\X2\00E9\X0\","é")
    return(sp)

但是 Python 3 给了我错误:

    sp.replace("\X2\00F4\X0\","ô")
                               ^
SyntaxError: invalid syntax

请问我该如何解决?提前致谢

编辑:如果有帮助,我宁愿在控制台中尝试这一行,但答案很奇怪:

$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a='02_RADIOTHERAPIE/ ARC -plateforme recherche- Radioth\X2\00E9\X0\rapie'
>>> a
'02_RADIOTHERAPIE/ ARC -plateforme recherche- Radioth\\X2\x00E9\\X0\rapie'
>>> a.replace('\X2\00E9\X0\\','é')
'02_RADIOTHERAPIE/ ARC -plateforme recherche- Radioth\\X2\x00E9\\X0\rapie'
4

3 回答 3

1

\ 字符会转义您的引号。这意味着 python 将继续运行,直到找到另一个引号来结束您的字符串。所以,实际上,你的字符串是\X2\00F4\X0\",为了解决这个问题,用 \ 转义 \ 或完全删除最后一个。新代码:

sp.replace("\X2\00F4\X0\\","ô")
于 2018-04-10T14:25:33.427 回答
0
于 2018-04-10T14:29:20.127 回答
0

Well, after a lot of tries and searches, solution for one line was to use raw-strings:

>>> a.replace(r'\X2\00E9\X0\ '[:-1], 'é')
"#353= IFCPROPERTYSINGLEVALUE('Charge d''éclairage spécifiée par surface',$,IFCREAL(10.7639104167097),$);"

For more lines, it was more difficult because bytes into my file are already written and it is not because I see a '\' that it is existing... Solution found for me was to work on bytes with antlr4

于 2018-04-16T12:59:14.923 回答