find the values from CSV files
test
test
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class CSVHeaderFinder {
public static void main(String[] args) {
String filePath = "your_csv_file.csv";
String targetValue = "target_value";
try (Reader reader = new FileReader(filePath);
CSVParser csvParser = CSVFormat.DEFAULT.parse(reader)) {
CSVRecord headerRecord = csvParser.getRecords().get(0);
int columnIndex = -1;
for (int i = 0; i < headerRecord.size(); i++) {
if (headerRecord.get(i).equals(targetValue)) {
columnIndex = i;
break;
}
}
if (columnIndex != -1) {
String headerValue = headerRecord.get(columnIndex);
System.out.println("Header value: " + headerValue);
} else {
System.out.println("Value not found in any column headers.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import pandas as pd
# Load your data into a pandas DataFrame
data = pd.read_csv("your_data.csv")
# Search for the value "target_value" in all columns
target_value = "target_value"
column_containing_value = data.columns[data.isin([target_value]).any()].tolist()[0]
# Retrieve the header corresponding to the column containing the value
header_value = data.columns[data.columns.get_loc(column_containing_value)]
print("Header value:", header_value)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class CSVValueSearch {
public static void main(String[] args) {
String filePath = "path_to_your_csv_file.csv"; // Update with your CSV file path
String searchValue = "value_to_search"; // Update with the value you want to search
try (BufferedReader br = new BufferedReader(new FileReader(new File(filePath)))) {
String headerLine = br.readLine(); // Read the header row
String[] headers = headerLine.split(","); // Assuming CSV uses comma as delimiter
// Find the index of the search value in the header row
int columnIndex = -1;
for (int i = 0; i < headers.length; i++) {
if (headers[i].equals(searchValue)) {
columnIndex = i;
break;
}
}
// If the search value is not found in the header row
if (columnIndex == -1) {
System.out.println("Value '" + searchValue + "' not found in the header row.");
return;
}
// Read and search the rest of the file
String line;
while ((line = br.readLine()) != null) {
String[] values = line.split(","); // Assuming CSV uses comma as delimiter
if (columnIndex < values.length) {
String headerName = headers[columnIndex];
String cellValue = values[columnIndex];
if (cellValue.equals(searchValue)) {
System.out.println("Found value '" + searchValue + "' in column '" + headerName + "'.");
return; // Exit if the value is found
}
}
}
// If the value is not found in the file
System.out.println("Value '" + searchValue + "' not found in the file.");
} catch (IOException e) {
e.printStackTrace();
}
}
}