Hello,
My raw csv file is in a weird format, causing the first data point I need to be imported as the header, which is not what I want. So I created a new, blank row at the top, and am trying to figure out how I can pull the column name into the blank row before renaming the header? Any help is appreciated!
names default to here(1);
dt=current data table();
For (i=1, i<=2, i++,
Row(1)[i] = Char(Column Name(i));
);
You might want to try another option.
You should be able to modify the settings to compensate for the weird format in your csv file. Below is a simple script that open a csv file, and you can change the Column Names Start() and the Data Starts() parameters so that you do not end up with bad headers or data.
Open(
"Path to your csv file",
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Comma, CSV( 1 ) ),
Strip Quotes( 0 ),
Use Apostrophe as Quotation Mark( 0 ),
Use Regional Settings( 0 ),
Scan Whole File( 1 ),
Treat empty columns as numeric( 0 ),
CompressNumericColumns( 0 ),
CompressCharacterColumns( 0 ),
CompressAllowListCheck( 0 ),
Labels( 1 ),
Column Names Start( 1 ),
Data Starts( 2 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
);
These JSL statements were retrieved by just opening up a csv file, and then looking in the Source by right clicking on the green triangle in front of the Source entry in the Tables Panel at the left of the opened data table.
You might want to try another option.
This script is meant to convert over 100 csv files into a particular format and append them all - so I'd really like to get your first suggestion working if I could. Clicking each file and changing the settings isn't going to work since there are so many.
The way I thought of was to add a new blank row and fill in cells 1 and 2 with the column headers then change the names of the columns afterwards. I actually have working code for this, but only if I run it in the debugger. If I just hit run to execute all in one go, the cells in the new row remain blank, but if I run it in the debugger (which I initially opened to try and figure out the issue), the script ends properly and the cells fill in correctly, so I'm super confused as to why that is.
Names Default To Here( 1 );
Open( "path to csv file" );
dt = Current Data Table();
dt << addRows( 1, atStart );
a = Char( Column Name( 1 ) );
b = Char( Column Name( 2 ) );
Column( 1 )[1] = a;
Column( 2 )[1] = b;
What I am suggesting, is to use the input wizard once, and then extracting the script it produces, and then use that script to read in the many different files.
I wonder if you could use the File > Import Multiple Files. The dialog from clicking the Settings button seems to address a headless CSV file.