openmolecules.org

 
Home » DataWarrior » Functionality » Batch convert .csv to .DWAR
Batch convert .csv to .DWAR [message #674] Fri, 18 October 2019 05:00 Go to next message
Luisa_ddc is currently offline  Luisa_ddc
Messages: 2
Registered: October 2019
Junior Member
Hello, I have a folder contains more than 50 files and I want to apply the same template to the entire folder. Is it possible to avoid repeating the same operation 50 times? Thanks Smile
Re: Batch convert .csv to .DWAR [message #680 is a reply to message #674] Mon, 21 October 2019 21:32 Go to previous messageGo to next message
thomas is currently offline  thomas
Messages: 715
Registered: June 2014
Senior Member
If you are on Linux or Mac, you may copy the following line into a new text file, save it as "myscript"
into your folder, make it executable with "chmod 755 myscript" and run it with "./myscript":

for FILE in *.dwar ; do (cat "$FILE" >> new_"$FILE"; cat template.dwat >> new_"$FILE"); done

If you have a new Windows 10 with bash support, I would assume that this should then also work.
Re: Batch convert .csv to .DWAR [message #681 is a reply to message #680] Tue, 22 October 2019 03:18 Go to previous messageGo to next message
Luisa_ddc is currently offline  Luisa_ddc
Messages: 2
Registered: October 2019
Junior Member
Hi, Thomas, thanks for your reply.
Now my raw files are in .csv format with one structure column and other numerical columns. How should I batch convert them to DWAR format in the first place?
Best.
Re: Batch convert .csv to .DWAR [message #684 is a reply to message #681] Wed, 23 October 2019 00:25 Go to previous messageGo to next message
nbehrnd is currently offline  nbehrnd
Messages: 224
Registered: June 2019
Senior Member
Assuming Thomas' script did the catenate the files, ensure that each of the columns
has a header; for example "structure, mass" as in the example.csv attached below.
(An extension .txt equally will be recognized sucessfully by DataWarrior. If working
in Linux, DataWarrior will trade well with the file regardless if the line ending
is encoded in dos, or unix convention, too.)

Then open either by shortcut Ctrl + O, or in the menue File -> Open the directory
and eventually the .csv in question. While reading the file, DataWarrior converts
automatically the structure string to populate an additional column.

index.php?t=getfile&id=125&private=0

Do not forget to explicitly instruct DataWarrior to save the read as .dwar.
  • Attachment: dw_import.png
    (Size: 4.77KB, Downloaded 761 times)
  • Attachment: example.csv
    (Size: 0.05KB, Downloaded 426 times)
  • Attachment: example.dwar
    (Size: 1.37KB, Downloaded 429 times)

[Updated on: Wed, 23 October 2019 00:27]

Report message to a moderator

Re: Batch convert .csv to .DWAR [message #690 is a reply to message #684] Thu, 24 October 2019 22:08 Go to previous message
thomas is currently offline  thomas
Messages: 715
Registered: June 2014
Senior Member
you might use a DataWarrior macro to open the csv-file, append the template, save the file and quit DataWarrior (as shown below). Then use a Linux loop to mv one file after another into another working directory, apply the macro and rename and move the saved file back. Assuming, your csv files are in /home/thomas/csv and we use a working directory /home/thomas/test that contains the template t.dwat and convert.dwam, then it would look like this:

cd /home/thomas/csv
for FILE in *.csv ; do (mv "$FILE" ../test/t.csv; /opt/datawarrior/datawarrior ../test/convert.dwam; mv ../test/t.dwar "$FILE".dwar); done


this goes into the convert.dwam:

<macro name="convert">
<task name="openFile">
fileName=/home/thomas/test/t.csv
</task>
<task name="openAndApplyTemplate">
fileName=/home/thomas/test/t.dwat
</task>
<task name="saveFileAs">
fileName=/home/thomas/test/t.dwar
</task>
<task name="exitProgram">
saveChanges=yes
</task>
</macro>
Previous Topic: Change 2D Axes Range
Next Topic: Structure No Column
Goto Forum:
  


Current Time: Sun Nov 24 23:07:50 CET 2024

Total time taken to generate the page: 0.03768 seconds