-2

例如,在使用 python 编写 txt 文件时,我必须在每个句子的末尾包含“]”。我不知道每个句子的长度。但是,我必须确保“]”是该行的第 50 个字符。(句子的长度将小于 50)。文件中的文本应如下所示:

Apple       ]
Ball        ]
Cat         ]
Dog         ]
Elephant    ]
Frog        ]
4

1 回答 1

0

就像是:

lst = ['Bpple','Apple','Ball','Cat','K','ABCDEFGH']
with open('out.txt','w') as f:
  for word in lst:
    f.write(word.ljust(49) + ']\n')

出.txt

Bpple                                            ]
Apple                                            ]
Ball                                             ]
Cat                                              ]
K                                                ]
ABCDEFGH                                         ]
于 2020-10-07T11:18:40.107 回答