我正在尝试在for循环中打印数据,但输出省略了行尾的'\ n'....
f.write(f'[{str(train_time[i])}] № {str(train_number[i])} {str(train_direct[i])}\n')
为什么我最后没有换行?把'\ n'放在最后并没有这样做。
这是所有代码块
import re
with open('input.txt', 'r', encoding='utf-8') as f:
text = str(f.read())
train_time = re.findall(r'\d{3}\s\w+\s\w{1,2}.+(\d{2}\:\d{2}\:\d{2})', text)
train_number = re.findall(r'(\d{3})\s\w+\s\w{1,2}.+\d{2}\:\d{2}\:\d{2}', text)
train_direct = re.findall(r'\d{3}\s\w+\s(\w{1,2}\s\w+)', text)
with open('output.txt', 'w+', encoding='utf-8', newline='\n') as f:
i = 0
for i in range(len(train_number)):
f.write(f'[{str(train_time[i])}] № {str(train_number[i])} {str(train_direct[i])}\n')
答案 0 :(得分:-2)
如果您最终要尝试格式化字符串,则必须像here中那样进行格式化。
此外,在格式化之后,尝试添加'\n'
,如下所示:
f.write(f'[{str(train_time[i])}] № {str(train_number[i])} {str(train_direct[i])}'+'\n')