Python CSV writerow到已打开fi中的特定列

2024-04-25 15:13:19 发布

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

我正在努力使用csv模块和writerow方法。你知道吗

注意:这是简化的代码,因为我可以。我请求理解。 我尽我所能提供了Minimal, Complete, and Verifiable example。你知道吗

我得到的是:

数据库中有三个表:

MODEL_test - contain data on which algorithm will learn

my_prediction - contain unseen data on which algorithm will be applied

OUT_predictions - contain output from algorithm predict method

在第一步中,我创建一个新的CSV文件并保持打开状态,直到当前算法的头韵完成。在训练迭代开始之前,我将CSV文件行与不可见表数据中的前7个值相加,这样数据就不会相乘。然后在每次算法迭代之后,我想用OUT_prediction值附加已经打开的文件。你知道吗

代码:

import csv
import datetime

def export_to_csv():

    ldb = sqlite3.connect('database.db')
    c = ldb.cursor()

    table_name = 'my_predictions'

    training_size = 3

    now = datetime.datetime.now()
    file_name = str.format('my_predictions {}', now.strftime("%Y-%m-%d %H %M %S"))

    export_columns = ['COLUMN ' + str(n) for n in range(1, 8)] + \
                     ['OUTPUT ' + str(n) for n in range(1, training_size + 1)]

    with open('archived/' + file_name + '.csv', 'w', newline='') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(export_columns)
        output_writer = csv.DictWriter(csv_file, fieldnames=export_columns)

        for o in range(1, 500): # < write all unseen data from database to csv

            c.execute(str.format('SELECT * FROM {} WHERE ID=?', table_name), [o])
            fetch_one = c.fetchone()

            writer.writerow(fetch_one[1:7])

        for t in range(training_size): #for each iteration write output to csv

            # some machine learning training code

            prediction = [0, 0, 1, 1, 0, 1] # <-- sample output from predictions

            combined_set = list(map(str, prediction))

            ids = 1

            for each in combined_set:
                c.execute(str.format('INSERT INTO OUTPUT_prediction VALUES ({})',
                                     ",".join(["?" for _ in range(1, len([ids] + [int(each)]) + 1)])), [ids] + [int(each)])

                ids += 1

            ldb.commit()

            for o in range(1, 500): # <-- write down output from last prediction iteration to specific column
                c.execute(str.format('SELECT * FROM {} WHERE ID=?', table_name), [o])
                fetch_output = c.fetchone()

                output_writer.writeheader()
                output_writer.writerow({'OUTPUT ' + str(t + 1): fetch_output[-1]})  # <-- columns remain empty

有什么问题

当代码完成并打开文件时,我可以看到输出列仍然是空的

CSV IMAGE

编辑:我不想使用pandasto_csv,因为它们非常慢。有时我看不见的数据有一百万行,使用to_csv进行一次迭代需要半个小时。你知道吗


Tags: 文件csvtonameinfromforoutput
1条回答
网友
1楼 · 发布于 2024-04-25 15:13:19

我知道我做错了什么,找到了解决这个问题的办法,但我并不满意。当我尝试在w模式下添加新列时,新数据总是写在文件的末尾。当我设置csv_file.seek(0)时,旧数据被覆盖

我还尝试以r+模式重新打开文件并设置csv_file.seek(0),但得到了相同的结果

我将在这个任务中使用xlwings,因为它给了我更多的控制,但仍然不知道它将如何影响输入数据的速度。我的目标是用看不见的数据、每次迭代的输出和统计信息准备总结报告

溶液(用r+):

now = datetime.datetime.now()
file_name = str.format('my_predictions {}', now.strftime("%Y-%m-%d %H %M %S"))

export_columns = ['COLUMN ' + str(n) for n in range(1, 8)] + \
                 ['OUTPUT ' + str(n) for n in range(1, training_size + 1)]


with open('archived/' + file_name + '.csv', 'w', newline='') as csv_file:

    writer = csv.writer(csv_file)
    writer.writerow(export_columns)

    for o in range(1, 500):

        c.execute(str.format('SELECT * FROM {} WHERE ID=?', table_name), [o])
        fetch_one = c.fetchone()

        writer.writerow(fetch_one[1:7])

for t in range(training_size):

    # some machine learning training code

    prediction = [0, 0, 1, 1, 0, 1] # <  sample output from predictions

    combined_set = List(Map(Str, prediction))

    # ids = 1
    #
    # for each in combined_set:
    #    c.execute(str.format('INSERT INTO OUTPUT_prediction VALUES ({})',
    #                         ",".join(["?" for _ in range(1, len([ids] + [int(each)]) + 1)])), [ids] + [int(each)])
    #
    #    ids += 1
    #
    # ldb.commit()

    with open('archived/' + file_name + '.csv', 'r+', newline='') as csv_file:

        writer = csv.writer(csv_file)
        csv_input = csv.reader(csv_file)
        rows = List(csv_input)
        writer.writerow(export_columns)

        for row, o in zip(rows, combined_set):

            row += [o]

            writer.writerow(row)

相关问题 更多 >