[英]Replacing one character with two (\n by \r\n ) replaces also one of the replacing characters
I'm trying to convert large number of files to a common line endings with this script. 我正在尝试使用此脚本将大量文件转换为公共行结尾。 The script is called in git-shell using for loop. 使用for循环在git-shell中调用该脚本。
After running all line endings have only CR as line ending. 运行所有行结束后只有CR作为行结束。 I suppose because replace(contents, '\\n', '\\r\\n' ) replaces also \\n after \\r. 我想因为替换(内容,'\\ n','\\ r \\ n')也会在\\ r \\ n之后替换\\ n。 Is there a may to prevent it? 是否有可能阻止它? Should I replace linewise? 我应该替换linewise吗?
import sys
import string
import os.path
for file in sys.argv[1:]:
if not os.path.exists(file):
continue
contents = open(file, 'rb').read()
cont1 = string.replace(contents, '\n', '\r\n' )
open(file, 'wb').write(cont1)
I tried your code literally copy-paste and it works just fine on python2.7: 我尝试了你的代码字面上的复制粘贴,它在python2.7上运行得很好:
bash$ cat file1
one
two
bash$ file file1
file1: ASCII text
bash$ hd file1
00000000 6f 6e 65 0a 74 77 6f 0a |one.two.|
00000008
bash$ python2 lineend.py file1
bash$ hd file1
00000000 6f 6e 65 0d 0a 74 77 6f 0d 0a |one..two..|
0000000a
bash$ file file1
file1: ASCII text, with CRLF line terminators
Note however that your are opening the same file twice: once for reading and once for writing. 但请注意,您要打开两次相同的文件:一次用于阅读,一次用于写入。 Probably won't cause problems in this exact case, but generally that is not good practice. 在这个确切的情况下可能不会引起问题,但通常这不是好的做法。
import sys
import string
import os.path
for file in sys.argv[1:]:
if not os.path.exists(file):
continue
f = open(file, 'rb')
contents = f.read()
f.close()
cont1 = string.replace(contents, '\n', '\r\n' )
open(file, 'wb').write(cont1)
You could use re.sub
to perform a regex substitution. 您可以使用re.sub
执行正则表达式替换。
Instead of this line: 而不是这一行:
cont1 = string.replace(contents, '\n', '\r\n' )
You would use the following line (don't forget to import re
): 您将使用以下行(不要忘记import re
):
cont1 = re.sub(r'([^\r])\n', r'\g<1>\r\n', contents)
Update: 更新:
r'([^\\r])\\n'
will not match a newline at the beginning of the file. r'([^\\r])\\n'
与文件开头的换行符不匹配。 Using r'([^\\r])?\\n'
instead should do the job. 使用r'([^\\r])?\\n'
代替应该完成这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.