I am using vertica_python to pull data from the database. The column that I pull comes as a string in the following format:
[{"id":0,"prediction_type":"CONV_PROBABILITY","calibration_factor":0.906556,"inte cept":-2.410414,"advMatchTypeId":-0.239877,"atsId":-0.135568,"deviceTypeId":0.439130,"dmaCode":-0.251728,"keywordId":0.442240}]
I then split and parse this sting and load it into excel in the following format, each index being a cell:
prediction_type CONV_PROBABILIT calibration_factor 0.90655 intercept -2.41041 advMatchTypeId -0.23987 atsId 1.44701 deviceTypeId 0.19701 dmaCode -0.69982 keywordId 0.44224
Here's my problem.The string doesn't have a definite format, meaning, that sometimes I be missing some features from the string, messing up my formatting. Here's an example:
intercept -2.41041 advMatchTypeId -0.23987 deviceTypeId 0.37839 dmaCode -0.53552 keywordId 0.44224
intercept -2.41041 advMatchTypeId -0.23987 atsId 0.80708 deviceTypeId -0.19573 dmaCode -0.69982 keywordId 0.44224
How can I retain formatting the way I want and have the above example come out looking like this instead:
intercept -2.41041 advMatchTypeId -0.23987 deviceTypeId 0.37839 dmaCode -0.53552 keywordId 0.44224
intercept -2.41041 advMatchTypeId -0.23987 atsId 0.80708 deviceTypeId -0.19573 dmaCode -0.69982 keywordId 0.44224
This is the code I am using:
data_all = cur.fetchall()
for i in range(len(data_all)):
col = 0
data_one = ''.join(data_all[i])
raw_coef = data_one.split(',')[1:len(data_all)]
for j in range(len(raw_coef)):
raw = ''.join(raw_coef[j])
raw = re.sub('"|}|{|[|]|', '', raw)[:-1]
raw = raw.split(":")
for k in range(len(raw)):
worksheet.write(i, col, raw[k], align_left)
feature.append(raw[0]) # for unique values
col+=1
My query:
cur.execute(
"""
select MODEL_COEF
from
dcf_funnel.ADV_BIDDER_PRICING_LOG
where MODEL_ID = 8960
and DATE(AMP_QUERY_TIMESTAMP) = '11-02-2016'
"""
)
cursorproperties and then I will try put something togetherlistcontaining adictionary). It only becomes a string when you do''.join(). You seem to be shooting yourself in the foot with that part. I normally useSQLitewhich returns tuples, but I can't think of any reason a query would ever return astringthat you have to chop up with regex.dictandlistin Python to be able to do anything useful. You've made this task near impossible for yourself without knowing that.