0

I have a text file that contain a bunch of websites.

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'

I was able to extract each website into a list

However there are '/' character in my list I cant seem to remove.

could some one tell me where I got it wrong

Thanks

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)
3
  • 5
    you arent printing the modified string. use: print(d.replace('/','')) or d = d.replace('/','') Commented Jan 13, 2020 at 7:05
  • What does this have to do with jQuery? It's a JavaScript library, and has nothing to do with Python. Commented Jan 13, 2020 at 7:10
  • 2
    Are you sure you want to remove forward slashes? More likely it's the backslashes you want to remove. I'm not sure why you're getting them, unless this is a JSON file. But if it's JSON, you should be using the json module to parse it, not do your own string processing. Commented Jan 13, 2020 at 7:11

3 Answers 3

1

You need to capture the output of d.replace('/','') into a new variable like this:

for d in dcm:
    new_string = d.replace('/','')
    print(new_string)
Sign up to request clarification or add additional context in comments.

Comments

0

replace returns the replaced string. Check the doc here

You can use

for d in dcm:
    new_string = d.replace('/','')
    print(new_string)

Comments

0

String are immutable in Python, therefore you have to create a new string.

Position based replacement:

d = d[:pos] + d[(pos+1):]

Character based replacement:

d = d.replace('/','')

then to see new string

print(d)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.