我从plug
`conn.req_headers中获得了以下信息:
[
{"accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"},
{"accept-encoding", "gzip, deflate, br"},
{"accept-language", "en-GB,en;q=0.9,fr;q=0.8"},
{"cache-control", "max-age=0"},
{"connection", "keep-alive"},
{"cookie",
"_xsrf=2|353a732a|e6f0e245b4f4617b78f5080f716b5a83|1548858283; username-localhost-8888=\"2|1:0|10:1548926887|23:username-localhost-8888|44:MjlmNjg1MmMwMTA5NGZlNjllM2FmNzg0YmFlMDM4ZTY=|b5777f2478a76e40e5d2a194c35a6dc49771b5522513299099e9baf47d7c0975\"; _shortr_key=SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYcnEvMWJQRE1OSVROSG1JOXRDY1VrUT09.SAlNvN6U9ol3ldsUP2qjlVXTJgvtLdSUzjBkAuuW5NM"},
{"host", "localhost:4000"},
{"referer", "http://localhost:4000/links"},
{"upgrade-insecure-requests", "1"},
{"user-agent",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36"}
]
我需要将其转换为Map(以发送给ecto),这样%{“ accept” =>“ ...”,...}
在python中,我会做类似的事情
{v[0]: v[1] for v in original_list}
灵丹妙药中有那么美的东西吗?
答案 0 :(得分:3)
使用Enum#into/2:
将给定的可枚举值插入可收集对象。
例如(有关更多示例,请参阅文档):
iex> [{"foo", "bar"}, {"fizz"}, {"buzz"}] |> Enum.into(%{})
%{"foo" => "bar", "fizz" => "buzz"}
在您的情况下:
iex(1)> [
...(1)> {"accept",
...(1)> "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"},
...(1)> {"accept-encoding", "gzip, deflate, br"},
...(1)> {"accept-language", "en-GB,en;q=0.9,fr;q=0.8"},
...(1)> {"cache-control", "max-age=0"},
...(1)> {"connection", "keep-alive"},
...(1)> {"cookie",
...(1)> "_xsrf=2|353a732a|e6f0e245b4f4617b78f5080f716b5a83|1548858283; username-localhost-8888=\"2|1:0|10:1548926887|23:username-localhost-8888|44:MjlmNjg1MmMwMTA5NGZlNjllM2FmNzg0YmFlMDM4ZTY=|b5777f2478a76e40e5d2a194c35a6dc49771b5522513299099e9baf47d7c0975\"; _shortr_key=SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYcnEvMWJQRE1OSVROSG1JOXRDY1VrUT09.SAlNvN6U9ol3ldsUP2qjlVXTJgvtLdSUzjBkAuuW5NM"},
...(1)> {"host", "localhost:4000"},
...(1)> {"referer", "http://localhost:4000/links"},
...(1)> {"upgrade-insecure-requests", "1"},
...(1)> {"user-agent",
...(1)> "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36"}
...(1)> ] |> Enum.into(%{})
%{
"accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"accept-encoding" => "gzip, deflate, br",
"accept-language" => "en-GB,en;q=0.9,fr;q=0.8",
"cache-control" => "max-age=0",
"connection" => "keep-alive",
"cookie" => "_xsrf=2|353a732a|e6f0e245b4f4617b78f5080f716b5a83|1548858283; username-localhost-8888=\"2|1:0|10:1548926887|23:username-localhost-8888|44:MjlmNjg1MmMwMTA5NGZlNjllM2FmNzg0YmFlMDM4ZTY=|b5777f2478a76e40e5d2a194c35a6dc49771b5522513299099e9baf47d7c0975\"; _shortr_key=SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYcnEvMWJQRE1OSVROSG1JOXRDY1VrUT09.SAlNvN6U9ol3ldsUP2qjlVXTJgvtLdSUzjBkAuuW5NM",
"host" => "localhost:4000",
"referer" => "http://localhost:4000/links",
"upgrade-insecure-requests" => "1",
"user-agent" => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36"
}
答案 1 :(得分:3)
您也可以使用| id | user_name |
| bigint | character varying(30) |
|--------+-----------------------+
| | |
https://hexdocs.pm/elixir/Map.html#new/1
Map.new