这是我的文件。
test1,30 (# assuming \n is here)
test2,undefined (# assuming \n is here)
test3,5 valid attempts
这是我目前的代码
## <> arrows suggests input & not shown here
import os
os.system(<file path directory>)
with open(<file path directory>) as openDocument:
openfile = openDocument.read()
openfile = openfile .replace("\x00", "").split("\n")
openfile = [openfile .split(",") for openfile in openfile [1:]]
openFinalFile = {policyName.strip(): policyValue.strip() for policyName, policyValue in openFile if policyName != ''} ## Currently causing an error
print(openFinalFile["test1"])
我怎样才能使我在print(openFinalFile["test1"])时,返回30的值(如文件所示)?
返回的错误: builtins.ValueError: Too many values to unpack (expected 2)
我宁愿用户 pathlib
:
from pathlib import Path
lines = Path(<file path directory>).read_text().replace("\x00", "").splitlines()
pairs = [i.split(',') for i in lines]
openFinalFile = {k.strip(): v.strip() for k, v in pairs if k}
print(openFinalFile["test1"])