解决Python csv.writer中转义字符和引用参数处理问题

霞舞
发布: 2025-07-10 18:14:28
原创
349人浏览过

解决python csv.writer中转义字符和引用参数处理问题

本文将围绕在使用 Python 的 csv.writer 模块时,如何避免输出内容被双引号包裹的问题展开讨论。通过分析常见错误和提供正确的代码示例,帮助开发者理解 csv.writer 的参数配置,特别是 delimiter、quotechar、escapechar 和 quoting 的作用,从而实现对 CSV 文件内容的精确控制。

问题分析

在使用 csv.writer 时,如果不正确设置参数,可能会导致输出的 CSV 文件中的某些字段被双引号包裹,这通常是因为 csv.writer 默认启用了引用(quoting)机制。为了解决这个问题,我们需要显式地禁用引用,并正确设置分隔符和转义字符。

解决方案

核心在于正确配置 csv.reader 和 csv.writer 的参数。以下是修改后的代码,展示了如何禁用引用并指定分隔符和转义字符:

import csv, io
import os, shutil

result = {}

csv_file_path = 'myreport.csv'
columns_to_process = ['money1', 'money2']
string_to_be_replaced = "."
string_to_replace_with = ","
mydelimiter =  ";"

# 检查文件是否存在
if not os.path.isfile(csv_file_path):
    raise IOError("csv_file_path is not valid or does not exists: {}".format(csv_file_path))

# 检查分隔符是否存在
with open(csv_file_path, 'r') as csvfile:
    first_line = csvfile.readline()
    if mydelimiter not in first_line:
        delimiter_warning_message = "No delimiter found in file first line."
        result['warning_messages'].append(delimiter_warning_message)

# 统计文件行数
NOL = sum(1 for _ in io.open(csv_file_path, "r"))

if NOL > 0:
    # 获取列名
    with open(csv_file_path, 'r') as csvfile:
        columnslist = csv.DictReader(csvfile, delimiter=mydelimiter)      
        list_of_dictcolumns = []
        for row in columnslist:
            list_of_dictcolumns.append(row)
            break  

    first_dictcolumn = list_of_dictcolumns[0]        
    list_of_column_names = list(first_dictcolumn.keys())
    number_of_columns = len(list_of_column_names)

    # 检查列是否存在
    column_existence = [ (column_name in list_of_column_names ) for column_name in columns_to_process ]

    if not all(column_existence):
        raise ValueError("File {} does not contains all the columns given in input for processing:
File columns names: {}
Input columns names: {}".format(csv_file_path, list_of_column_names, columns_to_process))

    # 确定要处理的列的索引
    indexes_of_columns_to_process = [i for i, column_name in enumerate(list_of_column_names) if column_name in columns_to_process]

    print("indexes_of_columns_to_process: ", indexes_of_columns_to_process)

    # 构建输出文件路径
    inputcsv_absname, inputcsv_extension = os.path.splitext(csv_file_path)
    csv_output_file_path = inputcsv_absname + '__output' + inputcsv_extension

    # 定义处理函数
    def replace_string_in_columns(input_csv, output_csv, indexes_of_columns_to_process, string_to_be_replaced, string_to_replace_with):
        number_of_replacements = 0

        with open(input_csv, 'r', newline='') as infile, open(output_csv, 'w', newline='') as outfile:
            reader = csv.reader(infile, quoting=csv.QUOTE_NONE, delimiter=mydelimiter, quotechar='',escapechar='\')
            writer = csv.writer(outfile, quoting=csv.QUOTE_NONE, delimiter=mydelimiter, quotechar='',escapechar='\')

            row_index=0

            for row in reader:              
                for col_index in indexes_of_columns_to_process:
                    # 跳过空行
                    if len(row) == 0:
                        break

                    cell = row[col_index]
                    columns_before = row[:col_index]
                    columns_after = row[(col_index + 1):]

                    print("col_index: ", col_index)
                    print("row: ", row)
                    print("cell: ", cell)

                    if string_to_be_replaced in cell and row_index != 0:                        
                        # 替换字符串
                        cell = cell.replace(string_to_be_replaced, string_to_replace_with)
                        number_of_replacements = number_of_replacements + 1
                        print("number_of_replacements: ", number_of_replacements)

                        row_replaced = columns_before + [ cell ] + columns_after
                        row = row_replaced

                # 写入行
                writer.writerow(row)
                print("written row: ", row, "index: ", row_index)

                row_index=row_index+1

        return number_of_replacements 

    # 执行处理函数
    result['number_of_modified_cells'] =  replace_string_in_columns(csv_file_path, csv_output_file_path, indexes_of_columns_to_process, string_to_be_replaced, string_to_replace_with)

    # 替换原始文件
    shutil.copyfile(csv_output_file_path, csv_file_path)
    os.remove(csv_output_file_path)

    if result['number_of_modified_cells'] > 0:
        result['changed'] = True
    else:
        result['changed'] = False
else:
    result['changed'] = False

result['source_csv_number_of_raw_lines'] = NOL
result['source_csv_number_of_lines'] = NOL - 1

print("result:

", result)
登录后复制

关键代码:

千面视频动捕
千面视频动捕

千面视频动捕是一个AI视频动捕解决方案,专注于将视频中的人体关节二维信息转化为三维模型动作。

千面视频动捕 173
查看详情 千面视频动捕

立即学习Python免费学习笔记(深入)”;

reader = csv.reader(infile, quoting=csv.QUOTE_NONE, delimiter=mydelimiter, quotechar='',escapechar='\')
writer = csv.writer(outfile, quoting=csv.QUOTE_NONE, delimiter=mydelimiter, quotechar='',escapechar='\')
登录后复制

参数解释:

  • delimiter: 指定字段之间的分隔符。
  • quotechar: 指定用于包围包含特殊字符(例如分隔符)的字段的字符。 设置为空字符串 '' 意味着没有引用字符。
  • escapechar: 指定用于转义分隔符的字符,当 quoting 设置为 csv.QUOTE_NONE 时,此参数仍然有用。
  • quoting: 控制何时应该生成引用。 csv.QUOTE_NONE 指示 writer 永远不要引用字段。

注意事项

  • 确保 delimiter 参数与 CSV 文件中实际使用的分隔符一致。
  • 如果 CSV 文件中的字段包含分隔符,并且你希望禁用引用,则需要使用 escapechar 来转义分隔符。
  • 在处理大型 CSV 文件时,可以考虑使用 csv.DictReader 和 csv.DictWriter 来提高代码的可读性和维护性。

总结

通过显式地设置 csv.reader 和 csv.writer 的参数,特别是 quoting、delimiter、quotechar 和 escapechar,可以有效地控制 CSV 文件的读写行为,避免不必要的双引号包裹,并确保数据的准确性。在实际应用中,请务必根据 CSV 文件的具体格式和需求,调整这些参数的值。

以上就是解决Python csv.writer中转义字符和引用参数处理问题的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号