删除字符串中的字符 Python
Delete characters in a string Python
orig_string = "\\file_foo\bar\text-to-be-deleted\foo-bar.pdf"
需要修改原始字符串(复制到新变量中)使其看起来像下面的 new_string。该文件有数千行具有相同格式(pdf文件的文件路径)。
new_string = "\\file_foo\bar\foo-bar.pdf"
如何修改 orig_string 使其看起来像新字符串?
编辑:
对不起,我忘了在我的原件上提到 post。 '\text-to-be-deleted' 不一样。所有文件路径都有不同的 '\text-to-be-deleted' 字符串。
例如
"\\file_foo\bar\path100\foo-bar.pdf"
"\\file_foo\bar\path-sample\foo-bar.pdf"
"\\file_foo\bar\another-text-be-deleted\foo-bar.pdf"
...
等等。
如果你知道text-to-be-deleted
是什么,那么你可以使用
new_string = orig_string.replace('text-to-be-deleted\','')
如果您只知道要保留的部分,我会使用 str.split() 将您知道的部分作为参数。
编辑(拆分版):
我会这样做,但那里可能更清洁:
orig_string = "\\file_foo\bar\text-to-be-deleted\foo-bar.pdf"
temp_str = orig_string.split('\')
idx = temp_str.index('bar')
new_string = temp_str[:idx+1] + temp_str[idx+2:]
new_string = '\'.join(new_string)
print(new_string)#\file_foo\bar\foo-bar.pdf
我正在考虑您要删除每条路径的倒数第二个元素
orig_string = "\\file_foo\bar\text-to-be-deleted\foo-bar.pdf"
orig_string = orig_string.split("\")
value = orig_string[:-1]
str1 = orig_string[-1]
value[-1] = str1
value[0] = "\"#Insert "\" at index 0
value[1] = "\"#Insert "\" at index 1
print('\'.join(value))#join the list
输出
\\file_foo\bar\foo-bar.pdf
使用以下代码:
orig_string = "\\file_foo\bar\text-to-be-deleted\foo-bar.pdf"
new_string = orig_string
start = new_string.find("bar\")
start = start + 4 # so the start points to char next to bar\
end = new_string.find("\foo")
temp = new_string[start:end] # this the text to be deleted
new_string = new_string.replace(temp , "") #this is the required final string
输出:
\file_foo\bar\foo-bar.pdf
我有办法。希望对你有帮助。
orig_string = "\\file_foo\bar\text-to-be-deleted\foo-bar.pdf"
back_index = orig_string.rfind('\')
front_index = orig_string[:back_index].rfind('\')
new_string = orig_string[:front_index] + orig_string[back_index:]
print(new_string)
输出
'\\file_foo\bar\foo-bar.pdf'
orig_string = "\\file_foo\bar\text-to-be-deleted\foo-bar.pdf"
需要修改原始字符串(复制到新变量中)使其看起来像下面的 new_string。该文件有数千行具有相同格式(pdf文件的文件路径)。
new_string = "\\file_foo\bar\foo-bar.pdf"
如何修改 orig_string 使其看起来像新字符串?
编辑: 对不起,我忘了在我的原件上提到 post。 '\text-to-be-deleted' 不一样。所有文件路径都有不同的 '\text-to-be-deleted' 字符串。
例如
"\\file_foo\bar\path100\foo-bar.pdf"
"\\file_foo\bar\path-sample\foo-bar.pdf"
"\\file_foo\bar\another-text-be-deleted\foo-bar.pdf"
... 等等。
如果你知道text-to-be-deleted
是什么,那么你可以使用
new_string = orig_string.replace('text-to-be-deleted\','')
如果您只知道要保留的部分,我会使用 str.split() 将您知道的部分作为参数。
编辑(拆分版): 我会这样做,但那里可能更清洁:
orig_string = "\\file_foo\bar\text-to-be-deleted\foo-bar.pdf"
temp_str = orig_string.split('\')
idx = temp_str.index('bar')
new_string = temp_str[:idx+1] + temp_str[idx+2:]
new_string = '\'.join(new_string)
print(new_string)#\file_foo\bar\foo-bar.pdf
我正在考虑您要删除每条路径的倒数第二个元素
orig_string = "\\file_foo\bar\text-to-be-deleted\foo-bar.pdf"
orig_string = orig_string.split("\")
value = orig_string[:-1]
str1 = orig_string[-1]
value[-1] = str1
value[0] = "\"#Insert "\" at index 0
value[1] = "\"#Insert "\" at index 1
print('\'.join(value))#join the list
输出
\\file_foo\bar\foo-bar.pdf
使用以下代码:
orig_string = "\\file_foo\bar\text-to-be-deleted\foo-bar.pdf"
new_string = orig_string
start = new_string.find("bar\")
start = start + 4 # so the start points to char next to bar\
end = new_string.find("\foo")
temp = new_string[start:end] # this the text to be deleted
new_string = new_string.replace(temp , "") #this is the required final string
输出:
\file_foo\bar\foo-bar.pdf
我有办法。希望对你有帮助。
orig_string = "\\file_foo\bar\text-to-be-deleted\foo-bar.pdf"
back_index = orig_string.rfind('\')
front_index = orig_string[:back_index].rfind('\')
new_string = orig_string[:front_index] + orig_string[back_index:]
print(new_string)
输出
'\\file_foo\bar\foo-bar.pdf'