相关疑难解决方法(0)

删除Python unicode字符串中重音的最佳方法是什么?

我在Python中有一个Unicode字符串,我想删除所有的重音符号(变音符号).

我在Web上发现了一种在Java中执行此操作的优雅方法:

  1. 将Unicode字符串转换为长标准化形式(字母和变音符号使用单独的字符)
  2. 删除Unicode类型为"变音符号"的所有字符.

我是否需要安装pyICU等库?或者只使用python标准库?那python 3怎么样?

重要说明:我想避免代码使用重音字符到非重音符号的显式映射.

python unicode diacritics python-2.x python-3.x

462
推荐指数
12
解决办法
23万
查看次数

如何删除非ASCII字符但使用Python留下句点和空格?

我正在使用.txt文件.我想要一个文件的字符串,没有非ASCII字符.但是,我想留下空格和句号.目前,我也正在剥离它们.这是代码:

def onlyascii(char):
    if ord(char) < 48 or ord(char) > 127: return ''
    else: return char

def get_my_string(file_path):
    f=open(file_path,'r')
    data=f.read()
    f.close()
    filtered_data=filter(onlyascii, data)
    filtered_data = filtered_data.lower()
    return filtered_data
Run Code Online (Sandbox Code Playgroud)

我应该如何修改onlyascii()以留出空格和句点?我想这不是太复杂但我无法弄明白.

python unicode text ascii filter

86
推荐指数
5
解决办法
16万
查看次数

如何在python 3中检查字符串是否为100%ASCII

我有两个弦

eng = "Clash of Clans – Android Apps on Google Play"
rus = "Castle Clash: ????? ??? - Android Apps on Google Play"
Run Code Online (Sandbox Code Playgroud)

现在我想通过使用来检查字符串是否为英文Python 3

我已经在这里阅读了Stackoverflow的答案,它对Python 2.x解决方案没有帮助,但是在评论中有人提到了使用

string.encode('ascii')
Run Code Online (Sandbox Code Playgroud)

使它起作用,Python 3.x但我的问题是,在两种情况下,它都会引发相同的UnicodeEncodeError异常!

屏幕截图: 在此处输入图片说明

所以现在我被困在这里,无法弄清楚如何使其工作!请指导我,否则我必须使用另一种方法来确定是否StringEnglish!谢谢

python string ascii encode python-3.x

3
推荐指数
1
解决办法
3095
查看次数

标签 统计

python ×3

ascii ×2

python-3.x ×2

unicode ×2

diacritics ×1

encode ×1

filter ×1

python-2.x ×1

string ×1

text ×1