python中string
也可以看做list
,並調用list
的方法,但是一旦調用原本的string
就會變爲[ .. , .. , ..]
的格式,要想再拼接爲string
的形式就需要用for
進行拼接,當然這個過程中也可以做很多的事情,比如路徑修改。
out_path = 'input_file=/home/xl/project/ChineseNER/tensorflow/test1.txt'
split_list = output_path.split('/')
# 去掉最後的test1.txt
file_name = split_list.pop()
head = ''
# 重新拼接路徑
for path in split_list:
head = os.path.join(head, path)
# 拼接路徑並將最後的文件名替換
entity_file = '/' + head + '/entity' + '_' + file_name
print(entity_file)
# /home/xl/project/ChineseNER/tensorflow/entity_test1.txt