# Create a CSV from other CSVs

**URL:** https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636
**Category:** Automation
**Created:** [September 3, 2022, 6:17pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636 "2022-09-03T18:17:22Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![rctill](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@rctill](https://discourse.devontechnologies.com/u/rctill)
#### Post date: [September 3, 2022, 6:17pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/1 "2022-09-03T18:17:22Z")

</div>

I want a copy of the header (first row) of about 50 .csv files to identify some of the non-compliant files.

Ideally, it would be great to automatically develop a .csv or an excel sheet that lists the filename in the first column, followed by the 26 header columns names of the .csv files. This way, I can determine the years the creators of the tables modified the structure. Doing this without automation will be pure drudgery.

Any thoughts on how to do this from within Devonthink?

---

<div class="post-metadata">

### Author: ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)
#### Post date: [September 3, 2022, 7:32pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/2 "2022-09-03T19:32:05Z")

</div>

I would use an Applescript, creating the .csv file  
Not clear on the format for the “26 header columns names”; samples might help

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [September 4, 2022, 2:46pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/3 "2022-09-04T14:46:30Z")

</div>

> [@rctill](#):
>
> to identify some of the non-compliant files.

What _non-compliant files_ ?

Yes, please clarify what you’re actually after and include an example file, if possible.

---

<div class="post-metadata">

### Author: ![rctill](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@rctill](https://discourse.devontechnologies.com/u/rctill)
#### Post date: [September 4, 2022, 6:24pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/4 "2022-09-04T18:24:28Z")

</div>

I have 30 CSV (Comma Separated Values) files. Each table of values in each file represents a year’s worth of information. The first line of each file is a header containing the column names (not numerical values).

Reading the first line of each file and comparing all 30 of them would allow me to compare the column names and find the years where someone might have added, removed, or renamed a column.

Is there a quick way to create a csv that takes the first line of each of these files and puts them in its own table?

The output table would look like this:

“Year 01”, “R Id”,“Empl Id”,“Instructor”,“Course Identifier”,“Section Code”,“Course Title”  
“Year 02”, “R Id”,“Empl Id”,“Instructor”,“Course Identifier”,“Section Code”,“Course Title”  
“Year 03”, “R Id”,“Empl Id”,“Instructor”,“Course Identifier”,“Section Code”,“Course Space”  
"Year …

The table would identify that a change occurred between Year 02 and Year 03

Thanks

---

<div class="post-metadata">

### Author: ![rmschne](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/r/8baadc/32.png) [@rmschne](https://discourse.devontechnologies.com/u/rmschne)
#### Post date: [September 4, 2022, 6:37pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/5 "2022-09-04T18:37:54Z")

</div>

I perhaps am a little biased. i can think of no easy way to do this DEVONthink. But Python has great ways to handle this sort of complexity including reading and writing files. The analysis part will be as complicated as you need.

---

<div class="post-metadata">

### Author: ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)
#### Post date: [September 4, 2022, 7:02pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/6 "2022-09-04T19:02:47Z")

</div>

> [@rctill](#):
>
> Is there a quick way to create a csv that takes the first line of each of these files and puts them in its own table

I’d say a short script could do that. But there might be another way I’m not aware of.

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [September 4, 2022, 10:12pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/7 "2022-09-04T22:12:08Z")

</div>

Try this…

```auto
tell application id "DNtp"
	if (count (selected records)) = 0 then return
	set tempText to {}
	repeat with theRecord in (selected records)
		if (type of theRecord) = sheet then
			copy (name of theRecord & linefeed & "	" & (columns of theRecord) & linefeed & linefeed) to end of tempText
		end if
	end repeat
	create record with {name:"Headers", type:rtf, content:tempText as string} in current group
end tell

```

---

<div class="post-metadata">

### Author: ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)
#### Post date: [September 4, 2022, 11:07pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/8 "2022-09-04T23:07:18Z")

</div>

> [@BLUEFROG](#):
>
> `if (type of theRecord) = sheet`

Will this work for .csv files?  
I did noticed they display as sheets  
edited: but with default ABCDEF headers

It’s less complicated than my processing  
I was literally copying the first line from each .csv file

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [September 5, 2022, 12:02am UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/9 "2022-09-05T00:02:13Z")

</div>

Yes, it should as .CSV files are considered sheets.

---

<div class="post-metadata">

### Author: ![rctill](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@rctill](https://discourse.devontechnologies.com/u/rctill)
#### Post date: [September 5, 2022, 12:05am UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/10 "2022-09-05T00:05:35Z")

</div>

Thanks, BLUEFROG!

The output lists two headers from the two files I selected, which is promising.

I tried changing the output type to csv by changing the type in this line:

**create record with** {name:“Headers”, type:csv, _content_ :tempText **as** _string_ } in current group

It compiles, but I’m not getting an output file anymore.

Thanks again,

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [September 5, 2022, 1:36am UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/11 "2022-09-05T01:36:27Z")

</div>

There is no .CSV type.  
Run it as-is. What is is the resulting rtf?

---

<div class="post-metadata">

### Author: ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)
#### Post date: [September 5, 2022, 4:07am UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/12 "2022-09-05T04:07:31Z")

</div>

> [@rctill](#):
>
> I tried changing the output type to csv

> [@BLUEFROG](#):
>
> There is no .CSV type.

My solution is to write the data to a .csv file on my desktop  
instead of a record in Devonthink

```auto
set targetFile to (path to desktop as text) & "theNewFile.csv"	
set openFile to open for access file targetFile with write permission
write theNewcsvFileText to openFile starting at eof as text
```

---

<div class="post-metadata">

### Author: ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)
#### Post date: [September 5, 2022, 4:19am UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/13 "2022-09-05T04:19:54Z")

</div>

> [@BLUEFROG](#):
>
> CSV files are considered sheets.

I’m having fun with “considered sheets”

All three of my test files show with default ABCDEF headers

When my script retrieves the file contents via plain text  
. an ABCDEF row has been added  
. the comma delimiters have been converted to tab delimiters

---

<div class="post-metadata">

### Author: ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)
#### Post date: [September 5, 2022, 11:34am UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/14 "2022-09-05T11:34:48Z")

</div>

What is the `type` property of these files? It it’s sheet, using `plainText` is not sensible. There are other properties for sheets containing the headers and cells.

---

<div class="post-metadata">

### Author: ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)
#### Post date: [September 5, 2022, 2:05pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/15 "2022-09-05T14:05:40Z")

</div>

> [@chrillek](#):
>
> What is the `type` property of these files?

These are text files, extension .csv imported into Devonthink  
They contain comma delimited data we need to access  
Devonthink shows the kind as CSV Document

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [September 5, 2022, 2:12pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/16 "2022-09-05T14:12:05Z")

</div>

I am seeing no issue with the script I offered and CSV files imported into DEVONthink. Regardless of the type shown, they are treated and displayed as sheets.

---

<div class="post-metadata">

### Author: ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)
#### Post date: [September 5, 2022, 3:48pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/17 "2022-09-05T15:48:33Z")

</div>

I have a CSV file like this:

```auto
"1979","Another Room","Reason"
"The Year","The Room","The Reason"

```

After importing it in DT, I get a “Sheet” record (which is fine) with header columns “A”, “B”, and “C”. I would have expected headers “1979”, “Another Room”, and “Reason”. That is exactly what @DTLow reported with their imported CSVs.

Also, I can’t _change_ the headers, contrary to what the manual seems to think:

> You will just need to provide starting column headings, which you can certainly add or take away from later.

OTOH, I do not really know what “add or take away from header” is supposed to mean.

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [September 5, 2022, 4:52pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/18 "2022-09-05T16:52:17Z")

</div>

I have CSV files from my financial institution and other sources and none of the values are quoted.  
They not only come through as a sheet with proper headers but I can also edit the columns as I wish.

@cgrunenberg would have to assess this.

Where are you creating the CSV file?

---

<div class="post-metadata">

### Author: ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)
#### Post date: [September 5, 2022, 5:31pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/19 "2022-09-05T17:31:54Z")

</div>

> [@BLUEFROG](#):
>
> Where are you creating the CSV file?

In my text editor. Afaik, quoted strings are ok in CSV files. They are, for example, used to quote strings containing commas

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [September 5, 2022, 5:43pm UTC](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636/20 "2022-09-05T17:43:06Z")

</div>

Yes, they should be.  
I’m not able to reproduce the behavior yet.  
I’m making mine in BBEdit, UTF-8 with LF endings. Then dragging and dropping into DEVONthink yields a sheet with the headers intact.

[Next page](https://discourse.devontechnologies.com/t/create-a-csv-from-other-csvs/72636.md?page=2)
