在elixir中将EXTO模式写入csv文件?
嗨,我在长生不老药里有这样的人在elixir中将EXTO模式写入csv文件?,csv,elixir,phoenix-framework,Csv,Elixir,Phoenix Framework,嗨,我在长生不老药里有这样的人 [ %Texting.Contact.Person{ __meta__: #Ecto.Schema.Metadata<:loaded, "people">, email: nil, name: "John", phone_number: "13334445555", phonebook: #Ecto.Association.NotLoaded<association :phonebook is not
[
%Texting.Contact.Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
email: nil,
name: "John",
phone_number: "13334445555",
phonebook: #Ecto.Association.NotLoaded<association :phonebook is not loaded>,
phonebook_id: 60,
previous_phonebook_id: 60,
subscribed: true,
updated_at: ~N[2018-09-22 14:36:04.788163],
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 54
},
%Texting.Contact.Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
email: nil,
name: "Rhee",
phone_number: "14443335555",
phonebook: #Ecto.Association.NotLoaded<association :phonebook is not loaded>,
phonebook_id: 60,
previous_phonebook_id: 60,
subscribed: true,
updated_at: ~N[2018-09-22 14:36:13.671479],
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 54
}
]
** (Protocol.UndefinedError) protocol String.Chars not implemented for #Function<62.51129937/2 in Stream.transform/3>. This protocol is implemented for: Atom, BitString, Date, DateTime, Decimal, Ecto.Date, Ecto.DateTime, Ecto.Time, Float, Floki.Selector, Floki.Selector.AttributeSelector, Floki.Selector.Combinator, Floki.Selector.Functional, Floki.Selector.PseudoClass, Integer, List, NaiveDateTime, Postgrex.Copy, Postgrex.Query, Postgrex.Stream, Time, URI, Version, Version.Requirement
(elixir) /home/ubuntu/bob/tmp/0a92cc555e2418d1b56e3b10e5321a85/elixir/lib/elixir/lib/string/chars.ex:3: String.Chars.impl_for!/1
(elixir) /home/ubuntu/bob/tmp/0a92cc555e2418d1b56e3b10e5321a85/elixir/lib/elixir/lib/string/chars.ex:22: String.Chars.to_string/1
(elixir) lib/io.ex:553: IO.write/2
(elixir) lib/enum.ex:1314: Enum."-map/2-lists^map/1-0-"/2
但我犯了这样的错误
[
%Texting.Contact.Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
email: nil,
name: "John",
phone_number: "13334445555",
phonebook: #Ecto.Association.NotLoaded<association :phonebook is not loaded>,
phonebook_id: 60,
previous_phonebook_id: 60,
subscribed: true,
updated_at: ~N[2018-09-22 14:36:04.788163],
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 54
},
%Texting.Contact.Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
email: nil,
name: "Rhee",
phone_number: "14443335555",
phonebook: #Ecto.Association.NotLoaded<association :phonebook is not loaded>,
phonebook_id: 60,
previous_phonebook_id: 60,
subscribed: true,
updated_at: ~N[2018-09-22 14:36:13.671479],
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 54
}
]
** (Protocol.UndefinedError) protocol String.Chars not implemented for #Function<62.51129937/2 in Stream.transform/3>. This protocol is implemented for: Atom, BitString, Date, DateTime, Decimal, Ecto.Date, Ecto.DateTime, Ecto.Time, Float, Floki.Selector, Floki.Selector.AttributeSelector, Floki.Selector.Combinator, Floki.Selector.Functional, Floki.Selector.PseudoClass, Integer, List, NaiveDateTime, Postgrex.Copy, Postgrex.Query, Postgrex.Stream, Time, URI, Version, Version.Requirement
(elixir) /home/ubuntu/bob/tmp/0a92cc555e2418d1b56e3b10e5321a85/elixir/lib/elixir/lib/string/chars.ex:3: String.Chars.impl_for!/1
(elixir) /home/ubuntu/bob/tmp/0a92cc555e2418d1b56e3b10e5321a85/elixir/lib/elixir/lib/string/chars.ex:22: String.Chars.to_string/1
(elixir) lib/io.ex:553: IO.write/2
(elixir) lib/enum.ex:1314: Enum."-map/2-lists^map/1-0-"/2
**(Protocol.UndefinedError)协议字符串。未为#函数实现字符。该协议的实现对象为:Atom、BitString、Date、DateTime、Decimal、Ecto.DateTime、Ecto.Time、Float、Floki.Selector、Floki.Selector.AttributeSelector、Floki.Selector.Combinator、Floki.Selector.Functional、Floki.Selector.PseudoClass、Integer、List、NaiveDateTime、Postgrex.Copy、Postgrex.Query、Postgrex.Stream、Time、URI、Version、,版本.要求
(elixir)/home/ubuntu/bob/tmp/0a92cc555e2418d1b56e3b10e5321a85/elixir/lib/elixir/lib/string/chars.ex:3:string.chars.impl_for/1.
(elixir)/home/ubuntu/bob/tmp/0a92cc555e2418d1b56e3b10e5321a85/elixir/lib/elixir/lib/string/chars.ex:22:string.chars.to_string/1
(elixir)lib/io.ex:553:io.write/2
(elixir)lib/enum.ex:1314:enum。“-map/2-lists^map/1-0-”/2
我想做的是只保存姓名和电话号码字段到csv文件格式。
如何执行此操作?根据,CSV.encode/2
获取字符串列表,但没有您尝试使用的选项
我是这样做的:
def write!(people) do
people
|> Stream.map(&[&1.name, &1.phone_number])
|> CSV.encode()
|> Enum.into(File.stream!("contacts.csv"))
end
Enum.map
创建一个字符串列表,然后对其进行编码并流式传输到contacts.csv
要在顶部添加标题行,可以使用Stream.concat/2
预先添加标题行:
def write!(people) do
[["name", "phone_number"]]
|> Stream.concat(people |> Stream.map(&[&1.name, &1.phone_number]))
|> CSV.encode()
|> Enum.into(File.stream!("contacts.csv"))
end
根据,CSV.encode/2
获取字符串列表,并且没有您尝试使用的选项
我是这样做的:
def write!(people) do
people
|> Stream.map(&[&1.name, &1.phone_number])
|> CSV.encode()
|> Enum.into(File.stream!("contacts.csv"))
end
Enum.map
创建一个字符串列表,然后对其进行编码并流式传输到contacts.csv
要在顶部添加标题行,可以使用Stream.concat/2
预先添加标题行:
def write!(people) do
[["name", "phone_number"]]
|> Stream.concat(people |> Stream.map(&[&1.name, &1.phone_number]))
|> CSV.encode()
|> Enum.into(File.stream!("contacts.csv"))
end
你在用还是别的什么?你在用还是别的什么?谢谢你的回复。但我犯了个错误
code
**(FunctionClauseError)文件中没有匹配的函数子句。normalize_modes/2为File.normalize_modes/2提供了以下参数:#1“contacts.csv”#2个真正尝试的函数子句(显示6个函数子句中的6个):defp normalize_modes([:utf8 | rest],binary?)defp normalize_modes([:read|ahead | rest],binary?)defp normalize_modes([mode | rest],_binary?)mode==:char|list或mode==:charlist defp normalize_modes([mode | rest],binary?)defp normalize| modes([],true)抱歉File.stream代码>行完全错误。你能试试编辑过的代码吗?我不需要使用IO.write函数来写入文件吗?不,file.stream
打开一个流,然后Enum.into
写入其中。无需手动调用IO。编写。感谢您的回复。但我犯了个错误code
**(FunctionClauseError)文件中没有匹配的函数子句。normalize_modes/2为File.normalize_modes/2提供了以下参数:#1“contacts.csv”#2个真正尝试的函数子句(显示6个函数子句中的6个):defp normalize_modes([:utf8 | rest],binary?)defp normalize_modes([:read|ahead | rest],binary?)defp normalize_modes([mode | rest],_binary?)mode==:char|list或mode==:charlist defp normalize_modes([mode | rest],binary?)defp normalize| modes([],true)抱歉File.stream代码>行完全错误。你能试试编辑过的代码吗?我不需要使用IO.write函数来写入文件吗?不,file.stream
打开一个流,然后Enum.into
写入其中。无需手动调用IO.write
。