Some incorrect condition happens when change a element of list

import csv

import csv


def process_csv(input_file_path, output_file_path):
    # 使用只读模式打开CSV文件
    with open(input_file_path, mode='r', newline='', encoding='utf-8') as file:
        reader = csv.reader(file)  # 创建一个CSV读取器
        headers = next(reader)  # 读取第一行作为标题行
        output_rows = []  # 初始化一个列表来存储最终要写入新CSV的行
        current_row = []  # 初始化用于存储当前处理的行的列表

        list_intent = []
        # 遍历CSV文件中的每一行
        for row in reader:

            # 检查是否为新的First Question
            if row[4] == 'First Question':
                # 如果current_row非空,则表示之前的First Question处理完毕,需要保存
                if current_row:  # 如果列表不为空,那么为真
                    
                    for i in range(len(list_intent)):
                        # print(current_row[i][3])
                        current_row[3] = list_intent[i]
                        # print(output_rows)
                        output_rows.append(current_row)
                # print(111)
                # 开始新的First Question的处理
                current_row = row

                list_intent = []
                list_intent.append(row[3])
            # 检查是否为当前First Question的Followup Question
            elif row[4] == 'Followup Question' and current_row:
                # 将Followup Question的内容合并到当前First Question的answer中
                current_row[2] += f" | question:{row[1]}| answer: {row[2]}"
                # 更新Intent字段,避免重复添加相同的intent
                # 这里假设intent可以简单拼接,实际处理可能需要更复杂的逻辑以避免重复

                list_intent.append(row[3])
        # # 确保最后一组First Question也被写入到output_rows
        if current_row:
            if current_row:  # 如果列表不为空,那么为真
                    for i in range(len(list_intent)):
                        current_row[3] = list_intent[i]

                        
                        output_rows.append(current_row)
            output_rows.append(current_row)

    # print(output_rows[0][3])
    # print(output_rows[1][3])
    # 使用写模式打开新的CSV文件
    with open(output_file_path, mode='w', newline='', encoding='utf-8') as file:
        writer = csv.writer(file)  # 创建一个CSV写入器
        writer.writerow(headers)  # 写入标题行

        writer.writerows(output_rows)  # 写入处理后的数据行


# 指定输入和输出文件路径
input_file_path = r'c:\Users\admin\Desktop\医疗\GPT3.5\BOT_4\HelpBotChat2.csv'
output_file_path = r'c:\Users\admin\Desktop\医疗\GPT3.5\BOT_4\HelpBotChat2_gather_6.csv'

# 调用函数
process_csv(input_file_path, output_file_path)

this part

when I use current_row[3] = list_intent[i] to change the element of current_row,and actually current_row has been changed. But output_rows can’t be put correct current_row. The content from correct_row is original, instead of the content after making current_row[3] = list_intent[i].
my python version is 3.11

if current_row: # 如果列表不为空,那么为真
for i in range(len(list_intent)):
current_row[3] = list_intent[i]

                    output_rows.append(current_row)

You need to clone current_row then append to output_rows. Either deep copy, or create a new list from current_row.

1 Like

good idea. I am not sure whether this condition is a characyer or bug

Please see:

1 Like

tks 1234567890