# How do I export strings to CSV file？

**URL:** https://discourse.julialang.org/t/how-do-i-export-strings-to-csv-file/46833
**Category:** General Usage
**Tags:** question
**Created:** [September 18, 2020, 9:45am UTC](https://discourse.julialang.org/t/how-do-i-export-strings-to-csv-file/46833 "2020-09-18T09:45:57Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![zlq178](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlq178/32/17952_2.png) [@zlq178](https://discourse.julialang.org/u/zlq178)
#### Post date: [September 18, 2020, 9:45am UTC](https://discourse.julialang.org/t/how-do-i-export-strings-to-csv-file/46833/1 "2020-09-18T09:45:57Z")

</div>

```julia
using CSV,DataFrames
data1=DataFrame(CSV.File(“D:\\documemt\\julia\\aaa.csv”))
a=[1,1,2,2,1,2]
index=findall(in(1),a)
Data=data1.colum1[index]
CSV.write(“BBBB.csv”,Data)

```

The contents of the aaa. CSV file are as follows  
colum1  
2018/12/30 0:00  
2018/12/30 1:00  
2018/12/30 2:00  
2018/12/30 3:00  
2018/12/30 4:00  
2018/12/30 5:00  
2018/12/30 6:00  
2018/12/30 7:00  
2018/12/30 8:00  
2018/12/30 9:00

The last line of the code reports an error, as follows:  
LoadError: ArgumentError: ‘Array{String,1}’ iterates ‘String’ values, which doesn’t satisfy the Tables.jl `AbstractRow` interface

How do I export the Data’s elements to a CSV file?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [September 18, 2020, 10:26am UTC](https://discourse.julialang.org/t/how-do-i-export-strings-to-csv-file/46833/2 "2020-09-18T10:26:27Z")

</div>

As the error says, `CSV.write` expects you to provide an object that is a Tables.jl `Table` (in the sense of `Tables.istable(obj) == true`).

The simplest object that satisfies this requirement is probably a named tuple, so you could do:

```julia
CSV.write("BBBB.csv", (column1 = Data))

```

where `(column1 = Data)` constructs the named tuple.

---

<div class="post-metadata">

### Author: ![zlq178](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlq178/32/17952_2.png) [@zlq178](https://discourse.julialang.org/u/zlq178)
#### Post date: [September 18, 2020, 11:17am UTC](https://discourse.julialang.org/t/how-do-i-export-strings-to-csv-file/46833/3 "2020-09-18T11:17:39Z")

</div>

I did the operation according to your advice, no error was reported, but I could not find the BBBB file. Is the last code invalid?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [September 18, 2020, 11:33am UTC](https://discourse.julialang.org/t/how-do-i-export-strings-to-csv-file/46833/4 "2020-09-18T11:33:29Z")

</div>

Apologies, it was indeed slightly wrong - you want either `(column1 = Data, )` (note the trailing comma) or `(; column1 = Data)` (note the leading semicolon) to construct the NamedTuple.

---

<div class="post-metadata">

### Author: ![zlq178](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlq178/32/17952_2.png) [@zlq178](https://discourse.julialang.org/u/zlq178)
#### Post date: [September 18, 2020, 12:48pm UTC](https://discourse.julialang.org/t/how-do-i-export-strings-to-csv-file/46833/5 "2020-09-18T12:48:51Z")

</div>

I still couldn’t find the BBBB file after running

```julia
using CSV,DataFrames
data1=DataFrame(CSV.File("D:\\documemt\\julia\\aaa.csv"))
a=[1,1,2,2,1,2]
index=findall(in(1),a)
Data=data1.colum1[index]
CSV.write("BBBB.csv";column1=Data)

```

Can you find this file?  
How can I solve it?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [September 18, 2020, 1:08pm UTC](https://discourse.julialang.org/t/how-do-i-export-strings-to-csv-file/46833/6 "2020-09-18T13:08:30Z")

</div>

No sorry you misunderstood what I was trying to say: the leading semicolon (or trailing comma) are just in the creation of the NamedTuple, so:

```julia
(; column1 = Data)

```

which should then be passed as the second argument to `CSV.write`:

```julia
CSV.write("BBBB.csv", (; column1 = Data))

```

---

<div class="post-metadata">

### Author: ![zlq178](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlq178/32/17952_2.png) [@zlq178](https://discourse.julialang.org/u/zlq178)
#### Post date: [September 18, 2020, 1:25pm UTC](https://discourse.julialang.org/t/how-do-i-export-strings-to-csv-file/46833/7 "2020-09-18T13:25:21Z")

</div>

Ok I‘ve got it. Thank you very much~

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [September 18, 2020, 1:48pm UTC](https://discourse.julialang.org/t/how-do-i-export-strings-to-csv-file/46833/8 "2020-09-18T13:48:14Z")

</div>

Glad you got it to work! A couple of other general hints if I may:

- The CSV.jl package has brought back the `CSV.read` function, so you can do `CSV.read("myfile.csv", DataFrame)` to read a file into a DataFrame

- If you know you want to write a table back out to csv, it’s probably easier to continue operating on a `DataFrame` rather than pulling out a `Vector` and creating a `NamedTuple` from that - I would probably do `data1[index, [:column1]]`. Note the column index is passed in `[`] brackets, making it an `Array{Symbol ,1}` (rather than just a `Symbol`). Doing so means the indexing operation returns a `DataFrame`.

---

<div class="post-metadata">

### Author: ![zlq178](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlq178/32/17952_2.png) [@zlq178](https://discourse.julialang.org/u/zlq178)
#### Post date: [September 19, 2020, 1:41am UTC](https://discourse.julialang.org/t/how-do-i-export-strings-to-csv-file/46833/9 "2020-09-19T01:41:08Z")

</div>

Nice! Your proposed code is much better than mine. Thanks again~
