You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python循环存CSV避免覆盖?多JSON对象迭代数据追加问题

Fixing CSV Append Issue in Your Loop

It looks like the main issues with your code are twofold: either you’re accidentally overwriting the file instead of appending, or you’re adding the header row multiple times (once per iteration) which might be causing confusion. Let’s break this down and fix it step by step.

Why Your Current Code Might Be Failing

If you’re only seeing the third iteration’s data in the CSV, the most likely culprit is that you were using 'w' (write mode) instead of 'a' (append mode) when opening the file. Write mode truncates the file each time you open it, so each loop overwrites the previous data—leaving only the last iteration’s results. Even if you’re using 'a', your code writes the header row three times (once for each section), which is messy and not what you want.

Corrected Approach 1: Open the File Once (Most Efficient)

Instead of opening and closing the file three times, open it once before the loop, write the header once, then append all your data in one go:

import csv

# Open the file once in write mode (creates it if it doesn't exist)
with open('Glassdoor_A.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    # Write the header row once
    writer.writerow(["Section", "Word", "Frequency"])
    
    # Loop through each section
    for section in ['Text_Main','Text_Pro','Text_Con']:
        common_words = get_top_n_trigram(df[section], 50)
        # Write all rows for this section
        writer.writerows([section, word, freq] for word, freq in common_words)
        # Optional: Print to terminal as before
        for word, freq in common_words:
            print(section, word, freq)

Corrected Approach 2: Append to Existing File (Preserve Previous Data)

If you want to add data to an existing CSV without overwriting it (and only write the header if the file is new), you can check if the file exists first:

import csv
import os

file_path = 'Glassdoor_A.csv'
# Check if the file exists
file_exists = os.path.isfile(file_path)

for section in ['Text_Main','Text_Pro','Text_Con']:
    common_words = get_top_n_trigram(df[section], 50)
    # Print to terminal
    for word, freq in common_words:
        print(section, word, freq)
    
    # Open in append mode
    with open(file_path, 'a', newline='') as file:
        writer = csv.writer(file)
        # Write header only if the file didn't exist before this run
        if not file_exists:
            writer.writerow(["Section", "Word", "Frequency"])
            # Mark file as existing so we don't write header again
            file_exists = True
        # Write rows for this section
        writer.writerows([section, word, freq] for word, freq in common_words)

Key Fixes Explained

  • Single Header Row: We write the header only once, either at the start of the file (Approach 1) or only when creating a new file (Approach 2).
  • Avoid Overwriting: Using 'w' once (Approach 1) creates the file and writes all data, while 'a' (Approach 2) adds to an existing file without truncating.
  • Efficient File Handling: Opening the file once (Approach 1) is better for performance than opening/closing it three times.

This should ensure all three iterations' data is saved in the CSV, with a single header row at the top.

内容的提问来源于stack exchange,提问作者Malte Susen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 22:57:30