如何从文件中所有引用的文本中删除换行符? [英] How could I remove newlines from all quoted pieces of text in a file?

查看:47
本文介绍了如何从文件中所有引用的文本中删除换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中导出了一个 CSV 文件.某些字段是较长的文本块,并且可以包含换行符.从该文件中仅删除双引号内的换行符但保留所有其他换行符的最简单方法是什么?

I have exported a CSV file from a database. Certain fields are longer text chunks, and can contain newlines. What would be the simplest way of removing only newlines from this file that are inside double quotes, but preserving all others?

我不在乎它是使用 Bash 命令行单行还是简单的脚本,只要它可以工作.

I don't care if it uses a Bash command line one liner or a simple script as long as it works.

例如

"Value1", "Value2", "This is a longer piece
    of text with
    newlines in it.", "Value3"
"Value4", "Value5", "Another value", "value6"

应删除较长文本中的换行符,但不应删除分隔两行的换行符.

The newlines inside of the longer piece of text should be removed, but not the newline separating the two rows.

推荐答案

在 Python 中:

In Python:

import csv
with open("input.csv", "rb") as input, open("output.csv", "wb") as output:
    w = csv.writer(output)
    for record in csv.reader(input):
        w.writerow(tuple(s.remove("\n") for s in record))

这篇关于如何从文件中所有引用的文本中删除换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆