Python 根据收到的字典信息动态写入文本文件

Python 根据收到的字典信息动态写入文本文件,python,python-3.x,dictionary,file-writing,Python,Python 3.x,Dictionary,File Writing,我正在尝试根据字典信息将作业写入文本文件。字典信息是从服务器接收的,并不总是相同的。它包含文件\u计数和文件\u路径,其中文件\u路径是一个列表。e、 g, {'file_paths': ['/file/path/one', '/file/path/two'], 'file_count': 2} 我有一个基线来写出一个文本块,其中包含根据字典信息插入的变量。e、 g, text_baseline = ('This is the %s file\n' 'and

我正在尝试根据字典信息将作业写入文本文件。字典信息是从服务器接收的,并不总是相同的。它包含
文件\u计数
文件\u路径
,其中
文件\u路径
是一个列表。e、 g,

{'file_paths': ['/file/path/one', '/file/path/two'], 'file_count': 2} 
我有一个基线来写出一个文本块,其中包含根据字典信息插入的变量。e、 g,

text_baseline = ('This is the %s file\n'
                'and the file path is\n'
                'path: %s\n')
根据从字典接收并写入文本文件的文件数量,需要复制此基线

因此,例如,如果字典有三个文件,它将有三个文本块,每个文本块都有文件编号和路径的更新信息

我知道我必须这样做:

f = open("myfile.txt", "w")
for i in dict.get("file_count"):
    f.write(text_baseline)     # this needs to write in the paths and the file numbers
我很难弄清楚如何根据使用基线收到的信息更新路径和文件号。

使用str.format()格式化字符串

data = {'file_paths': ['/file/path/one', '/file/path/two'], 'file_count': 2} 
text_baseline = "this is the {}th file and the path is {}"
with open('path','w') as f:
    for i in range(int(dict.get('file_count'))):
         f.write(text_baseline.format(i,data['file_paths']))

可以在此处使用枚举和字符串格式:

paths = {'file_paths': ['/file/path/one', '/file/path/two'], 'file_count': 2}
text_baseline = ('''This is the {num} file
and the file path is
path: {path}
''')
with open('myfile.txt','w') as f:
    for i, path in enumerate(paths['file_paths'], 1):
        f.write(text_baseline.format(num=i, path=path))

对于显示的输入,您希望看到什么样的输出?我使用的是python3.4,该版本支持字符串格式吗?我使用的是python3.4,该版本支持字符串格式吗?它支持吗?您缺少一些相近的内容parens@MadPhysicist我确实错过了最后的那些。谢谢,还是不够