使用 CRLF 而不是 LF 的 CSV 编写器 (Python)

CSV Writer (Python) with CRLF instead of LF

您好,我正在尝试使用 csv 库将我的 CSV 文件转换为新文件。

我写的代码如下:

import csv
import re

file_read=r'C:\Users\Comarch\Desktop\Test.csv'
file_write=r'C:\Users\Comarch\Desktop\Test_new.csv'

def find_txt_in_parentheses(cell_txt):
    pattern = r'\(.+\)'
    return set(re.findall(pattern, cell_txt))

with open(file_write, 'w', encoding='utf-8-sig') as file_w:
    csv_writer = csv.writer(file_w, lineterminator="\n")
    with open(file_read, 'r',encoding='utf-8-sig') as file_r:
        csv_reader = csv.reader(file_r)
        for row in csv_reader:
            cell_txt = row[0]
            txt_in_parentheses = find_txt_in_parentheses(cell_txt)
            if len(txt_in_parentheses) == 1:
                txt_in_parentheses = txt_in_parentheses.pop()
                cell_txt_new = cell_txt.replace(' ' + txt_in_parentheses,'')
                cell_txt_new = txt_in_parentheses + '\n' + cell_txt_new
                row[0] = cell_txt_new
            csv_writer.writerow(row)

唯一的问题是在生成的文件(Test_new.csv 文件)中,我有 CRLF 而不是 LF。 以下是示例图像:

因此,当我将 csv 列复制到 Google docs Excel 文件时,我在每一行后得到一个空行 CRLF.

是否可以使用 csv 库编写我的代码,以便 LF 留在单元格中而不是 CRLF

您在 Windows,并且您使用 'w' 模式打开文件 -- 这会为您提供 windows 风格的行结尾。使用模式 'wb' 应该会给你首选的行为。

来自documentation of csv.reader

If csvfile is a file object, it should be opened with newline=''1
[...]

Footnotes

1(1,2) If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline='', since the csv module does its own (universal) newline handling.

这正是您遇到的问题。所以...

with open(file_read, 'r', encoding='utf-8-sig', newline='') as file_r, \
     open(file_write, 'w', encoding='utf-8-sig', newline='') as file_w:
     
    csv_reader = csv.reader(file_r, dialect='excel')
    csv_writer = csv.writer(file_w, dialect='excel')

    # ...