open a csv file, split the third column into multiple 25 row columns to the right of the third column, save the modification, and then iterate over all csv files in a folder in python

To open a CSV file, split the third column, save the modification, and iterate over all CSV files in a folder in Python, you can use the csv module for file handling and the os module for working with folders. Here's an example:

main.py
import csv
import os

def split_column(csv_file):
    # Read the CSV file
    with open(csv_file, 'r') as file:
        reader = csv.reader(file)
        rows = list(reader)

    # Split the third column into multiple 25-row columns
    for i, row in enumerate(rows):
        third_column = row[2]  # Assuming the third column is at index 2
        split_values = [third_column[j:j+25] for j in range(0, len(third_column), 25)]
        row[2:2] = split_values

    # Save the modified CSV file
    with open(csv_file, 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerows(rows)

# Iterate over all CSV files in a folder
folder_path = "/path/to/folder"  # Replace with the actual folder path
for filename in os.listdir(folder_path):
    if filename.endswith(".csv"):
        csv_file = os.path.join(folder_path, filename)
        split_column(csv_file)
883 chars
27 lines

Make sure to replace /path/to/folder with the actual path to the folder containing the CSV files. Also, keep in mind that the code assumes the third column starts at index 2 in the CSV file. Adjust the index accordingly if your CSV file has a different structure.

gistlibby LogSnag