繼馬騰法布爾的建議,你可以讓你的數據接收功能爲數據發生器:
import pyexce as p
...
def data_gen():
for title in titles: # put your data recicving api code here
yield [title.name] + title.data
p.save_as(array=data_gen(), dest_file_name='result.csv')
那麼你將有數據result.csv爲:
Title1,data1,data2,data3
Title2,data4,data5,data6
然後就可以轉它後來:
>>> s=p.get_sheet(file_name='result.csv')
>>> s
result.csv:
+--------+-------+-------+-------+
| Title1 | data1 | data2 | data3 |
+--------+-------+-------+-------+
| Title2 | data4 | data5 | data6 |
+--------+-------+-------+-------+
>>> s.transpose()
>>> s
result.csv:
+--------+--------+
| Title1 | Title2 |
+--------+--------+
| data1 | data4 |
+--------+--------+
| data2 | data5 |
+--------+--------+
| data3 | data6 |
+--------+--------+
>>> s.save_as('perferct_result.csv')
或者,您可以馬上消耗,而不需要t轉交它:
>>> s=p.get_sheet(file_name='result.csv')
>>> s.name_rows_by_column(0)
>>> s
result.csv:
+--------+-------+-------+-------+
| Title1 | data1 | data2 | data3 |
+--------+-------+-------+-------+
| Title2 | data4 | data5 | data6 |
+--------+-------+-------+-------+
>>> s.to_dict()
OrderedDict([(u'Title1', [u'data1', u'data2', u'data3']), (u'Title2', [u'data4', u'data5', u'data6'])])
是數據編號還是某種形式不同於標題?像標題總是字和數據總是整數或浮點數/ – Jacobr365
你應該看看csv模塊:https://docs.python.org/3/library/csv.html –
@ Jacobr365標題和數據都是字符串包含字母和數字。 – shounen