Python:在多行中将循环结果写入csv [英] Python: write a result for loop to csv in multiple row

查看:1015
本文介绍了Python:在多行中将循环结果写入csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我想在csv文件中写入2500个数字列表。

从下面的代码中,我只设法将列表写成一行,共有2500列。我的期望是有25列,每25个数字后,它会开始写入下一行。



预计:

0.81 0.87 0.17 0.31 ......... 0.76(25th)

0.31 ........................ .0.10(50th)



我尝试过:



Hi everyone!

I want to write a list of 2500 numbers into csv file.
From the code below, I only manage to get the list written in one row with 2500 columns in total. My expectation is to have 25 columns, where after every 25 numbers, it will begin to write into the next row.

Expected:
0.81 0.87 0.17 0.31......... 0.76(25th)
0.31.........................0.10(50th)

What I have tried:

csv_output = csv.writer(open("c.csv", "wb"))
    csv_output.writerow(["Col1","Col2","Col3","Col4","Col5","Col6","Col7","Col8","Col9","Col10","Col11","Col12","Col13","Col14","Col15","Col16","Col17","Col18","Col19","Col20","Col21","Col22","Col23","Col24","Col25"])




    i=0
    for row in csv_f2:
        print ("Superpixel features",row)
        a = float(row[0])

        for row2 in csv_f1:
                b = float(row2[i])
                c = (b-a)
                print ("%.8f, %.8f, %.8f" % (a, b, c))
                # save the feature vector using CSV 
                outputf = ([c])
                outf.append(outputf)
                csv_output.writerow(outf)
        f1.seek(0)
        i=i+1

推荐答案

Quote:

从下面的代码中,我只设法将列表写在一行中总共2500列。我的期望是有25列,每25个数字之后,它将开始写入下一行。

From the code below, I only manage to get the list written in one row with 2500 columns in total. My expectation is to have 25 columns, where after every 25 numbers, it will begin to write into the next row.



你需要计算你输出的数字,每次你到达25,在输出中添加换行符。

这对你来说有问题吗?


You need to count the numbers you outputs, and every time you reach 25, add a newline to output.
Is it a problem for you ?


这篇关于Python:在多行中将循环结果写入csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆