向输出中添加另一列

2024-04-18 00:16:57 发布

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

非常感谢您抽出时间。很抱歉用我的编程技巧来打扰你,但是我在过去的两天里花了很多时间来测试和寻找一种方法来解决我的问题,我似乎只是缺少一个上下文来确定我认为应该是一个简单的解决方案。一个现在在外地的朋友为我写了一个简短的python脚本(复制到下面),它可以输入任意数量的.csv文件,这些文件具有不同但重叠的索引和索引值。然后,脚本请求在所有输入文件的索引值之间选择每个索引的最大值,并创建一个包含所有索引和每个索引的最大值的compilation.csv。我想在output.csv中添加第三列,显示每个最高值的源文件名。我认为我的问题不同于这个问题https://stackoverflow.com/questions/35005798/adding-another-column-to-a-csv-file-w-python,因为索引和文件名信息之间没有一一对应的关系—所需的文件名输出取决于最大值。文件名的来源是文件名本身还是手动输入到input.csvs中的第三列(预先显示每行的文件名)对我来说并不重要,如果两个文件对同一索引具有相同的值,则输出什么文件名也不重要。尽管尝试了我能想到的一切,但我并没有成功地将这个输出添加到脚本中。非常感谢!!你知道吗

import csv
import sys
import operator
import numpy

filenames = [
    "All_Culverts_K.csv",
    "All_Culverts5817.csv",
    "All_culverts_5.2.csv",
    "All_Culverts.csv",
    "All_CulvertsCopy.csv"]

output = "All_Culverts_Run_5.11_Max_Areas3.csv"

maxAreas = [None] * 3000

for filename in filenames:
     try:
        with open(filename, 'r') as csv_file:
            input_table = csv.reader(csv_file)

            # Get rid of header
            header_row = next(input_table)

            row_number = 0

            # Go through all rows in the table after the header.
            for row in input_table:

                try:
                    ws_index = row[0].index('ws')

                    index = int(row[0][:ws_index])
                    value = float(row[1])



                    if (maxAreas[index] == None):
                        maxAreas[index] = value


                    else:
                        if (maxAreas[index] < value) :
                            maxAreas[index] = value

                except ValueError:
                    print "Error, missing ws on row " + str(row_number)



                row_number += 1

        csv_file.close()


   except IOError:
        print "ERROR: Could not find file '" \
            + filename \
            + "'. Bailing out."
        sys.exit(0)

# Write the maximums.
f_out = open(output, 'wb')
csv_writer = csv.writer(f_out)
csv_writer.writerow(['BarrierID', 'Area_sqkm', 'Source_file'])

row_number = 0

for area in maxAreas:
    csv_writer.writerow([str(row_number) + 'ws', area])
    row_number += 1

print "Done! View .csv in folder."

f_out.close()

到目前为止我试过什么? -向input.csvs添加第三列以显示源文件 -正在创建源文件变量 -将源文件输入附加到if语句 -将源文件变量添加到writerow命令 -大量的谷歌搜索和阅读python文档


Tags: 文件csvinimportnumberinputindexws
1条回答
网友
1楼 · 发布于 2024-04-18 00:16:57

这能解决你的问题吗?你知道吗

import csv
import sys
import operator
import numpy

filenames = [
    "All_Culverts_K.csv",
    "All_Culverts5817.csv",
    "All_culverts_5.2.csv",
    "All_Culverts.csv",
    "All_CulvertsCopy.csv"]

output = "All_Culverts_Run_5.11_Max_Areas3.csv"

maxAreas = [None] * 3000

for filename in filenames:
     try:
        with open(filename, 'r') as csv_file:
            input_table = csv.reader(csv_file)

            # Get rid of header
            header_row = next(input_table)

            row_number = 0

            # Go through all rows in the table after the header.
            for row in input_table:

                try:
                    ws_index = row[0].index('ws')

                    index = int(row[0][:ws_index])
                    value = float(row[1])


    ##modification nr.1: use keyword is when checking for None
                    if (maxAreas[index] is None):
    ##modification nr.2: store a tuple instead of just the value
                        maxAreas[index] = (value, filename)


                    else:
    ##modification nr.3: use the numerical value in the stored tuple by adding [0]  
                        if (maxAreas[index][0] < value) :
    ##modification nr.4: store a tuple instead of just the value    
                            maxAreas[index] = (value, filename)

                except ValueError:
                    print "Error, missing ws on row " + str(row_number)



                row_number += 1

        csv_file.close()


   except IOError:
        print "ERROR: Could not find file '" \
            + filename \
            + "'. Bailing out."
        sys.exit(0)

# Write the maximums.
f_out = open(output, 'wb')
csv_writer = csv.writer(f_out)
csv_writer.writerow(['BarrierID', 'Area_sqkm', 'Source_file'])

row_number = 0

#modification nr. 5: unpack the tuple into area and filename1 when iterating
#through maxAreas; use filename1 instead of filename to catch possible errors
for area, filename1 in maxAreas:
#modification nr.6: store the additional filename1
    csv_writer.writerow([str(row_number) + 'ws', area, filename1])
    row_number += 1

print "Done! View .csv in folder."

f_out.close()

代码与原始代码几乎相同,但在确定每个索引的最大值的地方,我存储了一个元组(maxArea, filename),以保留在哪个文件中找到最大值的信息。最后,我从maxAreas中解压这两个值,并根据Jean-François Fabre的注释将附加行添加到csv文件中。我必须承认,我没有csv的经验,所以有一个机会,我得到它完全错误的。你知道吗

相关问题 更多 >

    热门问题