我的输出没有值

2024-09-28 13:15:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我的文件包含“Name”和5个眼动值(TFF、TFD、TVD、FB、FC)。如果“名称”列下的行相同,我想对每个眼动值进行汇总。似乎代码正在运行,没有发生错误,但我的输出文件仍然是空的。谁能告诉我哪里出错了吗?代码如下:

import csv

file  = open("P01_All.csv", "r") #Open CSV File in Read Mode
reader = csv.reader(file) #Create reader object which iterates over lines
outfile = open("Name.csv","w")
outfile2 = open("TFF.csv","w")
outfile3 = open("TFD.csv","w")
outfile4 = open("TVD.csv","w")
outfile5 = open("FB.csv","w")
outfile6 = open("FC.csv","w")


class Object:                   #Object to store unique data
    def __init__(self, Name, TFF, TFD, TVD, FB, FC):
        self.Name = Name
        self.TFF = TFF
        self.TFD = TFD
        self.TVD = TVD
        self.FB = FB
        self.FC = FC



rownum = 0 #Row Number currently iterating over
list = []  #List to store objects

def checkList(Name, TFF, TFD, TVD, FB, FC):

    for object in list:  #Iterate through list        
        if object.Name == Name:  
            object.TFF += float(TFF)
            object.TFD += float(TFD)
            object.TVD += float(TVD)
            object.FB += float(FB)
            object.FC += float(FC)
            return

    newObject = Object(Name, float(TFF),float(TFD), float(TVD), float(FB), float(FC)) #Create a new object with new eye and TFF
    list.append(newObject)  #Add to list and break out

for row in reader:  #Iterate through all the rows
    if rownum == 0:  #Store header row seperately to not get confused
        header = row
    else:
        Name = row[0]
        TFF = row[1]
        TFD = row[2]
        TVD = row[3]
        FB = row[4]
        FC = row[5]

        if len(list) == 0:  #Default case if list = 0
            newObject = Object(Name, float(TFF),float(TFD), float(TVD), float(FB), float(FC))
            list.append(newObject)
        else:  #If not...
            checkList(Name, TFF, TFD, TVD, FB, FC)


rownum += 1

for each in list: #Print out result
#   print(each.Name, each.TFF, each.TFD, each.TVD, each.FB, each.FC)
    outfile.write(each.Name + "\n" )
    outfile2.write(str(each.TFF)+ "\n" )
    outfile3.write(str(each.TFD)+ "\n" )
    outfile4.write(str(each.TVD)+ "\n" )
    outfile5.write(str(each.FB)+ "\n" )
    outfile6.write(str(each.FC)+ "\n" )

file.close() #Close file
outfile.close()
outfile2.close()
outfile3.close()
outfile4.close()
outfile5.close()
outfile6.close()

Tags: csvnameselfclosefbobjectopenfloat
1条回答
网友
1楼 · 发布于 2024-09-28 13:15:04

正如@zwer所说,输出文件中没有任何内容的原因是,在迭代输入文件中的行时,没有增加rownum。通过缩进行rownum += 1,可以将它放入循环中,在循环中读取每一行。所以只要稍加修改,它看起来

import csv

file  = open("P01_All.csv", "r") #Open CSV File in Read Mode
reader = csv.reader(file) #Create reader object which iterates over lines
outfile = open("Name.csv","w")
outfile2 = open("TFF.csv","w")
outfile3 = open("TFD.csv","w")
outfile4 = open("TVD.csv","w")
outfile5 = open("FB.csv","w")
outfile6 = open("FC.csv","w")


class Movement_value:                   #Object to store unique data
    def __init__(self, Name, TFF, TFD, TVD, FB, FC):
        self.Name = Name
        self.TFF = TFF
        self.TFD = TFD
        self.TVD = TVD
        self.FB = FB
        self.FC = FC



rownum = 0 #Row Number currently iterating over
notebook = []  #List to store objects

def checkList(Name, TFF, TFD, TVD, FB, FC):

    for value in notebook:  #Iterate through list        
        if value.Name == Name:  
            value.TFF += float(TFF)
            value.TFD += float(TFD)
            value.TVD += float(TVD)
            value.FB += float(FB)
            value.FC += float(FC)
            return

    newObject = Movement_value(Name, float(TFF),float(TFD), float(TVD), float(FB), float(FC)) #Create a new object with new eye and TFF
    notebook.append(newObject)  #Add to list and break out

for row in reader:  #Iterate through all the rows
    if rownum == 0:  #Store header row seperately to not get confused
        header = row
    else:
        Name = row[0]
        TFF = row[1]
        TFD = row[2]
        TVD = row[3]
        FB = row[4]
        FC = row[5]

        if len(notebook) == 0:  #Default case if list = 0
            newObject = Movement_value(Name, float(TFF),float(TFD), float(TVD), float(FB), float(FC))
            notebook.append(newObject)
        else:  #If not...
            checkList(Name, TFF, TFD, TVD, FB, FC)


    rownum += 1

for each in notebook: #Print out result
#   print(each.Name, each.TFF, each.TFD, each.TVD, each.FB, each.FC)
    outfile.write(each.Name + "\n" )
    outfile2.write(str(each.TFF)+ "\n" )
    outfile3.write(str(each.TFD)+ "\n" )
    outfile4.write(str(each.TVD)+ "\n" )
    outfile5.write(str(each.FB)+ "\n" )
    outfile6.write(str(each.FC)+ "\n" )

file.close() #Close file
outfile.close()
outfile2.close()
outfile3.close()
outfile4.close()
outfile5.close()
outfile6.close()

我做了一些额外的修改:最好不要使用^{}^{}作为变量名,因为它们已经在Python中使用过了,这样做可以覆盖它们的含义。你最终可能会有一个坏的惊喜。你知道吗

但我们可以做得更多。你知道吗

  • 我们不需要创建一个类来保存这些值
  • 我们可以使用上下文管理器来处理文件,以确保我们的文件不会因为不相关的原因而保持打开状态。你知道吗

这里有一个版本比你的短:

import csv
import pathlib

input_filepath = pathlib.Path("Input.csv")
output_filepath = pathlib.Path("")

with open(input_filepath, newline="") as input_file:
    # Where our data will be kept
    input_data = {}

    csv_reader = csv.reader(input_file)
    # Skip the first line
    next(csv_reader)

    for (Name, *rest_of_data) in csv_reader:
        if Name in input_data:
            for (index_of_data_to_update, data_to_update) in enumerate(rest_of_data):
                input_data[Name][index_of_data_to_update] += float(data_to_update)
        else:
            input_data[Name] = [float(x) for x in rest_of_data]

output_rows = ([name] + list(data) for (name, data) in input_data.items())

output_filenames = [
    "Name.csv",
    "TFF.csv",
    "TFD.csv",
    "TVD.csv",
    "FB.csv",
    "FC.csv"
    ]

output_files = [open(output_filepath / filename, "w") for filename in output_filenames]

# Open all the files
with output_files[0], output_files[1], output_files[2], output_files[3], \
     output_files[4], output_files[5]:
    for row in output_rows:
        for (output_file, data) in zip(output_files, row):
            output_file.write("{}\n".format(data))

相关问题 更多 >

    热门问题