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

Python随机CSV数据生成器生成空白行问题求助

问题

项目中需要生成随机数据,编写的Python数据生成器导出CSV时出现多余空白行。

预期CSV格式(无空白行)

Canada1,CAN,66.7631001973005,327464,124083,37.208477068753076,42.055800430596044,Tree1,Sport1,Energy1
Canada2,CAN,63.42569986832592,845406,2564,99.41705285611631,58.367586898438226,Tree2,Sport2,Energy2
Canada3,CAN,80.70135666200893,697617,552814,45.0220391009484,27.55749551009966,Tree3,Sport3,Energy3
Canada4,CAN,39.91456647957388,378395,859154,49.04878526186859,58.29166138539077,Tree4,Sport4,Energy4
Canada5,CAN,23.665606706690824,27748,50323,46.099226214455754,46.02385899749339,Tree5,Sport5,Energy5
Canada6,CAN,99.11484943040361,463793,926721,22.623982535050036,68.17148628441608,Tree6,Sport6,Energy6
Canada7,CAN,22.77326723315597,197781,161989,90.07404212037031,12.51439909283495,Tree7,Sport7,Energy7
Canada8,CAN,98.05987017622658,22699,686789,79.69776439153054,88.97691373932459,Tree8,Sport8,Energy8
Canada9,CAN,74.7926432968632,973377,656871,47.67399967110805,69.30160579846151,Tree9,Sport9,Energy9
Canada10,CAN,58.42869917577311,720578,266173,62.941805567587686,75.31905104550286,Tree10,Sport10,Energy10

实际生成的CSV格式(含空白行)

Canada1,CAN,66.7631001973005,327464,124083,37.208477068753076,42.055800430596044,Tree1,Sport1,Energy1

Canada2,CAN,63.42569986832592,845406,2564,99.41705285611631,58.367586898438226,Tree2,Sport2,Energy2

Canada3,CAN,80.70135666200893,697617,552814,45.0220391009484,27.55749551009966,Tree3,Sport3,Energy3

Canada4,CAN,39.91456647957388,378395,859154,49.04878526186859,58.29166138539077,Tree4,Sport4,Energy4

Canada5,CAN,23.665606706690824,27748,50323,46.099226214455754,46.02385899749339,Tree5,Sport5,Energy5

Canada6,CAN,99.11484943040361,463793,926721,22.623982535050036,68.17148628441608,Tree6,Sport6,Energy6

Canada7,CAN,22.77326723315597,197781,161989,90.07404212037031,12.51439909283495,Tree7,Sport7,Energy7

Canada8,CAN,98.05987017622658,22699,686789,79.69776439153054,88.97691373932459,Tree8,Sport8,Energy8

Canada9,CAN,74.7926432968632,973377,656871,47.67399967110805,69.30160579846151,Tree9,Sport9,Energy9

Canada10,CAN,58.42869917577311,720578,266173,62.941805567587686,75.31905104550286,Tree10,Sport10,Energy10

生成器代码

import random
import os
import csv

# Generating random data. The car information is not important, rather the car vin number needs to be unique
def generate_data_csv(rand_file_name, no_entries):
  # rand_file_name: the name of the random 
  # no_entries: the number of entries in the file
  
  file_name = "temp.csv" # temp file to hold the generated entries
  with open(file_name, 'w', encoding='UTF8') as f:
    writefile = csv.writer(f)
    for i in range(1, no_entries+1):
      country_name=("Canada" + str(i))
      country_code="CAN"
      access_to_electricity=(random.uniform(0.0, 100.0))
      renewable_electricity_output=(random.randint(1000,1000000))
      total_electricity_output=(random.randint(1000,1000000))
      tot_final_energy_consumption=(random.uniform(0.0, 100.0))
      renewable_energy_consumption=(random.uniform(0.0, 100.0))
      national_tree=("Tree" + str(i))
      most_popular_sport=("Sport" + str(i))
      primary_energy_consumption=("Energy" + str(i))
      row = [country_name,country_code,access_to_electricity,renewable_electricity_output,total_electricity_output,tot_final_energy_consumption,renewable_energy_consumption,national_tree,most_popular_sport,primary_energy_consumption]
      writefile.writerow(row)
  # randmize the entries in the file
  with open(file_name,'r') as source:
    csvreader = csv.reader(source)
    data = [ (random.random(), line) for line in csvreader ]
  data.sort()
  with open(rand_file_name,'w') as target:
    writefile = csv.writer(target)
    for _, line in data:
        writefile.writerow(line)

  # removing the temp file, not needed anymore
  os.remove(file_name)

已尝试移除随机打乱数据的部分,但问题仍存在,寻求解决方法。

解决方案

问题原因

Python的csv.writer模块会自行处理换行符,但若打开文件时未指定newline='',系统会自动对换行符进行转换(比如Windows下把\n转成\r\n),导致csv模块输出的换行符被重复转换,最终生成多余空白行。

修复步骤

在所有写入CSV文件的open()调用中添加newline=''参数,让csv模块全权处理换行逻辑,避免系统自动转换冲突:

  1. 写入临时文件时:open(file_name, 'w', encoding='UTF8', newline='')
  2. 写入最终目标文件时:open(rand_file_name,'w', newline='')

修改后的完整代码

import random
import os
import csv

def generate_data_csv(rand_file_name, no_entries):
  file_name = "temp.csv"
  # 添加newline=''参数
  with open(file_name, 'w', encoding='UTF8', newline='') as f:
    writefile = csv.writer(f)
    for i in range(1, no_entries+1):
      country_name = f"Canada{i}"
      country_code = "CAN"
      access_to_electricity = random.uniform(0.0, 100.0)
      renewable_electricity_output = random.randint(1000, 1000000)
      total_electricity_output = random.randint(1000, 1000000)
      tot_final_energy_consumption = random.uniform(0.0, 100.0)
      renewable_energy_consumption = random.uniform(0.0, 100.0)
      national_tree = f"Tree{i}"
      most_popular_sport = f"Sport{i}"
      primary_energy_consumption = f"Energy{i}"
      row = [
          country_name, country_code, access_to_electricity,
          renewable_electricity_output, total_electricity_output,
          tot_final_energy_consumption, renewable_energy_consumption,
          national_tree, most_popular_sport, primary_energy_consumption
      ]
      writefile.writerow(row)
  
  with open(file_name,'r') as source:
    csvreader = csv.reader(source)
    data = [ (random.random(), line) for line in csvreader ]
  data.sort()
  # 添加newline=''参数
  with open(rand_file_name,'w', newline='') as target:
    writefile = csv.writer(target)
    for _, line in data:
        writefile.writerow(line)

  os.remove(file_name)

额外优化

  • 使用f-string替代字符串拼接,代码更简洁易读
  • 拆分长列表为多行,提升代码可读性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 21:29:54