File size: 1,161 Bytes
ca165c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import csv
import os
# Function to extract the first column from a CSV file
def extract_first_column(csv_file):
with open(csv_file, 'r') as file:
reader = csv.reader(file)
first_column = [int(row[0]) for row in reader]
return first_column
# Directory containing combo CSV files
csv_directory = os.path.dirname(os.path.abspath(__file__))
# List to store the first columns from each CSV
all_first_columns = []
# Loop through each combo CSV file
for i in range(1, 7): # Assuming combo files are named combo1.csv to combo6.csv
csv_file_path = os.path.join(csv_directory, f'text4combo{i}.csv')
first_column = extract_first_column(csv_file_path)
all_first_columns.append(first_column)
# Transpose the list of lists to get a list of columns
all_first_columns_transposed = list(map(list, zip(*all_first_columns)))
# Save the transposed result into a new CSV file
output_csv_path = os.path.join(csv_directory, 'loopnum4.csv')
with open(output_csv_path, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(all_first_columns_transposed)
print(f"Combined first columns saved to {output_csv_path}")
|