我有一个CSV文件,这里有两行文件。
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我希望将c4列削减到最大长度(例如500),并保持其他所有内容不变,并将其保存到一个新的csv文件中。
这是我的实现。
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)但是,我获得的CSV文件是
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黑斜杠在我的新csv文件中不见了。我想要的是
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我尝试类似于quoting=csv.QUOTE_ALL,但当c4值小于500时,它也会更改我的原始CSV文件。我想要的是一个新的CSV文件,而不更改前500个字符的任何原始字符。
谢谢。
发布于 2022-12-01 18:59:02
您可以在doublequote=False中使用csv.writer
import csv
with open("input.csv", "r") as f_in, open("output.csv", "w") as f_out:
reader = csv.reader(f_in, delimiter=",", quotechar='"', escapechar="\\")
writer = csv.writer(
f_out,
delimiter=",",
quotechar='"',
escapechar="\\",
doublequote=False,
)
writer.writerow(next(reader))
for row in reader:
row[3] = row[3][:500]
writer.writerow(row)output.csv变成:
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\".",NULLhttps://stackoverflow.com/questions/74646678
复制相似问题