将数据写入CSV中的一行(该行中的一列除外),并将数据插入该空列

将数据写入CSV中的一行(该行中的一列除外),并将数据插入该空列,csv,floating-point,row,string,writing,Csv,Floating Point,Row,String,Writing,当前正在尝试将单个数据项添加到CSV中的行中,但它一直将其添加到正确的fieldvalue下面。我需要用浮子吗?我真的不确定。任何帮助都将不胜感激。 这段代码基本上是试图漏掉当前数据,然后在行中的空csv列中插入一个变量 writer.writerow([""]+[""]+[""]+[""]+[""]+[""]+[""]+[""]+[""]+[""]+[""]+[""]+[""]+[cweight]) 如果我正确理解您的问题,则您有以下类型的csv数据 Field1, Field2, Fiel

当前正在尝试将单个数据项添加到CSV中的行中,但它一直将其添加到正确的fieldvalue下面。我需要用浮子吗?我真的不确定。任何帮助都将不胜感激。 这段代码基本上是试图漏掉当前数据,然后在行中的空csv列中插入一个变量

writer.writerow([""]+[""]+[""]+[""]+[""]+[""]+[""]+[""]+[""]+[""]+[""]+[""]+[""]+[cweight])

如果我正确理解您的问题,则您有以下类型的csv数据

Field1, Field2, Field3, Field4
Value1,Value2,,Value4
您希望使用.writerow函数来实现以下操作

Field1, Field2, Field3, Field4
Value1,Value2,Value3,Value4
但是相反,你的结果一直是

Field1, Field2, Field3, Field4
Value1,Value2,,Value4
,,Value3,
如果是这种情况,则可以使用csv.reader读入值,将其分配到正确的索引位置,并使用csv.writer将结果写入新文件

import csv

# Open the input and output csv files
with open(example_input.csv, "rb") as csvFileInput:
    with open(example_output.csv, "wb") as csvFileOutput:

# Set the reader on the input csv
# Set the writer on the output csv
    reader = csv.reader(csvFileInput)
    writer = csv.writer(csvFileOutput)

# read the first row and assign the field names from the reader to a variable
    fields = reader.next()

# write the field names to the new output csv.
    writer.writerow(fields)

# Read the next line with data values from the input csv
    row = reader.next()

# read the index position of the empty (or replaceable) data value from input csv
# Assign the row value to a new value
# Write the new row to the output csv
    row[2] = 'Value3'
    writer.writerow(row)

我认为你需要提供更多的背景。如果我对结束提问感兴趣,我会投票结束这个问题,因为我不清楚你在问什么。