擅长:python、mysql、java
<p>假设列名将被排序。例如名称总是出现在列的第一位。下面的代码可以正常工作。你也不需要任何额外的图书馆。你知道吗</p>
<pre><code>arr = [('01', ['name abc', 'age 32', 'address addr123']), ('02', ['name def', 'age 22', 'address addr456'])]
columnName = ["id"]
output = []
for l in arr:
ID = l[0]
val = l[1]
line = str(ID) + '\t'
for x in val:
y = x.split(' ')
if y[0] not in columnName:
columnName.append(y[0])
line += str(y[1]) + '\t'
line += '\n'
output.append(line)
colLine = ''
for i in columnName:
colLine += str(i) + '\t'
colLine += '\n'
output = [colLine] + output
f = open("output.csv", 'w')
for x in output:
f.write(x)
f.close()
</code></pre>