come scorrere uno script attraverso più file CSVfacendo seguito a un post precedente, ho uno script che impila e formatta un file CSV ingombrante.
https://community.jmp.com/t5/Discussions/Help-doing-a-table-transform-and-converting-a-timestamp/td-p/303091
La mia prossima domanda di aiuto è come posso fare in modo che questo venga ripetuto su un gruppo di CSV in una cartella, catturando il nome del file come una delle colonne, idealmente anche impilandole. aka file step1, step2, step3, step4.
L'impilamento di un singolo CSV crea un output di 400k righe. Rischio di crash se unisco 4 o 8 o 10 di questi CSV?
Forse un extra sarebbe quello di passare nuovamente alla cartella successiva nella cartella principale, creando un nuovo file di passaggi combinato?
names default to here(1);
//Prompt to select and open an MS Excel file
dt = Open( Pick File( "Select Excel File", "C:\Users\folder\", {"Excel Files|csv;xlsx;xls", "All Files|*"}, 1, 0 ), Invisible );
// Get the column names from the data table
colNames = dt << get column names;
// Remove the 1st col from the list
remove from(colNames,1,1);
// Stack the data
dtStack = dt << Stack(
columns(
colNames
),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ),
Output Table( "Stacked_Data" )
);
//label the 1st column to Lamda
:Column 1 << Set Name("Lamda");
:Lamda << Modeling Type(nominal);
// Create a new JMP DateTime column
dtStack << New Column( "Timestamp",
Numeric,
"Continuous",
Format( "d/m/y h:m:s", 26, 3 ),
Input Format( "d/m/y h:m:s", 0 ),
Formula( Informat( Word( 1, :Label, "Z" ), "d/m/y h:m:s" ) )
);
// Remove formula to convert column to static values
dtStack:Timestamp << delete formula;
// Create the Time variable column
dtstack << New Column( "Time",
Numeric,
"Continuous",
Format( "hr:m:s", 17, 3 ),
Input Format( "hr:m:s", 0 ),
Formula( Time Of Day( :Timestamp ) )
);
//Create Absolute Time column, uses if>then error checking for Lamda group changing
dtstack << New Column( "AbsTime",
Numeric,
"Continuous",
Format( "Fixed Dec", 12, 1 ),
Formula(
If( Row() == 1 | :Lamda != Lag( :Lamda ),
0,
Lag( :AbsTime ) + (:Timestamp - Lag( :Timestamp ))
)
),
);
// Remove formula to convert column to static values
dtStack:AbsTime << delete formula;
// Get rid of no longer needed "Label" column
dtStack << delete columns( "Label" );
Tabella datiScriptingRi: come scorrere uno script attraverso più file CSVFondamentalmente, tutto ciò che devi fare è farlo
- Utilizzare "Files In Directory () per ottenere l'elenco dei file da elaborare
- Impostare una tabella dati da utilizzare per la tabella dati finale (dtFinal)
- Utilizzare un ciclo For () per eseguire il ciclo su tutti i file trovati nella directory che crea la tabella deStack
- Aggiungi la colonna Source a dtStack
- Concatena dtStack alla tabella dtFinal
- Chiudere le tabelle di dati dtStack e dt
Di seguito è riportato il tuo script modificato con gli elementi sopra.Il codice non è testato e devi aggiungere nella directory ecc. Per leggere i file da.Dovrebbe darti un ottimo punto di partenza per ottenere il tuo prodotto finale
names default to here(1);
//Prompt to select and open an MS Excel file
//dt = Open( Pick File( "Select Excel File", "C:\Users\folder\", {"Excel Files|csv;xlsx;xls", "All Files|*"}, 1, 0 ), Invisible );
// Get a list of files from the directory
fileList = Files In Directory( <PATH to="" your="" directory=""> || fileList[i] );
// Get the column names from the data table
colNames = dt << get column names;
// Remove the 1st col from the list
remove from(colNames,1,1);
// Stack the data
dtStack = dt << Stack(
columns(
colNames
),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ),
Output Table( "Stacked_Data" )
);
//label the 1st column to Lamda
:Column 1 << Set Name("Lamda");
:Lamda << Modeling Type(nominal);
// Create a new JMP DateTime column
dtStack << New Column( "Timestamp",
Numeric,
"Continuous",
Format( "d/m/y h:m:s", 26, 3 ),
Input Format( "d/m/y h:m:s", 0 ),
Formula( Informat( Word( 1, :Label, "Z" ), "d/m/y h:m:s" ) )
);
// Remove formula to convert column to static values
dtStack:Timestamp << delete formula;
// Create the Time variable column
dtstack << New Column( "Time",
Numeric,
"Continuous",
Format( "hr:m:s", 17, 3 ),
Input Format( "hr:m:s", 0 ),
Formula( Time Of Day( :Timestamp ) )
);
//Create Absolute Time column, uses if>then error checking for Lamda group changing
dtstack << New Column( "AbsTime",
Numeric,
"Continuous",
Format( "Fixed Dec", 12, 1 ),
Formula(
If( Row() == 1 | :Lamda != Lag( :Lamda ),
0,
Lag( :AbsTime ) + (:Timestamp - Lag( :Timestamp ))
)
),
);
// Remove formula to convert column to static values
dtStack:AbsTime << delete formula;
// Get rid of no longer needed "Label" column
dtStack << delete columns( "Label" );
// Add filename as a column to the data table
dtStack << new column( "Source", character, set each value( filename[i] ) );
// Concatenate to final table
dtFinal << concatenate( dtStack, append to first table(1) );
// Clean up
close( dtStack, nosave );
close( dt, nosave );
// End For() Loop
);</PATH>Ri: come scorrere uno script attraverso più file CSVControlla anche Importazione di più file. Carica i file CSV, li concatena se hanno le stesse colonne e può facoltativamente aggiungere la colonna del nome del file.
Ri: come scorrere uno script attraverso più file CSVEccezionale. Sembra funzionare, ma poi si verificano errori dopo il primo file con un file
"Name Unresolved: filenam} 1} nell'accesso o nella valutazione di" filename ", filename / * ### * /"
qualche idea?
Ho anche regolato lo script per creare un popup di selezione della cartella, presumo che non sia questo il problema.
// Get a list of files from the directory
dir = Pick Directory( "Select a directory", "C:\Users\folder\");
fileList = Files In Directory( dir );
// Create a skeleton data table to end up being the combined data table
dtFinal = New Table("Final");
// Loop across all of the files found in the directory and read them in
For(i = 1, i <= N Items( fileList ), i++,
dt = open( dir || fileList[i] );