我需要读取一个包含逗号字段的CSV文件,因此我引用了包含逗号的字段,例如:
1, "text1,text2", "text3, text4", a, b, c
Run Code Online (Sandbox Code Playgroud)
但是当我尝试用Python读取文件时,我得到的字段用逗号分隔,如下所示:
row[0] = 1
row[1] = text1
row[2] = text2
row[3] = text3
row[4] = text4
row[5] = a
row[6] = b
row[7] = c
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码阅读CSV文件:
info = csv.reader(open('./info.csv'))
for row in info :
print row[0] + " * " + row[1] ...
Run Code Online (Sandbox Code Playgroud)
是否可以读取包含逗号的双引号字段?
我正在插入一个包含外键的模型A到另一个模型B.
defmodule MyApp.ModelA do
use MyApp.Web, :model
schema "model_a" do
field :type, :string, null: false
field :data, :string, null: false
belongs_to :model_b, MyApp.ModelB
timestamps()
end
@required_fields ~w(type data)
@optional_fields ~w()
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @required_fields, @optional_fields)
|> assoc_constraint(:model_b)
end
end
Run Code Online (Sandbox Code Playgroud)
和模型B:
defmodule MyApp.ModelB do
use MyApp.Web, :model
schema "model_b" do
field :username, :string
field :pass, :string
has_many :model_a, MyApp.ModelA
timestamps()
end
@required_fields ~w(username …
Run Code Online (Sandbox Code Playgroud)