Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从字符串中删除字符(python)_Python_Character - Fatal编程技术网

如何从字符串中删除字符(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它,而不是做自己的字符串处理。