在 Python 中,将报告写入文件的过程可以使用内置的文件操作功能,通常涉及以下几个步骤:
基本步骤
open() 方法,指定文件名和模式(如写入模式 w 或追加模式 a)。write() 或 writelines() 方法将内容写入文件。close() 方法,或者通过 with 语句自动管理文件资源。1、问题背景
有一份报告需要写入文件。
报告是一个字典。
代码如下:
def write_report(r, filename):
input_file = open(filename, "w")
for k, v in r.items():
line = '{}'.format(k, v)
print(line, file=input_file)
input_file.close()
return input_file
if __name__ == '__main__':
r = {'longs': ['stretched'],
'avglen': 4.419354838709677,
'freqs': {'could': 1, 'number': 1, 'half': 1, 'scorned': 1, 'come': 2, 'numbers': 1, 'rage': 1, 'metre': 1, 'termed': 1, 'heavenly': 1, 'touches': 1, 'i': 1, 'their': 1, 'poets': 1, 'a': 2, 'lies': 1, 'verse': 1, 'an': 1, 'as': 1, 'eyes': 1, 'touched': 1, 'knows': 1, 'tongue': 1, 'not': 1, 'yet': 1, 'filled': 1, 'heaven': 1, 'of': 4, 'earthly': 1, 'hides': 1, 'to': 2, 'stretched': 1, 'deserts': 1, 'this': 1, 'tomb': 1, 'write': 1, 'yellowed': 1, 'that': 1, 'alive': 1, 'some': 1, 'so': 1, 'such': 1, 'should': 2, 'like': 1, 'than': 1, 'antique': 1, 'yours': 1, 'but': 2, 'age': 2, 'less': 1, 'fresh': 1, 'time': 2, 'rhyme': 1, 'true': 1, 'neer': 1, 'all': 1, 'in': 4, 'live': 1, 'be': 2, 'your': 6, 'who': 1, 'truth': 1, 'child': 1, 'twice': 1, 'shows': 1, 'poet': 1, 'most': 1, 'life': 1, 'song': 1, 'will': 1, 'my': 3, 'if': 2, 'parts': 1, 'were': 2, 'you': 1, 'is': 1, 'papers': 1, 'it': 3, 'which': 1, 'rights': 1, 'with': 2, 'say': 1, 'old': 1, 'beauty': 1, 'high': 1, 'and': 5, 'would': 1, 'believe': 1, 'faces': 1, 'though': 1, 'men': 1, 'graces': 1, 'the': 2},
'shorts': ['i', 'a'],
'count': 93,
'mosts': ['your']}
write_report(r, 'report.txt')运行代码会报错:
TypeError: file() argument 2 must be iterable2、解决方案
在第一种方案中,我们使用 open() 函数来创建一个文件对象,然后使用 print() 函数来将数据写入文件。
在第二种方案中,我们使用 with 语句来创建一个文件对象,然后使用 f.write() 函数来将数据写入文件。
def write_report(r, filename):
with open(filename, "w") as f:
for k, v in r.items():
f.write('{}, {}\n'.format(k, v))
if __name__ == '__main__':
r = {'longs': ['stretched'],
'avglen': 4.419354838709677,
'freqs': {'could': 1, 'number': 1, 'half': 1, 'scorned': 1, 'come': 2, 'numbers': 1, 'rage': 1, 'metre': 1, 'termed': 1, 'heavenly': 1, 'touches': 1, 'i': 1, 'their': 1, 'poets': 1, 'a': 2, 'lies': 1, 'verse': 1, 'an': 1, 'as': 1, 'eyes': 1, 'touched': 1, 'knows': 1, 'tongue': 1, 'not': 1, 'yet': 1, 'filled': 1, 'heaven': 1, 'of': 4, 'earthly': 1, 'hides': 1, 'to': 2, 'stretched': 1, 'deserts': 1, 'this': 1, 'tomb': 1, 'write': 1, 'yellowed': 1, 'that': 1, 'alive': 1, 'some': 1, 'so': 1, 'such': 1, 'should': 2, 'like': 1, 'than': 1, 'antique': 1, 'yours': 1, 'but': 2, 'age': 2, 'less': 1, 'fresh': 1, 'time': 2, 'rhyme': 1, 'true': 1, 'neer': 1, 'all': 1, 'in': 4, 'live': 1, 'be': 2, 'your': 6, 'who': 1, 'truth': 1, 'child': 1, 'twice': 1, 'shows': 1, 'poet': 1, 'most': 1, 'life': 1, 'song': 1, 'will': 1, 'my': 3, 'if': 2, 'parts': 1, 'were': 2, 'you': 1, 'is': 1, 'papers': 1, 'it': 3, 'which': 1, 'rights': 1, 'with': 2, 'say': 1, 'old': 1, 'beauty': 1, 'high': 1, 'and': 5, 'would': 1, 'believe': 1, 'faces': 1, 'though': 1, 'men': 1, 'graces': 1, 'the': 2},
'shorts': ['i', 'a'],
'count': 93,
'mosts': ['your']}
write_report(r, 'report.txt')运行代码后,文件 report.txt 中会生成如下数据:
longs, ['stretched']
avglen, 4.419354838709677
freqs, {'could': 1, 'number': 1, 'half': 1, 'scorned': 1, 'come': 2, 'numbers': 1, 'rage': 1, 'metre': 1, 'termed': 1, 'heavenly': 1, 'touches': 1, 'i': 1, 'their': 1, 'poets': 1, 'a': 2, 'lies': 1, 'verse': 1, 'an': 1, 'as': 1, 'eyes': 1, 'touched': 1, 'knows': 1, 'tongue': 1, 'not': 1, 'yet': 1, 'filled': 1, 'heaven': 1, 'of': 4, 'earthly': 1, 'hides': 1, 'to': 2, 'stretched': 1, 'deserts': 1, 'this': 1, 'tomb': 1, 'write': 1, 'yellowed': 1, 'that': 1, 'alive': 1, 'some': 1, 'so': 1, 'such': 1, 'should': 2, 'like': 1, 'than实践
with 语句: 自动管理文件资源,防止文件未关闭的问题。根据报告的内容和用途,选择合适的方法将报告写入文件即可。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。