首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python编写器保留转义字符

python编写器保留转义字符
EN

Stack Overflow用户
提问于 2022-12-01 18:37:48
回答 1查看 21关注 0票数 1

我有一个CSV文件,这里有两行文件。

代码语言:javascript
复制
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文件中。

这是我的实现。

代码语言:javascript
复制
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文件是

代码语言:javascript
复制
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文件中不见了。我想要的是

代码语言:javascript
复制
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个字符的任何原始字符。

谢谢。

EN

Stack Overflow用户

发布于 2022-12-01 18:59:02

您可以在doublequote=False中使用csv.writer

代码语言:javascript
复制
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变成:

代码语言:javascript
复制
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
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74646678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档