0

How can we strip a string off its single quotes.

I have a file with the names of positive and negative reviews and I wish to go through another directory and check if the file exists there or not and if it does save it into a new directory.

I tried the following piece of code to split it, but the resulting output is as shown below.

import os
path = (r"C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos")
with open('Data\Positive_Reviews.txt','r') as file: 
    for line in file:
        for word in line.split(', '):
            word.replace("\'", "")
            #word.translate(str.maketrans({"'":None}))
            filename = word
            file_path = os.path.join(path, filename)
            print(file_path)

OUTPUT:

C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\'cv559_0050.txt'
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\'cv144_5007.txt'
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\'cv059_28885.txt'
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\'cv987_6965.txt'
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\'cv253_10077.txt'
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\'cv778_17330.txt'
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\'cv121_17302.txt'
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\'cv864_3416.txt'
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\'cv320_9530.txt'
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\'cv646_15065.txt'
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\'cv921_12747.txt'

However I would like the output to be

EXPECTED OUTPUT:

C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\cv559_0050.txt
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\cv144_5007.txt
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\cv059_28885.txt
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\cv987_6965.txt
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\cv253_10077.txt
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\cv778_17330.txt
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\cv121_17302.txt
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\cv864_3416.txt
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\cv320_9530.txt
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\cv646_15065.txt
C:\Users\HP\1. ALL MY FOLDERS\SOLARILLION\MLBasics-master\Data\pos\cv921_12747.txt

I wish to do this so that I can check the file for its names and hence try to save it into new directory

Seanny123
  • 8,776
  • 13
  • 68
  • 124
GV-9wj
  • 37
  • 4
  • Does this answer your question? [replace characters not working in python](https://stackoverflow.com/questions/7208861/replace-characters-not-working-in-python) – Pranav Hosangadi May 30 '22 at 17:20

1 Answers1

0

Change this line from:

filename = word

to this:

filename = word.replace("\'", "")