
本文将围绕在使用 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='\')
参数解释:
通过显式地设置 csv.reader 和 csv.writer 的参数,特别是 quoting、delimiter、quotechar 和 escapechar,可以有效地控制 CSV 文件的读写行为,避免不必要的双引号包裹,并确保数据的准确性。在实际应用中,请务必根据 CSV 文件的具体格式和需求,调整这些参数的值。
以上就是解决Python csv.writer中转义字符和引用参数处理问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号