简体   繁体   English

读取文件,但每当我得到密钥对值......无法摆脱报价

[英]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)例如 - 带有 r 扩展名的示例文件 (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'. key1 的字典值始终为“'2022-01-01'”我如何让字典仅为'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

您的问题已经有了答案:去掉内引号。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM