cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
dlemieu1
Level II

Moving header down into row 1

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));
);
1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Moving header down into row 1

You might want to try another option.

  1. Click on Open
  2. Navigate to the folder where the csv file is
  3. Single click on the csv file you want to open
  4. A list of options will be displayed, select "Data (Using Preview)"txnelson_0-1624989098428.png

     

  5. Click Open
  6. Go through the input Wizard.  Start by unselecting "File contains column names on line:"
Jim

View solution in original post

6 REPLIES 6
txnelson
Super User

Re: Moving header down into row 1

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.

Jim
dlemieu1
Level II

Re: Moving header down into row 1

I changed the settings you mentioned to:
Column Names Start( 0 ),
Data Starts( 1 ),

Since this raw csv file doesn't have a header at all. But it doesn't change how it's opening - row 1 is still being imported as the header unfortunately.
txnelson
Super User

Re: Moving header down into row 1

You might want to try another option.

  1. Click on Open
  2. Navigate to the folder where the csv file is
  3. Single click on the csv file you want to open
  4. A list of options will be displayed, select "Data (Using Preview)"txnelson_0-1624989098428.png

     

  5. Click Open
  6. Go through the input Wizard.  Start by unselecting "File contains column names on line:"
Jim
dlemieu1
Level II

Re: Moving header down into row 1

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;
txnelson
Super User

Re: Moving header down into row 1

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.

Jim

Re: Moving header down into row 1

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.