我有包含此示例内容的文本文件:
12/13/18, 14:06 - her:IMG-20181213-WA0005.jpg (file attached)12/13/18, 14:06 - her:PTT-20181213-WA0006.opus (file attached)12/13/18, 14:07 - kristal: its not in the right quality?12/13/18, 14:14 - her:bla bla bla bla12/13/18, 14:43 - kristal: ok for this size12/13/18, 14:43 - kristal: somthing somthing
12/13/18, 14:43 - kristal: rect12/13/18, 14:43 - Enav Sharon-kristal: need square12/13/18, 14:48 - her:sending files12/13/18, 14:49 - Enav Sharon-kristal: ok then
我如何制作一个读取所有文本并将其分成行(当您看到 12 断行)并保存的代码。
你确定你想要一个new line
before every12
吗?
我看到它来自date,但如果你看:
12/13/18, 14:06 - her:IMG-20181213-WA0005.jpg
12
文本中有一个您可能不想要换行的地方。
如果您在约会之前休息,该解决方案可能会很可靠:
with open('file.txt', 'r') as file: # the file you want to read
with open('new_file.txt', 'w') as new_file: # the file you want to save
for line in file.readlines():
new_line = re.sub('(\d+/\d+/\d+)', '\n\g<1>', line).strip()) # find all dates
new_file.write('{}\n'.format(new_line)) # save to new file
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句