I'm Parsing some data using pd.read_csv
, so i intend to write the data retrieved to a csv file... But, Its giving me error.
Is this issue with pandas dataframe or csv.writer?
And, Please. How do i solve this error(I'm new at python).
This is the code... that returns the content in String format...
data = query(symbols, qualifier, api_format, snapshot, username, password, \
flag_parse_data, session, debug)
print(data)
Here is the new code i added that was giving me error
filename = symbols.replace("/","") + ".csv"
with open(filename, 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(data)
If you pay attention to the log, it seems Python wants to access a field but is struggling to index it, therefore you get this tradicional dictionary error. The best solution for you is to change from the beginning and start using Pandas to generate your dataframe.
Read the documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_csv.html
You don't need to worry, just declare how the values are separated, which column from your csv file should be the index and which line is going to be considered the header (column names). These parameters have default values, check if that may be giving you headaches.
Try it out:
import pandas as pd
df = pd.DataFrame.from_csv(filename, header=0, sep=', ', index_col=0)
Tell me which error you get from that. Bear in mind these arguments I am calling are already the default arguments, check if they are valid for your case and adjust what you need.
Now that you have a valid DataFrame that Pandas know how to work with, do the adjustments you need to and use to_csv method, as it as been already mentioned.