python script to remove - and replace with space for name column
Here is a sample Python script to remove "- " and replace with space for the name column in a CSV file:
import csv
# Open the CSV file
with open('input.csv', 'r') as infile, open('output.csv', 'w', newline='') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
# Loop through each row in the CSV file
for row in reader:
# Replace "- " with space in the name column
row[0] = row[0].replace('- ', ' ')
# Write the modified row to the output CSV file
writer.writerow(row)
In this script, we open the input CSV file and create a new output CSV file. We then use the csv.reader and csv.writer to read and write the rows in the CSV file.
For each row, we replace "- " with a space in the first (name) column using the replace method. Finally, we write the modified row to the output CSV file using the writer.writerow method.
You can adjust the input and output file paths as needed for your specific use case
原文地址: https://www.cveoy.top/t/topic/eCkU 著作权归作者所有。请勿转载和采集!