在某个系统中,我需要生成只允许使用纯 ASCII 字符(az、0-9、破折号)的用户名。然而,许多用户的名字并不简单地符合这些限制,例如德国名字“Müller”或“Röthlin”。
现在这些元音变音有另一种输入方式(我确定它有一个名字,但我不知道 - 可能有助于谷歌搜索)
一种天真的方法是使用转换表:
name = name.replace('Ä', 'Ae')
name = name.replace('ä', 'ae')
name = name.replace('ö', 'oe')
Run Code Online (Sandbox Code Playgroud)
等等。
但是,一旦您的用户来自除德语之外的文化,可能会出现其他字符,这种方法就会失败。所以我正在寻找一种通用的方法来“转换”尽可能多的非 ascii 字符,然后再回退到简单地将它们去除。
我将一组句子的录音保存到相应的一组音频文件中.
句子包括:
Ich weiß es nicht!
¡No lo sé!
Ég veit ekki!
Run Code Online (Sandbox Code Playgroud)
您如何建议我将句子转换为人类可读的文件名,稍后将在在线服务器上提供.我现在不确定将来可能会处理哪些语言.
更新:
请注意,两句话不能相互冲突.例如:
É bär icke dej.
E bår icke dej.
Run Code Online (Sandbox Code Playgroud)
无法解析为相同的文件名,因为这些将相互覆盖.这是这里提到的slugify函数的问题:将字符串转换为有效的文件名?
我想出的最好的是使用urllib.parse.quote.但是我认为最终的输出比我希望的更难阅读.有什么建议?:
Ich%20wei%C3%9F%20es%20nicht%21
%C2%A1No%20lo%20s%C3%A9%21
%C3%89g%20veit%20ekki%21
Run Code Online (Sandbox Code Playgroud) 我有一个dataSwiss包含瑞士城市信息的数据框。我想用普通字母用重音符号替换字母。
这就是我正在做的:
dataSwiss['Municipality'] = dataSwiss['Municipality'].str.encode('utf-8')
dataSwiss['Municipality'] = dataSwiss['Municipality'].str.replace(u"é", "e")
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
----> 2 dataSwiss['Municipality'] = dataSwiss['Municipality'].str.replace(u"é", "e")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
数据看起来像:
dataSwiss.Municipality
0 Zürich
1 Zürich
2 Zürich
3 Zürich
4 Zürich
5 Zürich
6 Zürich
7 Zürich
Run Code Online (Sandbox Code Playgroud)
我找到了解决方案
s = dataSwiss['Municipality']
res = s.str.decode('utf-8')
res = res.str.replace(u"é", "e")
Run Code Online (Sandbox Code Playgroud) 我需要用文本文件中的重音替换所有字符,即:
á é í ó ú ñ
Run Code Online (Sandbox Code Playgroud)
因为他们的非重音等价物:
a e i o u n
Run Code Online (Sandbox Code Playgroud)
这可以通过一次性的整个文件的regex命令来实现吗?
更新(2017年2月1日)
我带着极大的答案由基思·霍尔,变成了一个崇高的包.你可以在这里找到它:删除非Ascii Chars.
如果你在python(2.x)中循环使用unicode字符串,请说:
ak.sɛp.tɑ
你怎么知道当前的char是否是一个组合变音符号?
例如,上面字符串中的最后一个字符实际上是一个组合标记:
ak.sɛp.tɑ - >
我正在编写一个程序,要求用户输入包含重音的输入.测试用户输入字符串以查看它是否与程序中声明的字符串匹配.如下所示,我的代码不起作用:
# -*- coding: utf-8 -*-
testList = ['má']
myInput = raw_input('enter something here: ')
print myInput, repr(myInput)
print testList[0], repr(testList[0])
print myInput in testList
Run Code Online (Sandbox Code Playgroud)
enter something here: má
m?° 'm\xe2\x88\x9a\xc2\xb0'
má 'm\xc3\xa1'
False
Run Code Online (Sandbox Code Playgroud)
enter something here: má
má u'm\xe1'
má 'm\xc3\xa1'
Warning (from warnings module):
File "/Users/ryanculkin/Desktop/delete.py", line 8
print myInput in testList
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
False
Run Code Online (Sandbox Code Playgroud)
在比较两个字符串时,如何让我的代码打印出True?
另外,我注意到在同一输入上运行此代码的结果是不同的,这取决于我是使用eclipse还是IDLE.为什么是这样?我的最终目标是将我的程序放在网上; 有什么我需要注意的,因为结果似乎是如此不稳定吗?
我正在从Python(Django)发送电子邮件.电子邮件主机是'smtp.gmail.com'.当我使用localhost时,我可以在电子邮件主题中使用特殊字符.但是,现在我正在尝试从服务器(webfaction)我得到一个错误"UnicodeDecodeError:'ascii'编解码器无法解码字节..."在电子邮件模板中我使用十六进制代码,但它们不适用于主题(他们没有翻译).该怎么办?
# coding=UTF-8
...
subject = "æøå"
c = {}
t_html = loader.get_template(template_html)
t_text = loader.get_template(template_txt)
e = EmailMultiAlternatives(subject, t_text.render(Context(c)), from_email, [to_email])
e.attach_alternative(t_html.render(Context(c)), "text/html")
e.send()
Run Code Online (Sandbox Code Playgroud) 我必须将所有拉丁字符转换为相应的英文字母.我可以使用Python来做吗?或者是否有可用的映射?
Unicode值为非unicode字符
Ramírez Sánchez应转换为Ramirez Sanchez.
我需要在Python中使用BeautifulSoup查找HTML文件中段落元素内的所有可见标记.
例如,
<p>Many hundreds of named mango <a href="/wiki/Cultivar" title="Cultivar">cultivars</a> exist.</p>
应该返回:
Many hundreds of cultivars exist.
PS某些文件包含需要提取的Unicode字符(印地语).
任何想法如何做到这一点?
在python中是否有一些考虑到重音的编辑距离.例如,举行以下财产
d('ab', 'ac') > d('àb', 'ab') > 0
Run Code Online (Sandbox Code Playgroud) d = {
"key": "Impress the playing crowd with these classic "
"Playing Cards \u00a9 Personalized Coasters.These beautiful"
" coasters are made from glass, and measure approximately 4\u201d x 4\u201d (inches)"
".Great to look at, and lovely to the touch.There are 4 coasters in a set.We have "
"created this exclusive design for all card lovers.Each coaster is a different suit, "
"with the underneath.Make your next Bridge, or Teen Patti session uber-personal!"
"Will look great on the bar, or …Run Code Online (Sandbox Code Playgroud) 我正在使用Django开发一个简单的网页,我需要实现搜索功能.我目前正在使用这样的东西:
search_box = request.GET['search_box']
X = Foo.objects.filter(Q(title__contains=search_box) | Q(info__contains=search_box)).values()
Run Code Online (Sandbox Code Playgroud)
如果指定的列包含搜索的字符串,它会检查我的数据库,但如果我搜索"kočík"但我的数据库包含"kocik",该怎么办?我如何在Python 3中从字符串中删除diacritis,或者实现它的最佳方法是什么?谢谢
我有个字符的几个文本文件,其中有区别标记,例如è,á,ô等等。我想将这些字符替换e,a,o,等
我怎样才能在 Python 中实现这一点?感谢帮助!
python ×12
diacritics ×4
unicode ×4
python-3.x ×3
string ×2
decode ×1
dictionary ×1
django ×1
email ×1
encoding ×1
filenames ×1
html-parsing ×1
nlp ×1
pandas ×1
raw-input ×1
regex ×1
sanitize ×1
sublimetext3 ×1
text ×1