Printing works fine, but when I write () the same to a file, I get "Character Buffer Expected Object"?

I'm working with Splunk, but this seems to be a python related issue I have.

On an API call, I get a list of dictionaries, and I repeat through separate dictionaries to print 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")

      

the reader is a list, an item is a separate dictionary.

The print call works fine. It prints lines of business the way I want it to be. So I am not giving out any personal information (real words are English, similar in length, if that matters ... one word without spaces), the output looks like this:

Alpha

Bravo

Charlie

However, when I write () is the same (item [key]), I get an error: "expected a character buffer object"

.

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 in bold for visual convenience):

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 = , 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 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 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 it looks huge and you have no idea what it means. Just listen to me :). Obviously there is a difference in how write () works versus print (). Now that this is explained, my question is:

  • Does anyone know how I can mimic the print () way that write () works, so that I get clean output A, B, C on each line?

Thank you very much. I think this is ^ the best way to approach the problem, if possible.

+3


source to share


2 answers


Can you try again with this code and give us a way out?

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 "%s\n" % (item[key]) # TODO: Replace this with however I display it on the webpage.
                    LOBs.write("%s\n" % (item[key])) # write the LOBs to the file, one LOB per line

      



You should now see the same in print as in the file

0


source


https://docs.python.org/3/library/functions.html#print

All non-keyword arguments are converted to strings such as str () and written to the stream,

As the error message indicates, you need to convert the objects to strings (however this is fine for your purpose) before you can write them to the stream.




Link to Python 2.7 docs: https://docs.python.org/release/2.7/library/functions.html#print

0


source







All Articles