Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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

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 如何打印csv文件中转置的数组列表_Python_Python 2.7_Csv_Transpose - Fatal编程技术网

Python 如何打印csv文件中转置的数组列表

Python 如何打印csv文件中转置的数组列表,python,python-2.7,csv,transpose,Python,Python 2.7,Csv,Transpose,我有一个列表,其中包含以下形式的数组(type=numpy.ndarray): final = [[a, b, c, d],[e, f, g, h],[i, j, k, l]] a, e, i b, f, j c, g, k d, h, l 我想将它们转换成csv文件,以以下形式打印: final = [[a, b, c, d],[e, f, g, h],[i, j, k, l]] a, e, i b, f, j c, g, k d, h, l 其中a,b,c…j,k,l都是字符串(nu

我有一个列表,其中包含以下形式的数组(type=numpy.ndarray):

final = [[a, b, c, d],[e, f, g, h],[i, j, k, l]]
a, e, i
b, f, j
c, g, k
d, h, l
我想将它们转换成csv文件,以以下形式打印:

final = [[a, b, c, d],[e, f, g, h],[i, j, k, l]]
a, e, i
b, f, j
c, g, k
d, h, l
其中a,b,c…j,k,l都是字符串(numpy.string_u2;)。 我试图将其作为一个包含列表的列表来处理(另一篇文章对此进行了回答),但它不起作用,而是创建了一个空的csv文件。 我的尝试是这样的:

csvfile=open('new.csv','wb')    
wr = csv.writer(csvfile)
final=map(list, zip(*final))
wr.writerows(final)
csvfile.close()
有人能提供一些建议吗

试试这个:

with open('new.csv', 'wb') as f:
    wr = csv.writer(f)
    wr.writerows(map(list, zip(*final)))

您可以将列表转换为numpy数组:
final=np.array(final)
然后使用转置方法
numpy.ndarray.T

final.T=[[a,e,i],[b,f,j],[c,g,k],[d,h,l]

因此:

将numpy导入为np
np.savetxt(“foo.csv”,np.array(final.T,delimiter=“,”)

它不起作用:
-请解释实际问题A、b、c、d都是字符串,对吗?是的,它们都是numpy.string\谢谢大家的回答和努力!