我有這是越來越線一些Python代碼結局都是錯誤的:蟒蛇的FileInput行尾
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該文件的副本,並插入到內容在被叫out_filename另一個文件文件中的「INPUT_TAG」位置。
問題是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')'代替? – Carlos 2014-11-23 20:08:52
只是嘗試打印content.rstrip('\ r \ n')並且沒有變化 – Daniel 2014-11-23 20:15:16