在这篇文章中,我们将带领您了解python字符串替换为u的全貌,包括python字符串替换为小写的相关情况。同时,我们还将为您介绍有关python–如何用下一个字符串替换字符串中的字符?、python
在这篇文章中,我们将带领您了解python字符串替换为u的全貌,包括python字符串替换为小写的相关情况。同时,我们还将为您介绍有关python – 如何用下一个字符串替换字符串中的字符?、python – 错误的字符串替换版本、Python 字符串替换、python 字符串替换 不区分大小写的知识,以帮助您更好地理解这个主题。
本文目录一览:- python字符串替换为u(python字符串替换为小写)
- python – 如何用下一个字符串替换字符串中的字符?
- python – 错误的字符串替换版本
- Python 字符串替换
- python 字符串替换 不区分大小写
python字符串替换为u(python字符串替换为小写)
如何解决python字符串替换为\u?
我有一个字符串。
m = ''I have two element. <U+2F3E> and <U+2F8F>''
我想替换为:
m = ''I have two element. \u2F3E and \u2F8F'' # utf-8
我的代码:
import re
p1 = re.compile(''<U+\+'') # start "<"
p2 = re.compile(''>+'') # end ">"
m = ''I have two element. <U+2F3E> and <U+2F8F>''
out = pattern.sub(''\\u'',m) # like: ''I have two element. \u2F3E> and \u2F8F>''
但我收到此错误消息:
SyntaxError: (unicode error) ''unicodeescape'' codec can''t decode bytes in position 0-1: truncated \uXXXX escape
我该如何解决。谢谢。
解决方法
import re
m = ''I have two element. <U+2F3E> and <U+2F8F>''
print(re.sub(r''<U\+(\w+)>'',r"\\u\1",m))
# I have two element. \u2F3E and \u2F8F
,
您可以使用单个正则表达式来查找字符串并提取您要在替换中使用的部分。
您收到错误的原因是 ''\\u''
将文字字符串 \u
传递给正则表达式引擎,该引擎尝试将其解析为 Unicode 字符,但失败了; \u
需要紧跟四个十六进制数字以形成有效的 Unicode 代码点。但是你仍然在接近这个,好像你想用一个文字字符串替换,根据你的澄清评论是错误的。
import re
m = re.sub(r''<U\+([0-9a-fA-F]{4})>'',lambda x: chr(int(x.group(1),16)),m)
lambda
接收匹配对象作为其参数; x.group(1)
取出第一个带括号的组,chr(int(that,16))
产生相应的文字字符。
如果您实际上想要生成它的 UTF-8 编码,那也很容易:
>>> re.sub(r''<U\+([0-9a-fA-F]{4})>'',''I have two element. <U+2F3E> and <U+2F8F>'')
''I have two element. ⼾ and ⾏''
>>> re.sub(r''<U\+([0-9a-fA-F]{4})>'',''I have two element. <U+2F3E> and <U+2F8F>'').encode(''utf-8'')
b''I have two element. \xe2\xbc\xbe and \xe2\xbe\x8f''
如您所见,UTF-8 编码是一个字节序列,根本不对应于可打印字符。 (嗯,它们可以用其他一些编码打印;但那只是 mojibake.)
,m.replace(''<U+'',''\u'')
m.replace(''>'','' '')
,
请使用下面的代码,这可能会有所帮助
aa
aaa
aaaa
aaaaa
aaaab
aaaac
aaaad
aaaae
aaaaf
...
...
ffffa
ffffb
ffffc
ffffd
ffffe
fffff
python – 如何用下一个字符串替换字符串中的字符?
abcdefghijklmnopqrstuvwxyz
应成为:
bcdefghijklmnopqrstuvwxyza
是否可以在不使用替换功能的情况下进行26次?
解决方法
str.translate()
method让Python在一个步骤中替换其他字符.
使用string.maketrans()
function将ASCII字符映射到其目标;使用string.ascii_lowercase
可以帮助您,因为它可以节省您自己键入所有字母:
from string import ascii_lowercase try: # Python 2 from string import maketrans except ImportError: # Python 3 made maketrans a static method maketrans = str.maketrans cipher_map = maketrans(ascii_lowercase,ascii_lowercase[1:] + ascii_lowercase[:1]) encrypted = text.translate(cipher_map)
演示:
>>> from string import maketrans >>> from string import ascii_lowercase >>> cipher_map = maketrans(ascii_lowercase,ascii_lowercase[1:] + ascii_lowercase[:1]) >>> text = ''the quick brown fox jumped over the lazy dog'' >>> text.translate(cipher_map) ''uif rvjdl cspxo gpy kvnqfe pwfs uif mbaz eph''
python – 错误的字符串替换版本
> How to replace two things at once in a string? 6个
> How to replace multiple substrings of a string? 19个
我陷入了一个非常直接的问题,但经过一些调整后,我根本找不到一个简单有效的算法来做到这一点.
所以基本上我有一个字符串,其中包含某些元素,例如:
l = "test %1,%13,%14,%15"
还有一张地图:
dict = {"%1": "%33","%13": "%14","%14","%15","%15": "%17"}
我想做以下的事情:
for k in dict.keys(): l = l.replace(k,dict[k])
所以我期待的是:
l = "test %33,%15,%17"
但显然这是不可行的,因为键和值之间存在一些冲突.所以上面的代码会输出:
l = "test %33,%17,%17"
很抱歉这样的原生问题,但我该如何解决这个并得到我预期的输出?请注意,每个键/值的长度不固定,所以我不能做类似的事情:
m_idx = l_copy.find(key) l = l[:m_idx] + dict[key] + l[m_idx+len(key):]
因为长度不固定,上面的代码仍然是错误的.
我清楚这个吗?任何建议将非常感激!
=======更新
因此所有键都遵循%[0-9]的这种模式.
解决方法
import re l = "test %1,%15" _dict = {"%1": "%33","%14":"%15","%15": "%17"} new_l = re.sub('%\d+',lambda x:_dict[x.group()],l)
输出:
'test %33,%17'
如果_dict中没有键,则可以使用dict.get通过提供默认值来防止KeyError:
new_l = re.sub('%\d+',lambda x:_dict.get(x.group(),x.group()),l)
Python 字符串替换
python 字符串替换可以用2种方法实现:
1是用字符串对象本身的方法。
2用正则来替换字符串
下面用个例子来实验下:
a = ''hello word''
我把a字符串里的word替换为python
1用字符串本身的replace方法
a.replace(''word'',''python'')
输出的结果是hello python
2用正则表达式来完成替换:
import re
strinfo = re.compile(''word'')
b = strinfo.sub(''python'',a)
print b
输出的结果也是hello python
python 字符串替换 不区分大小写
目录@H_301_2@
方法一 正则表达式
方法二 字符串直接操作
方法一 正则表达式
str.replace(old,new[,max])
的替换是区分大小写的
不区分大小写替换需要正则表达式re.sub(
)带上re.IGnorECASE
选项
>>> import re
>>> reg = re.compile(re.escape('hello'),re.IGnorECASE)
>>> reg.sub('My','Hello World,HELLO PYTHON')
'My World,My PYTHON'
方法二 字符串直接操作
def replace_case(old,new,text):
index = text.lower().find(old.lower())
if index == -1:
return text
return replace_case(old,text[:index] + new + text[index + len(old):])
dest="Hello World,HELLO PYTHON"
print(dest)
print(replace_case("hello","My",dest))
输出:
Hello World,HELLO PYTHON
My World,My PYTHON
关于python字符串替换为u和python字符串替换为小写的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于python – 如何用下一个字符串替换字符串中的字符?、python – 错误的字符串替换版本、Python 字符串替换、python 字符串替换 不区分大小写的相关知识,请在本站寻找。
本文标签: