【发布时间】:2014-11-23 19:58:55
【问题描述】:
我有一些 python 代码导致行尾全错:
command = 'svn cat -r {} "{}{}"'.format(svn_revision, svn_repo, svn_filename)
content = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read()
written = False
for line in fileinput.input(out_filename, inplace=1):
if line.startswith("INPUT_TAG") and not written:
print content
written = True
print line,
这会获取名为 svn_filename 的文件的副本,并将内容插入到文件中“INPUT_TAG”位置的另一个名为 out_filename 的文件中。
问题在于 out_filename 中的行结尾。 它们应该是 \r\n 但我插入的块是 \r\r\n。
将打印语句更改为:
print content, # just removes the newlines after the content block
或
print content.replace('\r\r','\r') # no change
没有效果。在内容离开我的代码后插入额外的回车。似乎有些东西正在决定,因为我在 Windows 上,它应该将所有 \n 转换为 \r\n。
我该如何解决这个问题?
【问题讨论】:
-
您是否尝试过改用
rstrip('\r\n')? -
刚刚尝试打印 content.rstrip('\r\n') 并没有改变