我有这个代码,我使用一对列表来获取一个列表,其中一个列表是键,另一个列表是值。非常简单的代码:
defp create_data_map(columns, row) do
Enum.zip(columns, row)
|> Enum.into(%{}, fn {k, v} ->
{String.to_atom(k), v}
end)
end
我从csv文件中获取这些列表,其中列是第一行,即标题列表,第二行是之后的任何一行。这是标题的样子:
["action", "source_application", "partner_name", "detail", "college_name",
"ipeds_id", "deleted", "deleted_at", "athlete_id", "athlete_email",
"athlete_first_name", "athlete_last_name", "athlete_sport_id",
"athlete_sport_name", "pass_uuid", "coach_id", "coach_id",
"coach_email",
"coach_first_name", "coach_last_name", "coach_position", "coach_sport_id",
"coach_sport_name", "occurred_at"]
出于某种原因,调用String.to_atom / 1后的'action'键最终为:"action"
。没有其他键具有该问题。它们都是正确形成的原子。
除了它在列表的开头之外,我没有看到来自其他键的操作键有什么不同。
答案 0 :(得分:1)
使用Atom
String.to_atom()
有三种不同的显示方式
以上所有3都是原子。它们的唯一区别在于字符串的格式化方式。
iex> "Atom-" |> String.to_atom
:"Atom-"
然后类似于在elixir中巧妙地格式化functions
名称的方式。即字符串以字母开头,剩下的字符串只能包含字母,数字或三个特殊字符_!@。
iex> "Atom" |> String.to_atom
:Atom
再次以与Modules
相同的方式在Elixir中巧妙地命名。从内部来看,模块名称也只是原子。字符串应以Elixir.
开头,后跟大写字母,并且只能包含字母。
iex> "Elixir.Atom" |> String.to_atom
Atom
答案 1 :(得分:0)
When inspect/1
renders your atom as :"hello world"
, the name of the atom contains characters like whitespace, dots or uppercase letters.
See how you would not be able to reference the above atom as :hello world
, because it would interpret world
as a variable.
They are still valid atoms, though it might be harder to use them in your code.