I have a CSV file, here are two lines in the file.
c1,c2,c3,c4,c5
17939,2507974,11,DVD version has 1 hour of extras of 5 bonus matches including: - Stacy Keibler vs Torrie Wilson in a bikini contest. - A tour of Trish Stratus\' place. - Behind the scenes look at the WWE women division.,NULL
16641,2425413,11,"The Australian TV version had a scene included at the end where a cop car was driving in an alley way, narrowly missing someone walking. This scene was also used in the 1980 film, \"Alligator\".",NULL
127472,2130098,13,"FACT: Dunn uploads a file from an Apple Powerbook in \"C:\\\", which would be appropriate for a DOS/Windows system.",NULL
I would like to cut the c4 columns to maximal length (say 500) and keep everything else unchanged and save it to a new csv file.
Here is my implementation.
import csv
import sys
with open("new_file_name.csv", 'w', newline='') as csvwriter:
spamwriter = csv.writer(csvwriter, delimiter=',', quotechar='"', escapechar='\\')
with open("old_file_name.csv", newline='') as csvreader:
spamreader = csv.reader(csvreader, delimiter=',', quotechar='"', escapechar='\\')
for row in spamreader:
if len(row[3]) > 500:
print("cut this line")
row[n] = row[n][:500]
spamwriter.writerow(row)
However, the CSV file that I obtained is
17939,2507974,11,DVD version has 1 hour of extras of 5 bonus matches including: - Stacy Keibler vs Torrie Wilson in a bikini contest. - A tour of Trish Stratus' place. - Behind the scenes look at the WWE women division.,NULL
16641,2425413,11,"The Australian TV version had a scene included at the end where a cop car was driving in an alley way, narrowly missing someone walking. This scene was also used in the 1980 film, ""Alligator"".",NULL
127472,2130098,13,"FACT: Dunn uploads a file from an Apple Powerbook in \"C:\\", which would be appropriate for a DOS/Windows system.",NULL
The black-slash is missing in my new csv file. What I want is
17939,2507974,11,DVD version has 1 hour of extras of 5 bonus matches including: - Stacy Keibler vs Torrie Wilson in a bikini contest. - A tour of Trish Stratus\' place. - Behind the scenes look at the WWE women division.,NULL
16641,2425413,11,"The Australian TV version had a scene included at the end where a cop car was driving in an alley way, narrowly missing someone walking. This scene was also used in the 1980 film, \"Alligator\".",NULL
127472,2130098,13,"FACT: Dunn uploads a file from an Apple Powerbook in \"C:\\\", which would be appropriate for a DOS/Windows system.",NULL
I try something like quoting=csv.QUOTE_ALL, but it also changes my origin CSV file when value of c4 is less than 500. What I want is a new CSV file without changing any origin character for the first 500 characters.
Thanks.