come scorrere uno script attraverso più file CSVin 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, acquisendo il nome del file come una delle colonne, idealmente impilandole anche. ovvero file step1, step2, step3, step4.
L'impilamento di un singolo CSV crea un output di 400.000 righe. Rischio di bloccarmi se unisco 4 o 8 o 10 di questi CSV?
Forse sarebbe necessario scorrere nuovamente la 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" );
Combinazione e pulizia dei datiRe: come scorrere uno script attraverso più file CSVSembra che il codice voglia utilizzare filelist anziché filename per l'elenco dei nomi di file. Quello verso la fine.
Re: come scorrere uno script attraverso più file CSVHo guardato questo ma non sembra adatto alle mie esigenze. Si apriranno semplicemente i singoli jmp dt. I numeri di colonna sono dinamici, in base all'intervallo di tempo per ciascun file di processo.
Re: come scorrere uno script attraverso più file CSVEccezionale. Sembra che funzioni, ma poi si verificano errori dopo il primo file w/ an
"Nome non risolto: nomefile}1} nell'accesso o nella valutazione di 'nomefile' , nomefile/*###*/"
qualche idea?
Ho anche modificato lo script per eseguire 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] );Re: come scorrere uno script attraverso più file CSVControlla anche l'importazione di più file. Carica file CSV, li concatena se hanno le stesse colonne e facoltativamente può aggiungere la colonna del nome file.
Re: come scorrere uno script attraverso più file CSVFondamentalmente, tutto ciò che devi fare è farlo
- Utilizzare "File nella directory() per ottenere l'elenco dei file da elaborare
- Imposta 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 creando la tabella deStack
- Aggiungi la colonna Source a dtStack
- Concatena dtStack alla tabella dtFinal
- Chiudi le tabelle dei dati dtStack e dt
Di seguito è riportato lo script modificato con gli elementi precedenti.Il codice non è testato e devi aggiungere la directory ecc. da cui leggere i file.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>