0
def event_handler_quote_update(message):
    # print(f"quote update {message}")
    global row_no
    global token
    global ltp
    global Date
    token = message['token']
    ltp =   message['ltp']
    Date = message['exchange_time_stamp']

i need to write the above live running data into csv file currently my python script will not do anything writing the csv file.

currently it is working with excel file, but i am having issue while reading same excel from different script, so i am planning to write the data into csv file,

raju kiranlr
  • 15
  • 1
  • 4

2 Answers2

1

There is no need to declare any of those things global. A global declaration is useful and necessary when there is a variable outside of your def which you need to change from inside.

For writing CSV data, probably use the csv library.

import csv

# ...

def event_handler_quote_update(message):
    # print(f"quote update {message}")
    token = message['token']
    ltp   = message['ltp']
    date  = message['exchange_time_stamp']
    with open('results.csv', 'a+') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow([token, ltp, date])

It would be a lot better if there was a way to open the CSV file for writing and instantiate the writer object just once, outside the callback, and simply pass in writer as the second argument after message; though then, you will need to close() it separately when you are done.

import csv

# ...

def event_handler_quote_update(message, writer):
    # print(f"quote update {message}")
    token = message['token']
    ltp   = message['ltp']
    date  = message['exchange_time_stamp']
    writer.writerow([token, ltp, date])

# ...

if __name__ == '__main__':
    # whatever else you have here
    csvfile = open('results.csv', 'w')
    writer = csv.writer(csvfile)

    # ... whatever else your program needs to do

    csvfile.close()

But maybe you don't have control over how the callback gets called. Then maybe you do need writer to be global inside the def.

There's no need really to copy each field into a separate variable; if there are a lot of fields you want to extract, probably switch to something like

        writer.writerow([message[x] for x in ('token', 'ltp', 'exchange_time_stamp')])

See the documentation link above if you need to use a different variant of CSV format, like tab or semicolon delimited, or write a header at the top, etc. It's all there.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

convert your data into csv formate and pass into below function.

tempstr = token + ";" + ltp + ";" + Date + "\n"
write_file = "output.csv"
with open(write_file, "w") as output:
    output.write(tempstr)
Vishal Patel
  • 511
  • 6
  • 21
  • how to convert the data into csv format ? it is live data which is available, – raju kiranlr Oct 22 '20 at 11:07
  • Merge your above three objects in one string using delimiter and newline at end of your data and pass in this function. example: ";;\n" and pass this string into above function. make sure your string doesn't contain delimiter ; otherwise it not gonna work. – Vishal Patel Oct 22 '20 at 11:44
  • i am doing as you told, but no output, ` def event_handler_quote_update(message): # print(f"quote update {message}") global row_no global token global ltp global Date token = message['token'] ltp = message['ltp'] Date = message['exchange_time_stamp'] write_csv( ";;\n") def write_csv(data): with open('outfile.csv', 'a') as outfile: writer = csv.writer(outfile) writer.writerow(data) ` – raju kiranlr Oct 22 '20 at 12:30
  • check my updated answer. i gave you example not solution in above comment. but now this is perfect solution. Hope you get this answer. – Vishal Patel Oct 22 '20 at 13:00