简体   繁体   中英

get data from .dat file with python

I need to read a .dat file in python, i need to read two value from dat file

[Registration information]
Name=nikam
Key=**KDWOE**

need to get nilkam from name and KDWOE from key

    datContent = [i.strip().split() for i in open("./license.dat").readlines()]
        print (datContent)       

i got this result [['[Registration', 'information]'], ['Name=nilkam'], ['Key=KZOiX=BFcjLKqJr6HwYxYU+NHN8+MP7VO0YA5+O1PwX0C3twCmum=BLfBI95NQw']] and from second

    with open("./license.dat", 'r') as f :
              f = (f.read())
        print (f)

i got this

[Registration information]
Name=nikam
Key=KDWOE

i need result need to get nilkam from name and KDWOE from key

I'm not sure what a .dat file is, and you don't specify, but given your example it looks like the configparser library might work for you.

import configparser

config = configparser.ConfigParser()
config.read('./license.dat')

print(config['Registration information']['Name'])
print(config['Registration information']['Key'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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