阅读包含twitter emojis描述的文件

时间:2017-09-14 08:50:41

标签: twitter julia

我试图读取每行中包含推文的文件,并将推文的每个字符转换为整数。该文件可以找到here

但是,该文件中第28个line出现了错误。当我查看文件时,我看到该行如下:

Wish she could have told me herself. @NicoleScherzy #nicolescherzinger
#OneLove #myfav #MyQueen :heavy_black_heart:️:heavy_black_heart:️ 

此外,在阅读文件时,我在阅读时打印出每一行,在这种情况下,该行打印为(忽略前两段以简化):

Wish she could have told me herself. @NicoleScherzy #nicolescherzinger #OneLove #myfav #MyQueen :heavy_black_heart:️:heavy_black_heart:️ 

现在,如果我想逐个字符地打印它们,我收到了一个错误。这是我的代码和错误:

x=" Wish she could have told me herself. @NicoleScherzy #nicolescherzinger #OneLove #myfav #MyQueen :heavy_black_heart:️:heavy_black_heart:️"

for i=1:length(x)
  println(x[i])
end

.
.
.
INFO: #
INFO: m
INFO: y
INFO: f
INFO: a
INFO: v
INFO:  
INFO: #
INFO: M
INFO: y
INFO: Q
INFO: u
INFO: e
INFO: e
INFO: n
INFO:  
INFO: :
INFO: h
INFO: e
INFO: a
INFO: v
INFO: y
INFO: _
INFO: b
INFO: l
INFO: a
INFO: c
INFO: k
INFO: _
INFO: h
INFO: e
INFO: a
INFO: r
INFO: t
INFO: :
INFO: ️
ERROR: UnicodeError: invalid character index
 in slow_utf8_next(::Array{UInt8,1}, ::UInt8, ::Int64) at ./strings/string.jl:67
 in next at ./strings/string.jl:96 [inlined]
 in getindex(::String, ::Int64) at ./strings/basic.jl:70
 in macro expansion; at ./REPL[2]:1 [inlined]
 in anonymous at ./<missing>:?

到底是什么?为什么 h 表示为 h,顶部有一个条,而且在错误消息之前有一个空格,应该是另一个:

1 个答案:

答案 0 :(得分:3)

字符串和Unicode在任何地方都很复杂(因为人类语言很复杂)和Julia。此外,实施将来(并且应该)可能在未来发生变化。从v0.5 / v0.6开始,在问题中编写循环的方法是

for c in x
    println(c)
end

使用索引,例如:

i = 1
while i<=endof(x)
    println(x[i])
    i = nextind(x,i)
end

一般情况下,您应该熟悉endofnextind,以便在v0.5 / v0.6中在Julia中编写正确的字符串操作。 REPL帮助和documentation应该涵盖它们。

相关问题