Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 写入csv文件(python)中同一行的列_Python 2.7_Csv - Fatal编程技术网

Python 2.7 写入csv文件(python)中同一行的列

Python 2.7 写入csv文件(python)中同一行的列,python-2.7,csv,Python 2.7,Csv,我正在尝试将值写入csv文件,以便每两次迭代,结果都在同一行中,然后下一次将值打印到新行。任何帮助都将不胜感激。非常感谢。 这就是我到目前为止所做的: import csv import math savePath = '/home/dehaoliu/opencv_test/Engineering_drawings_outputs/' with open(str(savePath) +'outputsTest.csv','w') as f1: writer=csv.writer

我正在尝试将值写入csv文件,以便每两次迭代,结果都在同一行中,然后下一次将值打印到新行。任何帮助都将不胜感激。非常感谢。 这就是我到目前为止所做的:

import csv
import math
savePath = '/home/dehaoliu/opencv_test/Engineering_drawings_outputs/'

with open(str(savePath) +'outputsTest.csv','w') as f1:
        writer=csv.writer(f1, delimiter='\t',lineterminator='\n',)
        temp = []
 for k in range(0,2):
        temp = []            
       for i in range(0,4):
            a = 2 +i
            b = 3+ i
            list = [a,b]
            temp.append(list)
       writer.writerow(temp)
我现在得到的结果是

[2 3][3 4][4 5][5 6]
[2 3][3 4][4 5][5 6]
但是我想得到这样一个结果(不带括号),行中的每个数字都在一个单独的列中:

2 3 3 4
4 5 5 6
请尝试以下操作:

import csv
import math
savePath = '/home/dehaoliu/opencv_test/Engineering_drawings_outputs/'

with open(str(savePath) +'outputsTest.csv','w') as f1:
        writer=csv.writer(f1, delimiter='\t',lineterminator='\n',)
        temp = [2, 3]
        for i in range(2):
            temp = [x + i for x in temp]
            additional = [y+1 for y in temp]
            writer.writerow(temp + additional)
            temp = additional[:]
这应返回:

# 2    3    3   4
# 4    5    5   6
您可以从包含数字
2
3
的临时列表开始。然后,从
0
循环到
2
(不包括)。在每次迭代中,您将临时列表的值按当前索引递增,然后使用临时列表的这些新值创建一个附加列表。完成后,将两个列表合并在一起,并将结果写入文件。此时,您可以将临时列表设置为等于附加列表的值,然后再进行下一次迭代

我希望这能有所帮助。

尝试以下方法:

import csv
import math
savePath = '/home/dehaoliu/opencv_test/Engineering_drawings_outputs/'

with open(str(savePath) +'outputsTest.csv','w') as f1:
        writer=csv.writer(f1, delimiter='\t',lineterminator='\n',)
        temp = [2, 3]
        for i in range(2):
            temp = [x + i for x in temp]
            additional = [y+1 for y in temp]
            writer.writerow(temp + additional)
            temp = additional[:]
这应返回:

# 2    3    3   4
# 4    5    5   6
您可以从包含数字
2
3
的临时列表开始。然后,从
0
循环到
2
(不包括)。在每次迭代中,您将临时列表的值按当前索引递增,然后使用临时列表的这些新值创建一个附加列表。完成后,将两个列表合并在一起,并将结果写入文件。此时,您可以将临时列表设置为等于附加列表的值,然后再进行下一次迭代


我希望这会有所帮助。

您可以通过一个简单的种子和增量来展示它:

import csv
import os

save_path = "/home/dehaoliu/opencv_test/Engineering_drawings_outputs/"

with open(os.path.join(save_path, "outputsTest.csv"), "w") as f:
    writer = csv.writer(f, delimiter="\t", lineterminator="\n")
    temp = [2, 3, 3, 4]  # init seed
    increment = len(temp) // 2  # how many pairs we have, used to increase our seed each row
    for _ in range(2):  # how many rows do you need, any positive integer will do
        writer.writerow(temp)  # write the current value
        temp = [x + increment for x in temp]  # add 'increment' to the elements
导致:

2 3 3 4 4 5 5 6
您可以通过一个简单的种子和增量来呈现它:

import csv
import os

save_path = "/home/dehaoliu/opencv_test/Engineering_drawings_outputs/"

with open(os.path.join(save_path, "outputsTest.csv"), "w") as f:
    writer = csv.writer(f, delimiter="\t", lineterminator="\n")
    temp = [2, 3, 3, 4]  # init seed
    increment = len(temp) // 2  # how many pairs we have, used to increase our seed each row
    for _ in range(2):  # how many rows do you need, any positive integer will do
        writer.writerow(temp)  # write the current value
        temp = [x + increment for x in temp]  # add 'increment' to the elements
导致:

2 3 3 4 4 5 5 6