Print可以正常工作,但是当我将相同的东西写入文件时,会得到“预期的字符缓冲区对象”?

2024-10-01 17:31:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Splunk,但这似乎是一个与python相关的问题。你知道吗

通过一个API调用,我接收到一个字典列表,并遍历各个字典以打印出一个特定的字段。看起来是这样的:

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是列表,item是单个字典。你知道吗

打印调用非常有效。它按我的要求打印出业务线,这是它应该做的。所以我不透露个人信息(真正的单词是英语,长度相似,如果有关系的话。。。一个单词没有空格),输出如下所示:

Alpha

Bravo

Charlie

但是,当我写()相同的东西(item[key])时,我会得到一个:"expected a character buffer object"错误。你知道吗

所以,我把它改成LOBs.write(str(item[key])。但是当我写这个文件时,我没有得到上面的输出,而是得到(A,B,C加粗以便于查看):

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

现在,我知道这看起来很大,你不知道这意味着什么听我说:)。很明显,write()和print()的工作方式是不同的。既然解释清楚了,我的问题是:

  • 有人知道我如何模仿print()的工作方式吗 write()工作,这样我就可以在每一行上得到干净的A,B,C输出?你知道吗

非常感谢。如果可能的话,我认为这是解决问题的最好办法。你知道吗


Tags: thekeylogage字典itemwriteinternal
2条回答

你能用这个代码再试一次并提供输出吗?你知道吗

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

现在,您应该在打印中看到与文件中相同的内容

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

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

正如错误消息所指出的,在将对象写入流之前,您需要将对象转换为字符串(但是这对于您的目的是合适的)。你知道吗


链接到Python2.7文档:https://docs.python.org/release/2.7/library/functions.html#print

相关问题 更多 >

    热门问题