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'])
Related
I need to extract the uid from a .sgm file, I tried the below code but it doesn't, work can anybody help?
Sample .sgm file content:
<miscdoc n='1863099' uid='0001863099_20220120' type='seccomlett' t='frm' mdy='01/20/2022'><rname>Kimbell Tiger Acquisition Corp, 01/20/2022</rname>
<table col='2' type='txt'>
<colspec col='1' colwidth='*'>
<colspec col='2' colwidth='2*'>
<tname>Meta-data</tname>
<tbody>
<row><entry>SEC-HEADER</entry><entry>0001104659-22-005920.hdr.sgml : 20220304</entry></row>
<row><entry>ACCEPTANCE-DATETIME</entry><entry>20220120160231</entry></row>
<row><entry>PRIVATE-TO-PUBLIC</entry></row>
<row><entry>ACCESSION-NUMBER</entry><entry>0001104659-22-005920</entry></row>
<row><entry>TYPE</entry><entry>CORRESP</entry></row>
<row><entry>PUBLIC-DOCUMENT-COUNT</entry><entry>1</entry></row>
<row><entry>FILING-DATE</entry><entry>20220120</entry></row>
<row><entry>FILER</entry></row>
code I tried:
import os
# Folder Path
path = "Enter Folder Path"
# Change the directory
os.chdir(path)
# Read text File
def read_file(file_path):
with open(file_path, 'r') as f:
print(f.read())
# iterate through all file
for file in os.listdir():
# Check whether file is in text format or not
if file.endswith(".sgm"):
if 'uid' in file:
print("true")
file_path = f"{path}\{file}"
# call read text file function
read_file(file_path)
I need extract the uid value from the above sgm file, is there any other way I could do this? what should I change in my code?
SGM format may just by an XML superset. If it isn't then for this particular case (and if one could rely on the format being as shown in the question) then:
import re
def get_uid(filename):
with open(filename) as infile:
for line in map(str.strip, infile):
if line.startswith('<miscdoc'):
if uid := re.findall("uid='(.*?)'", line):
return uid[0]
I have the following script to import a text-based file.
with open("test.tsv") as import_lines:
for line in import_lines:
line_parsed = line.strip().split("\t")
print(line_parsed[0])
The output of this file is something like this:
2006\u5E74\u5B66\u672F\u6587\u7AE0
2006\u5E74\u5B78\u8853\u6587\u7AE0
2006\u5E74\u5B78\u8853\u6587\u7AE0
I assumed that decoding was as simple as:
print(line_parsed[0].encode().decode("utf-8"))
But this results in the exact same result.
I did notice that:
print(line_parsed[0].encode())
results in:
b'2006\\u5E74\\u5B66\\u672F\\u6587\\u7AE0'
b'2006\\u5E74\\u5B78\\u8853\\u6587\\u7AE0'
b'2006\\u5E74\\u5B78\\u8853\\u6587\\u7AE0'
You don't need to encode().decode(), open your file in binary mode:
with open("test.tsv", "rb") as import_lines:
for line in import_lines:
line_parsed = line.strip().decode('unicode-escape').split("\t")
print(line_parsed[0])
Output:
2006年学术文章
2006年學術文章
2006年學術文章
print(line_obj["label"].encode().decode("unicode-escape"))
I'm new to Python and the task I am performing is to extract a specific key value from a list of .iris ( which contains the list of nested dictionary format) files in a specific directory.
I wanted to extract the specific value and save it as a new .csv file and repeat it for all other files.
Below is my sample of .iris file from which I should extract only for the these keys ('uid','enabled','login','name').
{"streamType":"user",
"uid":17182,
"enabled":true,
"login":"xyz",
"name":"abcdef",
"comment":"",
"authSms":"",
"email":"",
"phone":"",
"location":"",
"extraLdapOu":"",
"mand":997,
"global":{
"userAccount":"View",
"uid":"",
"retention":"No",
"enabled":"",
"messages":"Change"},
"grants":[{"mand":997,"role":1051,"passOnToSubMand":true}],
I am trying to convert the .iris file to .json and reading the files one by, but unfortunately, I am not getting the exact output as desired.
Please, could anyone help me?
My code (added from comments):
import os
import csv
path = ''
os.chdir(path)
# Read iris File
def read_iris_file(file_path):
with open(file_path, 'r') as f:
print(f.read())
# iterate through all files
for file in os.listdir():
# Check whether file is in iris format or not
if file.endswith(".iris"):
file_path = f"{path}\{file}"
# call read iris file function
print(read_iris_file(file_path))
Your files contain data in JSON format, so we can use built-in json module to parse it. To iterate over files with certain extension you can use pathlib.glob() with next pattern "*.iris". Then we can use csv.DictWriter() and pass "ignore" to extrasaction argument which will make DictWriter ignore keys which we don't need and write only those which we passed to fieldnames argument.
Code:
import csv
import json
from pathlib import Path
path = Path(r"path/to/folder")
keys = "uid", "enabled", "login", "name"
with open(path / "result.csv", "w", newline="") as out_f:
writer = csv.DictWriter(out_f, fieldnames=keys, extrasaction='ignore')
writer.writeheader()
for file in path.glob("*.iris"):
with open(file) as inp_f:
data = json.load(inp_f)
writer.writerow(data)
Try the below (the key point here is loading the iris file using ast)
import ast
fields = ('uid','enabled','login','name')
with open('my.iris') as f1:
data = ast.literal_eval(f1.read())
with open('my.csv','w') as f2:
f2.write(','.join(fields) + '\n')
f2.write(','.join(data[f] for f in fields) + '\n')
my.csv
uid,enabled,login,name
17182,true,xyz,abcdef
enter image description here
I wrote the code like this:
intents = json.loads(open('intents.json').read())
Check your intents.json file is in the same folder on which you python file is.
you can use, for example, the os builf-in module to check on the existence of file and os.path for path manipulation. Check the official doc at https://docs.python.org/3/library/os.path.html
import os
file = 'intents.json'
# location of the current directory
w_dir = os.path.abspath('.'))
if os.path.isfile(os.path.join(w_dir, file)):
with open(file, 'r') as fd:
fd.read()
else:
print('Such file does not exist here "{}"...'.format(w_dir))
You can try opening the file using the normal file operation and then use json.load or json.loads to parse the data as per your needs. I may be unfamiliar with this syntax to the best of my knowledge your syntax is wrong.
You can open the file like this:
f = open(file_name)
Then parse the data:
data = json.load(f)
You can refer to this link for more info and reference
https://www.geeksforgeeks.org/read-json-file-using-python/
im quite new to programing and i don´t understand this error message i get, file was loaded in the wrong encoding utf-8 or it´s not really a error message in the code but i get it in my new .txt file where i write all found keywords to. The .txt file get upp to 4000+ rows with information that i sort to Excel in another program and later send it to Access. What dose the message mean and is thhere a way to fix it? Thanks
im using pycharm with anaconda36
import glob
def LogFile(filename, tester):
data = []
with open(filename) as filesearch: # open search file
filesearch = filesearch.readlines() # read file
file = filename[37:]
for line in filesearch:
if tester in line: # extract "Create Time"
short = line[30:]
data.append(short) # store all found wors in array
print (file)
with open('Msg.txt', 'a') as handler: # create .txt file
for i in range(len(data)):
handler.write(f"{file}|{data[i]}")
# open with 'w' to "reset" the file.
with open('LogFile.txt', 'w') as file_handler:
pass
# ---------------------------------------------------------------------------------
for filename in glob.glob(r'C:\Users\Documents\Access\\GTX797\*.log'):
LogFile(filename, 'Sending Request: Tester')
I just had the same error in pyCharm and fixed it by specifying UTF-8 when creating the file. You will need to import codecs to do this.
import codecs
with codecs.open(‘name.txt', 'a', 'utf-8-sig') as f: