0

I have fetched some data from an application and the output is in tuple.

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?

3
  • 1
    This might be useful: pandas.pydata.org/pandas-docs/stable/reference/api/… Commented Jul 16, 2020 at 10:34
  • 2
    Please add what you have tried so far. Stack Over Flow is not a people write code for you for free. Commented Jul 16, 2020 at 10:34
  • Take a look at tablib. Commented Jul 16, 2020 at 10:36

4 Answers 4

2

The tuple returns 2 values. 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

none
Sign up to request clarification or add additional context in comments.

1 Comment

Finally an answer trying to show the why, not a workaround instead. +1
0
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')

2 Comments

The df looks like this 0 0 Instrument Date Announced SDC Deal No 0 ... 1 None
I am confused, can you do print(deals[:5]) and share the output?
0
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))

This should work. This is taken (slightly modified) from csv docs for Python.

10 Comments

I have tried this. but getting this error "TypeError: a bytes-like object is required, not 'str'"
Then, instead of "wb" try using "w". Check my edited post.
5 6 for x in deals: ----> 7 write.writerow(x) Error: iterable expected, not NoneType
Is your deals tuple ONE HUGE TUPLE or TUPLE OF TUPLES?
Luck or no luck?
|
0

You may try something like below code to write a list of string in a csv file:

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])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.