How do I remove all the Chinese characters from a string?

人走茶凉 提交于 2021-02-09 10:56:29

问题


I am trying to remove all the Chinese characters from the following string:

x <- "2.87Y 1282501 12电网MTN4 AAA 4.40 /4.30* 2000、" 

How can I do this?


回答1:


I went Googling around and found a page about Unicode character ranges. After looking through some of the CJK (Chinese, Japanese, Korean) Unicode ranges, I came to the conclusion that you need to remove the following Unicode ranges if all your strings are similar to this particular string.

  • 4E00-9FFF for CJK Unified Ideographs
  • 3000-303F for CJK Symbols and Punctuation

Using gsub(), we can do

gsub("[\U4E00-\U9FFF\U3000-\U303F]", "", x)
# [1] "2.87Y 1282501 12MTN4 AAA 4.40 /4.30* 2000"

Data:

x <- "2.87Y 1282501 12电网MTN4 AAA 4.40 /4.30* 2000、" 



回答2:


You can also do this using iconv. This will remove all Non-ASCII characters including your Chinese, Japanese, Korean etc.

iconv(x, "latin1", "ASCII", sub="")
#[1] "2.87Y 1282501 12MTN4 AAA 4.40 /4.30* 2000"



回答3:


Chinese characters' unicode range is \u4E00-\u9FA5

First use re.findall(u'[^\u4E00-\u9FA5]', string) to get the list of non-chinese characters in the string, then scan the string and remove all the characters that not in that list.

Try this:

import re
def strip_chinese(string):
    en_list = re.findall(u'[^\u4E00-\u9FA5]', string)
    for c in string:
        if c not in en_list:
            string = string.replace(c, '')
    return string



回答4:


This can be done using unicode blocks and stringr package. This answer gives the unicode blocks, there's more than one.

> str_replace_all("先秦兩漢", "[\u2E80-\u2FD5\u3190-\u319f\u3400-\u4DBF\u4E00-\u9FCC\uF900-\uFAAD]", "")
[1] ""


来源:https://stackoverflow.com/questions/47068770/how-do-i-remove-all-the-chinese-characters-from-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!