[英]How to save a tuple to excel in python
I have fetched some data from an application and the output is in tuple.我从应用程序中获取了一些数据,并且 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)
How do i save this to an xlsx file using python?如何使用 python 将其保存到 xlsx 文件中?
The tuple returns 2 values.元组返回 2 个值。 One with the data frame and other with any error.
一个带有数据框,另一个带有任何错误。 In this case.
在这种情况下。 So the below code helps.
所以下面的代码有帮助。
datframe,error=ek.function_name(argument1,argument2,...)
or,或者,
deals,e=ek.get_data(common_name,company_id)
So now所以现在
print(deals) returns打印(交易)返回
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
and print(e) returns和 print(e) 返回
none
import pandas as pd
df = pd.DataFrame(deals)
df.head()
Can you see what you get from this?你能看到你从中得到什么吗? After you editted your index and columns, you can run:
编辑索引和列后,您可以运行:
df.to_excel('name.xls')
You may try something like below code to write a list of string in a csv file:您可以尝试类似下面的代码在 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.