Icon

kn_​forum_​65924_​import_​csv_​detect_​header

import CSV file with KNIME node - skipping headers

import CSV file with KNIME node - skipping headers

import CSV file with KNIME node - skipping headershttps://forum.knime.com/t/number-of-lines-is-different-from-what-the-txt-file-actually-contains/65924/8?u=mlauber71 import knime.scripting.io as knio# This example script creates an output table containing randomly drawn integers using numpyand pandas.import numpy as npimport pandas as pdimport csvfile_path = knio.flow_variables['v_csv_file']with open(file_path, "r", encoding="utf-8") as f: # Read the file as a list of lines lines = f.readlines()# Find the line number with "| Ano|Período|Cen.lucro"start_line = 0for i, line in enumerate(lines): if "| Ano|Período|Cen.lucro" in line: start_line = i breakfirst_line = start_line -1# Skip the first 8 lines (0-indexed) and start reading from line 9df = pd.read_csv(file_path, delimiter="|", skiprows=range(first_line), header=1)# Clean up the column namesdf.columns = [col.strip() for col in df.columns]# Print the DataFrame# print(df)knio.flow_variables['v_skip_first_lines'] = start_lineknio.output_tables[0] = knio.Table.from_pandas(df) my_data.txtlocate and create/data/ folderwith absolute pathsv_csv_fileimportCSV filemy_data.txtuse Flow Variable"v_skip_first_lines"to skip lines CSV Reader Collect LocalMetadata Java EditVariable (simple) Python Script CSV Reader import CSV file with KNIME node - skipping headershttps://forum.knime.com/t/number-of-lines-is-different-from-what-the-txt-file-actually-contains/65924/8?u=mlauber71 import knime.scripting.io as knio# This example script creates an output table containing randomly drawn integers using numpyand pandas.import numpy as npimport pandas as pdimport csvfile_path = knio.flow_variables['v_csv_file']with open(file_path, "r", encoding="utf-8") as f: # Read the file as a list of lines lines = f.readlines()# Find the line number with "| Ano|Período|Cen.lucro"start_line = 0for i, line in enumerate(lines): if "| Ano|Período|Cen.lucro" in line: start_line = i breakfirst_line = start_line -1# Skip the first 8 lines (0-indexed) and start reading from line 9df = pd.read_csv(file_path, delimiter="|", skiprows=range(first_line), header=1)# Clean up the column namesdf.columns = [col.strip() for col in df.columns]# Print the DataFrame# print(df)knio.flow_variables['v_skip_first_lines'] = start_lineknio.output_tables[0] = knio.Table.from_pandas(df) my_data.txtlocate and create/data/ folderwith absolute pathsv_csv_fileimportCSV filemy_data.txtuse Flow Variable"v_skip_first_lines"to skip linesCSV Reader Collect LocalMetadata Java EditVariable (simple) Python Script CSV Reader

Nodes

Extensions

Links