如何在Julia中替换字符串中不支持的文字?

huangapple go评论59阅读模式
英文:

how to replace not supported literals inside string In Julia?

问题

我有一个包含字符串的向量。它们是名字,但其中有一些带重音的西班牙名字。

我得到了类似于 Benda?a(指的是Bendaña)的东西。

我尝试使用大写函数,但它无法处理文字字符(InvalidCharError{Char}('ñ'))。

我尝试使用 replace,但没有成功。

英文:

I have a vector with strings. They are names but there some spanish names who has accent.

I got something like Benda?a ( refering to Bendaña).

I am trying to do use the uppercase function but it can't deal with literals (InvalidCharError{Char}('\xf1')

I have tried to use replace but with no success.

答案1

得分: 1

Julia的字符串采用UTF-8编码。如果您有一个使用其他编码的向量或字符串,您应该使用transcode将其转换为UTF-8。

英文:

Julia Strings are UTF-8 encoded. If you have a Vector or String in another encoding, you should transcode it to convert it to UTF-8.

答案2

得分: 0

> 我得到了类似于 Benda?a 的东西(指的是 Bendaña)。

这可能意味着您有一个非Unicode字符串,例如使用 Windows-1252 编码。(\xf1ñ 的 Latin1 编码。)

(这与transcode以及上面提到的UTF-8与其他Unicode编码(如UTF-16)无关...您的数据从一开始就不是Unicode存在更基本的问题。)

您可以使用 StringEncodings.jl 包 来将非Unicode字符串转换为Unicode,例如:

julia> s = decode([0x42, 0x65, 0x6e, 0x64, 0x61, 0xf1, 0x61], "Windows-1252")
"Bendaña"

julia> uppercase(s)
"BENDAÑA"

(更好的做法是——现在已经是2023年了,您真的应该找一个可以输入Unicode字符串的编辑器。某些Windows编辑器可能需要配置才能使用UTF-8。)

英文:

> I got something like Benda?a ( refering to Bendaña).

Probably this means that you have a non-Unicode string, e.g. in the Windows-1252 encoding. (\xf1 is the Latin1 encoding of ñ.)

(This is separate from transcode and UTF-8 vs. other Unicode encodings like UTF-16 mentioned above … you have a more fundamental problem in that your data is not Unicode to begin with.)

You can use the StringEncodings.jl package to convert non-Unicode strings to/from Unicode. For example:

julia> s = decode([0x42, 0x65, 0x6e, 0x64, 0x61, 0xf1, 0x61], "Windows-1252")
"Bendaña"

julia> uppercase(s)
"BENDAÑA"

(Better yet — it's 2023, you should really find an editor that lets you enter your strings in Unicode. Some Windows editors require a configuration to use UTF-8.)

huangapple
  • 本文由 发表于 2023年6月16日 06:14:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定