Home > Mobile >  get data from .dat file with python
get data from .dat file with python

Time:07-01

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

CodePudding user response:

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'])
  • Related