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

解决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)

关键代码:

立即学习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 文件的具体格式和需求,调整这些参数的值。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享