Read csv file in python and save its output in different csv file
import csv
from gensim.summarization import summarize
# Define the input and output file paths
input_file_path = "input.csv" # Replace with the path to your input CSV file
output_file_path = "output.csv" # Replace with the path for the output CSV file
# Open the input CSV file and the output CSV file
with open(input_file_path, "r", encoding="utf-8") as input_csv_file, \
open(output_file_path, "w", encoding="utf-8", newline="") as output_csv_file:
# Create CSV reader for the input file and writer for the output file
csv_reader = csv.reader(input_csv_file, delimiter=',') # Change delimiter if needed
csv_writer = csv.writer(output_csv_file, delimiter=',')
# Skip the header row in the input CSV file
next(csv_reader, None) # Skip the first row (header)
# Iterate through each row in the input CSV file
for row in csv_reader:
text_data = row[0] # Assuming the text is in the first column (index 0)
# Apply summarization to the text_data
summary = summarize(text_data, ratio=0.2, split=False)
# Write the input and summary to the output CSV file
csv_writer.writerow([text_data, summary])
# Close the files
input_csv_file.close()
output_csv_file.close()
Comments
Post a Comment