简体   繁体   English

Python - 使用字典获取数据时出错,因为它只能从 14 个数据中获取 1 个输出。 谢谢

[英]Python - Error when get data using dictionary because it was only get 1 output from 14 data. Thx

I'm new in pyhton and i have error when i made dictionary to get key and value from string.我是pyhton的新手,当我制作字典以从字符串中获取键和值时出错。 For example i have string "nama:xxxx" and I wan convert it to "nama" : "xxxx" and i will use that result to json.例如,我有字符串 "nama:xxxx" 并且我想将它转换为 "nama" : "xxxx" 并且我将使用该结果到 json。

the input is输入是

PROVINSI DKI JAKARTA JAKARTA UT/ ARA 
<NIK : xxxxxx 
<Nama :  xxxx 
<Tempat/Tgl Lahir : TANJUNGBALAI, 13-03-1993 
<Jenis Kelamin :  LAKI-LAKI 
<Gol. Darah : - 
<Alamat : JL WALET ELOK 3 NO. II A 
<RT/RW : 015/006 
<Kel/Desa : KAPUK MUARA 
<Kecamatan : PENJARINGAN 
<Agama : BUDHA 
<Status Perkawinan : BELUM KAWIN 
<Pekerjaan : KARYAWAN SWASTA 
<Kewarganegaraan : WNI 
<Berlaku Hingga : SEUMUR HIDUP

Here is my code and my dictionary to remove "<" and I want the result from "NIK : xxxxx" to "NIK" : "xxxx"这是我的代码和我的字典,用于删除“<”,我希望结果从“NIK:xxxxx”到“NIK”:“xxxx”

dct = {1: 'NIK', 2: 'Nama', 3: 'Tempat', 4: 'Jenis', 6: 'Gol', 7: 'Alamat', 8: 'RT/RW', 9: 'Kel/Desa', 10: 'Kecamatan', 11: 'Agama', 11: 'Status', 12: 'Pekejaan', 13: 'Kewarganegaraan', 14: 'Berlaku'}
ls = []
rs = {}
for words in listString.split("<"):
  for word in dct.values():
    if(similar(words,word))>0.8:
        ls.append(words)
print(ls)
list_to_dict(rs,ls)
res = json.dumps(rs)
parsed = json.loads(res)
print(json.dumps(parsed, indent=4, sort_keys=True))

and if I execute that code I only get "Kewarganegaraan : WNI" and "Kewarganegaraan" : "WNI" i confused about where is the other else like NIK, Nama, Alamat etc. Please dong pay attention to "PROVINSI DKI JAKARTA JAKARTA UT/ ARA" because i don't need that result.如果我执行该代码,我只会得到 "Kewarganegaraan : WNI" 和 "Kewarganegaraan" : "WNI" 我很困惑 NIK、Nama、Alamat 等其他代码在哪里。请注意“PROVINSI DKI JAKARTA JAKARTA UT/ ARA”,因为我不需要那个结果。

And this is the list_to_dict function这是 list_to_dict 函数

def list_to_dict(res,listo):
x = []
y = []
key = []
value = []
for words in listo:
    x,y = words.split(":")[0], words.split(":")[1]
    key.append(x.strip())
    value.append(y.strip())
for k in key: 
    for val in value: 
        res[k] = val
        value.remove(val)
        break
return res

Thankyou谢谢

You can directly create a dictionary using the below code:您可以使用以下代码直接创建字典:

ip = '''PROVINSI DKI JAKARTA JAKARTA UT/ ARA 
<NIK : xxxxxx 
<Nama :  xxxx 
<Tempat/Tgl Lahir : TANJUNGBALAI, 13-03-1993 
<Jenis Kelamin :  LAKI-LAKI 
<Gol. Darah : - 
<Alamat : JL WALET ELOK 3 NO. II A 
<RT/RW : 015/006 
<Kel/Desa : KAPUK MUARA 
<Kecamatan : PENJARINGAN 
<Agama : BUDHA 
<Status Perkawinan : BELUM KAWIN 
<Pekerjaan : KARYAWAN SWASTA 
<Kewarganegaraan : WNI 
<Berlaku Hingga : SEUMUR HIDUP
'''
result = {}
for line in ip.split('<'):
    if ":" in line:
        l_split = line.split(":")
        k, v = l_split[0].strip(), l_split[1].strip()
        result[k] = v

res = json.dumps(result)
parsed = json.loads(res)
print(json.dumps(parsed, indent=4, sort_keys=True))

Output:输出:

{
    "Agama": "BUDHA",
    "Alamat": "JL WALET ELOK 3 NO. II A",
    "Berlaku Hingga": "SEUMUR HIDUP",
    "Gol. Darah": "-",
    "Jenis Kelamin": "LAKI-LAKI",
    "Kecamatan": "PENJARINGAN",
    "Kel/Desa": "KAPUK MUARA",
    "Kewarganegaraan": "WNI",
    "NIK": "xxxxxx",
    "Nama": "xxxx",
    "Pekerjaan": "KARYAWAN SWASTA",
    "RT/RW": "015/006",
    "Status Perkawinan": "BELUM KAWIN",
    "Tempat/Tgl Lahir": "TANJUNGBALAI, 13-03-1993"
}

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

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