【问题标题】:Removing strings with regex producing special character: â使用产生特殊字符的正则表达式删除字符串: â
【发布时间】:2015-11-06 16:42:51
【问题描述】:

短版:

我有很多 .txt 文件,其中包含一些不需要的字符 ,并且在使用正则表达式删除 URL 和 空格 后到处都是。我需要从所有文件中删除所有这些。

这些a在清理文件之前不存在,它们是清理后产生的。

加长版

我找到了一个适用于我的文本的正则表达式,并且正在删除 URL。 首先,我的清洁过程(注释掉的行是我尝试过的其他事情):

clean_file <-  sapply(curr_file, function(x) {
    gsub("&amp;", "&", x) %>%
        gsub("http\\S+\\s*", "", .) %>%
        gsub("[^[:alpha:][:space:]&']", "", .) %>%
        #gsub("[^[:alnum:][:space:]\\'-]", "", .) %>%
        stripWhitespace() %>%
        gsub("^ ", "", .) %>%
        gsub(" $", "", .)
        #gsub("â", "", .)
})

输入文本示例(每行是一个字符串):

Gluskin’s Rosenberg: Don’t Bet on a Bear Market for Treasurys -  Rising Treasury yields?... http://j.mp/UVM31t   #FederalReserve
Jacquiline Chabolla liked Capital Preservation In a Secular Bear Market: Large investment asset losses can be… http://goo.gl/fb/cgzGv 
Thank You http://pages.townhall.com/campaign/will-2013-be-a-bull-or-bear-market …  via @townhallcom
Calif. GHG cap-and-trade: a bull or a bear market? http://bit.ly/VG9DTr 

很遗憾这里没有出现,不过上面的文字中也有一些不规范的字符,即\302R 可以看到它们:

> x = _                                   <-- appears as an underscore in my text editor
Error: object '\302' not found

它们可能来自shift+spaceas hinted here,但它们是我的数据的人工制品,因此我需要删除它们 - 我无法阻止它们。

产生的输出(在保存的.txt 文件中可见):

Gluskinâs Rosenberg Donât Bet on a Bear Market for Treasurys - Rising Treasury yields FederalReserve
Jacquiline Chabolla liked Capital Preservation In a Secular Bear Market Large investment asset losses can beâ
Thank You â via townhallcom
Calif GHG cap-and-trade a bull or a bear market

在 R 控制台中可见的输出:

> head(clean_file)
      ..text                                                                                                        
[1,] "Nice bear market rally for the Lakers NBA"                                                                    
[2,] "Commented on StockTwits your scenario is entirely possible and as long as SPX doesn't exceed the bear market" 
[3,] "Gluskin\342s Rosenberg Don\342t Bet on a Bear Market for Treasurys Rising Treasury yields FederalReserve"           
[4,] "Jacquiline Chabolla liked Capital Preservation In a Secular Bear Market Large investment asset losses can be\342"
[5,] "Thank You \342 via townhallcom"
[6,] "Calif GHG capandtrade a bull or a bear market"

在我认为这是一个编码问题之前,简单地替换 â 字符失败了:

gsub("â", "", myText)

我尝试了一些其他解决方案来更改文件的编码(在solutions here 中找到) 我试图写入文件,强制使用fileEncoding = 'ascii' 而不是默认的utf-8(我相信)对输出进行编码,但ascii 只是给了我警告并截断了许多行,留下一些完全空的。那些被删除的行与之前出现的 a 字符的位置之间似乎也没有任何关联。

以后写的时候能不能尽量避免创建这些字符?

【问题讨论】:

    标签: r


    【解决方案1】:

    这仅保留从十六进制 0 到十六进制 7f 的字符,其中 Lines 是一个字符向量,其组件是文件的行:

    gsub("[^\\x{00}-\\x{7f}]", "", Lines, perl = TRUE)
    

    【讨论】:

    • 如果有人想知道文件中还剩下哪些字符,请参阅this link。我非常感谢你,G。格洛腾迪克 :-)
    • 这里可能是a better link来显示前31个ascii字符的含义,即十六进制{0-1F}
    猜你喜欢
    • 2016-05-26
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多