簡體   English   中英

讀取文件,但每當我得到密鑰對值......無法擺脫報價

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

我正在從這個數據集文件創建一個字典,但每次我從文件中讀取。

例如 - 帶有 r 擴展名的示例文件 (test.r)

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

# COMMAND --------

key1 的字典值始終為“'2022-01-01'”我如何讓字典僅為'2022-01-01'。 我嘗試替換雙引號,但沒有用。 這是我到目前為止的工作代碼。

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

進行初始解析的函數

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