I'm working with Splunk, but this seems to be a python-related problem I'm having.
By an API call, I'm receiving a list of dictionaries, and I'm iterating through the individual dictionaries to print out a specific field. It looks like this:
with open(lobFileName, "w+") as LOBs: #read/write, truncates file!
for item in reader:
for key in item: # iterate through the dictionary
if key == 'cost_center':
print item[key] # TODO: Replace this with however I display it on the webpage.
LOBs.write(item[key]) # write the LOBs to the file, one LOB per line
LOBs.write("\n")
reader is the list, item is the individual dictionary.
The print call works perfectly. It prints out the lines of businesses as I want it to, as it should. So I don't give out personal information (the real words are English, similar in length, if that matters... one word no spaces), the output looks like:
Alpha
Bravo
Charlie
However, when I write() the same thing (item[key]), I get an: "expected a character buffer object"
error.
So, I change it to LOBs.write(str(item[key])
. But when I write the file, instead of getting the above output, I get (A,B,C bolded for ease of sight):
Alpha~1116~7F4F9983-72F8-48C8-BFAD-82C0F713CA34 1116:18886924 1437770160 1 07-24-2015 16:35:59.888 -0400 INFO Metrics - group=per_index_thruput, series="clo", kbps=3.596555, eps=13.129038, kb=111.493164, ev=407, avg_age=2.422604, max_age=27 199 ['ksplidx4c', '_internal'] splunkd .888 2015-07-24T16:35:59.888-04:00
Bravo
psplfwd1a _internal 1 clo /opt/splunk/var/log/splunk/metrics.log splunkd ksplidx4c _internal~1116~7F4F9983-72F8-48C8-BFAD-82C0F713CA34 1116:18886931 1437770160 1 07-24-2015 16:35:59.888 -0400 INFO Metrics - group=per_index_thruput, series="cos", kbps=564.982992, eps=1387.129659, kb=17514.464844, ev=43001, avg_age=2.232622, max_age=11 198 ['ksplidx4c', '_internal'] splunkd .888 2015-07-24T16:35:59.888-04:00
Charlie
psplfwd1a _internal 1 cos /opt/splunk/var/log/splunk/metrics.log splunkd ksplidx4c _internal~1116~7F4F9983-72F8-48C8-BFAD-82C0F713CA34 1116:18886952 1437770160 1 07-24-2015 16:35:59.888 -0400 INFO Metrics - group=per_index_thruput, series="issofim", kbps=1.250410, eps=12.193554, kb=38.762695, ev=378, avg_age=1.738095, max_age=8 195 ['ksplidx4c', '_internal'] splunkd .888 2015-07-24T16:35:59.888-04:00
Now, I know that looks huge and you have no idea what that means Just hear me out :). Obviously there's a difference in how write() works vs. how print() works. Now that this is explained, my question:
- Does anybody know how I can mimic the way print() works into how write() works, so that I get the clean A, B, C output on each line?
Thank you so much. I think this^ is the best way to approach the problem, if possible.