Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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 将多个列表写入单个列_Python_List_Csv - Fatal编程技术网

Python 将多个列表写入单个列

Python 将多个列表写入单个列,python,list,csv,Python,List,Csv,我想读取这样的2列文件: 13 4.7 28 然后返回一个单列文件,如下所示: 3 7 7 7 7 8 八, 这是我的密码: import csv array1 = [] array2 = [] with open('test.txt', 'rb') as f: reader = csv.reader(f, delimiter=' ') for row in reader: array1.append(row[0]) array2.append(r

我想读取这样的2列文件:

13
4.7
28

然后返回一个单列文件,如下所示:

3
7
7
7
7
8
八,

这是我的密码:

import csv
array1 = []
array2 = []
with open('test.txt', 'rb') as f:
    reader = csv.reader(f, delimiter=' ')
    for row in reader:
        array1.append(row[0])
        array2.append(row[1])
range1 = len(array1)
array1 = [int(x) for x in array1]
array2 = [int(x) for x in array2]

outfile = open('drp', 'wb')
writer=csv.writer(outfile)

for s in range(0, range1, 1):
            res = [array2[s]] * array1[s]
            print res
            writer.writerow([res])
outfile.close()  
除了最后的写作步骤外,它似乎在起作用。我在输出文件中得到的是:

[3]  
"[7, 7, 7, 7]"  
"[8, 8]"

显然,我没有正确使用csv编写器。有什么建议吗?谢谢。

以二进制模式或使用csv阅读文本文件有什么特殊原因吗

with open('test.txt', 'r') as f, open('drp','w') as ff::
     for l in f:
         a,b=l.split(' ')
         for i in range(int(a)):
             ff.write(b+'\n')

您以二进制模式或使用csv读取文本文件有什么特殊原因吗

with open('test.txt', 'r') as f, open('drp','w') as ff::
     for l in f:
         a,b=l.split(' ')
         for i in range(int(a)):
             ff.write(b+'\n')
这应该可以

with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
    for line in infile:
        mult, val = line.strip().split()
        outfile.write('\n'.join([val for _ in xrange(int(mult))]))
        outfile.write('\n')
这应该可以

with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
    for line in infile:
        mult, val = line.strip().split()
        outfile.write('\n'.join([val for _ in xrange(int(mult))]))
        outfile.write('\n')

你把这个简单的问题复杂化了:

import csv
with open('test.txt') as f, open('out.csv','w') as f2:
    writer = csv.writer(f2, delimiter = '\n')
    for line in f:
        x, y = line.split() 
        writer.writerow([y]*int(x))
输出:

>>> cat out.csv
3
7
7
7
7
8
8

你把这个简单的问题复杂化了:

import csv
with open('test.txt') as f, open('out.csv','w') as f2:
    writer = csv.writer(f2, delimiter = '\n')
    for line in f:
        x, y = line.split() 
        writer.writerow([y]*int(x))
输出:

>>> cat out.csv
3
7
7
7
7
8
8
另一种选择

from itertools import chain
with open('test.txt') as f:
    z = (line.split() for line in f)
    print '\n'.join(chain.from_iterable(int(x)*[y] for (x, y) in z))
另一种选择

from itertools import chain
with open('test.txt') as f:
    z = (line.split() for line in f)
    print '\n'.join(chain.from_iterable(int(x)*[y] for (x, y) in z))