python-3.xquotation-marks

reading a file but whenever I get the Key Pair Value...unable to get rid of the quotations


I'm creating a dictionary from this data set file but every time I read from the file.

for example - sample file with r extension (test.r)

#######################TEST###########################
#### 1. Test
key1 = '2022-01-01'    #random comment
key2 = 123L 
key3 = 'Hello World'

# COMMAND --------

The value for the dictionary for key1 is always "'2022-01-01'" how do I get the dictionary to only be '2022-01-01'. I tried replacing the double quotes and it didn't work. This is the working code I have so far.

with open(path, 'r') as reader:
line = reader.readlines()

for text in line:
    if not text.startswith('#') and not text.startswith('\n'):
        print(text)
        result = strip_comments(text)
        print(result)
        value = result[1].strip().split()[0]
        print(value=='2022-01-01')
        break

function to do initial parsing

def strip_comments(line):
    result = line.split(' = ')
    return result

Solution

  • You already have the answer in your question: strip the inner quotation marks.

    value = result[1].strip().split()[0].strip("'")