【问题标题】:Erlang: read non English letters and print themErlang:阅读非英文字母并打印出来
【发布时间】:2020-09-25 15:34:13
【问题描述】:

我正在编写一个用 erlang 读取 csv 文件的代码。

我从csv中得到一个字符串,例如:

亚历山大·布贝克|弗洛里安·魏斯哈特|马蒂亚斯·格鲁勒|乌尔里希·赖泽

然后我使用此命令将其转换为列表并将其打印到终端:

Authors = string:tokens(element(2,Row),[$|]),
io:format("The authors in row ~p are: ~p~n", [Num,Authors])

问题出在这个名字上:Florian Weißhardt

因为它有一个非英文字母,所以输出是 [70,108,111,114,105,97,110,32,87,101,105,195,159, 104,97,114,100,116]

我该如何解决这个问题?

谢谢

【问题讨论】:

  • 试试io:format("The authors in row ~p are: ~s~n", [Num,Authors]).

标签: csv unicode erlang readfile non-english


【解决方案1】:

尝试将utf8unicode格式的列表转换为二进制:

1> 1> Authors = [70,108,111,114,105,97,110,32,87,101,105,195,159, 104,97,114,100,116].
[70,108,111,114,105,97,110,32,87,101,105,195,159,104,97,114, 100,116]
2> io:format("The authors: ~ts~n", [list_to_binary(Authors)]).
The authors: Florian Weißhardt
ok
3> list_to_binary(Authors).
<<"Florian Weißhardt"/utf8>>

【讨论】:

  • 有效!但现在它打印出来了:Alexander BubeckFlorian WeißhardtMatthias GruhlerUlrich Reiser。不同作者之间没有任何空格(每个作者都是作者列表中的一个元素)。有没有办法增加空间?甚至存在于某种数据结构中(例如元组)
  • 请提供您的完整列表示例,其中包含您尝试打印的元组和数据
  • 这是输入:Alexander Bubeck|Florian Weißhardt|Matthias Gruhler|Ulrich Reiser。 ------ 现在输出是:Alexander BubeckFlorian WeißhardtMatthias GruhlerUlrich Reiser。 ------ 我希望输出为:Alexander Bubeck Florian Weißhardt Matthias Gruhler Ulrich Reiser。或 {Alexander Bubeck, Florian Weißhardt, Matthias Gruhler, Ulrich Reiser}(不一定是元组,列表也不错)
  • 你能显示一个数据使用io吗?只需致电io:format("~p~n", [YourData]). 并将输出放在这里 - 这会很有帮助。谢谢。附言input Alexander Bubeck|Florian Weißhardt|Matthias Gruhler|Ulrich Reiser 必须有一个类型,这是一个列表还是二进制?需要分隔符|?等。从您这边提供完整的数据将有助于尝试准备工作解决方案。
  • 我显示的输出来自这一行:io:format("The authors in row ~p are: ~ts~n", [Num,list_to_binary(Authors)]) 分隔符 | 来自 csv 文件。我从一个网站下载它,它有大约 100k 行,我无法真正改变它。 input Alexander Bubeck|Florian Weißhardt|Matthias Gruhler|Ulrich Reiser 是元组中的字符串元素
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 2013-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-20
相关资源
最近更新 更多