[英]How to save a tuple to excel in python
我從應用程序中獲取了一些數據,並且 output 在元組中。
deals
(
Instrument Date Announced SDC Deal No
0 154085693165 2020-07-16 3577020040
1 154086829977 2020-07-16 3603239040
2 154086830011 2020-07-16 3603259040
3 154086830078 2020-07-16 3603284040
4 154086830341 2020-07-16 3603413040,
None)
如何使用 python 將其保存到 xlsx 文件中?
元組返回 2 個值。 一個帶有數據框,另一個帶有任何錯誤。 在這種情況下。 所以下面的代碼有幫助。
datframe,error=ek.function_name(argument1,argument2,...)
或者,
deals,e=ek.get_data(common_name,company_id)
所以現在
打印(交易)返回
Instrument Date Announced SDC Deal No
0 154085693165 2020-07-16 3577020040
1 154086829977 2020-07-16 3603239040
2 154086830011 2020-07-16 3603259040
3 154086830078 2020-07-16 3603284040
4 154086830341 2020-07-16 3603413040
和 print(e) 返回
none
import csv
deals = list(deals)
with open("NameOfFile.csv", "w") as csvfile:
write = csv.writer(csvfile)
for x in deals:
write.writerow(map(lambda x: [x], deals))
這應該有效。 這是從Python的 csv 文檔中獲取的(稍作修改)。
import pandas as pd
df = pd.DataFrame(deals)
df.head()
你能看到你從中得到什么嗎? 編輯索引和列后,您可以運行:
df.to_excel('name.xls')
您可以嘗試類似下面的代碼在 csv 文件中寫入字符串列表:
import numpy as np
import pandas as pd
deals = ("Instrument", "Date", "Announced", "SDC", "Deal", "No")
x = []
for j in range(0,len(deals)):
x.append(str(deals[j]))
with open("myfile.txt", "w") as file1:
for i in range(0,len(x)):
file1.write(x[i])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.