0
我正在使腳本從文件中找到目錄路徑以形成螞蟻mkdir元素。Python將換行符添加到意想不到的地方
的問題是,當我在結束打印有斷行每行內加入:
ant_mkdir = '<mkdir dir="..' + path + '"/>'
代碼:
from io import open
from string import *
def main():
with open("file.txt", "r") as f:
content = f.readlines()
paths = []
for line in content:
if ("d:\\apps" in line):
line = line.split("d:\\apps")
path = line[1]
path = path.replace("\\", "/")
if path not in paths:
paths.append(path)
for path in paths:
ant_mkdir = '<mkdir dir="..' + path + '"/>'
print ant_mkdir
if __name__ == "__main__":
main()
打印結果:
<mkdir dir="../path/folder/1
"/>
<mkdir dir="../path/folder/2
"/>
<mkdir dir="../path/folder/3
"/>
問題是您讀取的文件的每一行都有換行符。看看這是否有幫助:http://stackoverflow.com/questions/275018/how-can-i-remove-chomp-a-newline-in-python – darthbith
這工作thx! – osguosgu