如何从字符串中删除字符(python)
我有一个包含大量网站的文本文件如何从字符串中删除字符(python),python,character,Python,Character,我有一个包含大量网站的文本文件 text = '"wadouri:https:\/\/dev.pluginslab.com\/dicomviewer\/wp-content\/plugins\/pl-dicom-viewer-amazon-s3\/assets\/cases\/8255\/20191209113141\/sagittal-00000001.dcm","wadouri:https:\/\/dev.pluginslab.com\/dicomviewer\/wp-content\/pl
text = '"wadouri:https:\/\/dev.pluginslab.com\/dicomviewer\/wp-content\/plugins\/pl-dicom-viewer-amazon-s3\/assets\/cases\/8255\/20191209113141\/sagittal-00000001.dcm","wadouri:https:\/\/dev.pluginslab.com\/dicomviewer\/wp-content\/plugins\/pl-dicom-viewer-amazon-s3\/assets\/cases\/8255\/20191209113141\/sagittal-00000002.dcm","wadouri:https:\/\/dev.pluginslab.com\/dicomviewer\/wp-content\/plugins\/pl-dicom-viewer-amazon-s3\/assets\/cases\/8255\/20191209113141\/sagittal-00000003.dcm", etc'
我能够将每个网站提取到一个列表中
但是,我的列表中有“/”字符,我似乎无法删除
谁能告诉我哪里弄错了吗
谢谢
import re
import bs4 as bs
import urllib.request
import os
myfile = open('C:/test/test.txt', 'r')
regex = re.compile(r'(?<=https).*?(?=dcm)')
dcm =[]
for line in myfile:
matches = regex.findall(line)
for m in matches:
dcm.append (str('https' + m + 'dcm'))
for d in dcm:
d.replace('/','')
print(d)
重新导入
将bs4作为bs导入
导入urllib.request
导入操作系统
myfile=open('C:/test/test.txt','r')
regex=re.compile(r’(?replace
返回被替换的字符串。检查文档
你可以用
for d in dcm:
new_string = d.replace('/','')
print(new_string)
您需要将d.replace('/','')
的输出捕获到如下新变量中:
for d in dcm:
new_string = d.replace('/','')
print(new_string)
字符串在Python中是不可变的,因此必须创建一个新字符串
基于职位的替换:
d = d[:pos] + d[(pos+1):]
d = d.replace('/','')
基于字符的替换:
d = d[:pos] + d[(pos+1):]
d = d.replace('/','')
然后查看新字符串
print(d)
您没有打印修改后的字符串。请使用:print(d.replace('/','')
或d=d.replace('/','')
这与jQuery有什么关系?它是一个JavaScript库,与Python无关。你确定要删除正斜杠吗?很可能是要删除反斜杠。我不确定为什么要删除它们,除非这是一个JSON文件。但是如果是JSON,你应该使用JSON
模块来PARe它,而不是做自己的字符串处理。