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