使用 Python 每 64 个字符插入一个换行符 [英] Insert a newline character every 64 characters using Python
本文介绍了使用 Python 每 64 个字符插入一个换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Python 我需要每 64 个字符插入一个换行符到一个字符串中.在 Perl 中很容易:
Using Python I need to insert a newline character into a string every 64 characters. In Perl it's easy:
s/(.{64})/$1\n/
如何使用 Python 中的正则表达式来完成此操作?有没有更pythonic的方法来做到这一点?
How could this be done using regular expressions in Python? Is there a more pythonic way to do it?
推荐答案
与 Perl 相同,但使用反斜杠而不是用于访问组的美元:
Same as in Perl, but with a backslash instead of the dollar for accessing groups:
s = "0123456789"*100 # test string
import re
print re.sub("(.{64})", "\\1\n", s, 0, re.DOTALL)
re.DOTALL
是相当于 Perl 的 s/
选项.
这篇关于使用 Python 每 64 个字符插入一个换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文