Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a file with this pattern :
[account.invoice.set_num]
job_size = 0
trans_size = 100
[commission.invoice_second.create_full]
j_size = 0
[commission.invoice_principal.finalize]
j_size = 12
in_directory = /to/the/directory
I want to transform this pattern to a text like :
ACCOUNT_INVOICE_SET_NUM_JOB_SIZE = 0
ACCOUNT_INVOICE_SET_NUM_TRANS_SIZE = 100
COMMISSION_INVOICE_SECOND_CREATE_FULL_J_SIZE=0
COMMISSION_INVOICE_PRINCIPALE_FINALIZE_J_SIZE=12
COMMISSION_INVOICE_PRINCIPALE_FINALIZE_IN_DIRECTORY=/to/the/directory
I try to do that in Bash unix or in Python.
I don't konw what is the best/easiest way to do that.
It's quite feasible with config.ConfigParser features:
from configparser import ConfigParser
config = ConfigParser()
config.read('yourfile')
config_lines = ''
for section in config.sections():
s_key = section.replace('.', '_') # transformed section key
for k, v in config.items(section):
config_lines += f'{s_key}_{k}'.upper() + f'={v}\n'
print(config_lines)
The output:
ACCOUNT_INVOICE_SET_NUM_JOB_SIZE=0
ACCOUNT_INVOICE_SET_NUM_TRANS_SIZE=100
COMMISSION_INVOICE_SECOND_CREATE_FULL_J_SIZE=0
COMMISSION_INVOICE_PRINCIPAL_FINALIZE_J_SIZE=12
COMMISSION_INVOICE_PRINCIPAL_FINALIZE_IN_DIRECTORY=/to/the/directory
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
import json
f = open('C:\Users\Hello\Desktop\usecase2.json',encoding = 'utf-8')
data = json.load(f)
print(data)
-Raw Data sheet contains the raw data to be parsed and arrange as per "Desired Output" sheet
-The number of iteration data for each Pur Lot is not same, however it is required to capture the entire raw da -you have to create a JSON file to capture entire data of Raw Data with the key as Pur Lot.
data
desired output
If you're looking to convert JSON (essentially text) to a python dict , then I would suggest you use this:
**
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y["age"])
**
then you can call y['keys'] = values to use data
here , y['name'] = "John"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to add all elements in a list and print that out. Is it possible to make this
x_irp = []
for i in range(1,5):
for r in range(1,7):
for p in range(1,4):
a = "x_{}_{}_{}".format(i,r,p)
x_irp.append(a)
print(x_irp)
['x_1_1_1', 'x_1_1_2', 'x_1_1_3', 'x_1_2_1', 'x_1_2_2', 'x_1_2_3', 'x_1_3_1', 'x_1_3_2', 'x_1_3_3', 'x_1_4_1', 'x_1_4_2', 'x_1_4_3', 'x_1_5_1', 'x_1_5_2', 'x_1_5_3', 'x_1_6_1', 'x_1_6_2', 'x_1_6_3', 'x_2_1_1', 'x_2_1_2', 'x_2_1_3', 'x_2_2_1', 'x_2_2_2', 'x_2_2_3', 'x_2_3_1', 'x_2_3_2', 'x_2_3_3', 'x_2_4_1', 'x_2_4_2', 'x_2_4_3', 'x_2_5_1', 'x_2_5_2', 'x_2_5_3', 'x_2_6_1', 'x_2_6_2', 'x_2_6_3', 'x_3_1_1', 'x_3_1_2', 'x_3_1_3', 'x_3_2_1', 'x_3_2_2', 'x_3_2_3', 'x_3_3_1', 'x_3_3_2', 'x_3_3_3', 'x_3_4_1', 'x_3_4_2', 'x_3_4_3', 'x_3_5_1', 'x_3_5_2', 'x_3_5_3', 'x_3_6_1', 'x_3_6_2', 'x_3_6_3', 'x_4_1_1', 'x_4_1_2', 'x_4_1_3', 'x_4_2_1', 'x_4_2_2', 'x_4_2_3', 'x_4_3_1', 'x_4_3_2', 'x_4_3_3', 'x_4_4_1', 'x_4_4_2', 'x_4_4_3', 'x_4_5_1', 'x_4_5_2', 'x_4_5_3', 'x_4_6_1', 'x_4_6_2', 'x_4_6_3']
I must print this list in a form as x_1_1_1 + x_1_1_2 + x_1_1_3....
It has to continue until the last element of list.
How about using
print(" + ".join(x_irp))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
i would like to search and replace Ids in two different txt files.
So i have those .txt files.
OLDIDS.txt which seems like:
12F
130
132
106
100
...
and the other file MASTERIDS.txt where the old and new Ids where splittet in columns like: (LEFT old ids, RIGHT new ids)
100 132
12F 1FF
106 256
... ...
What I want to do is to open OLDIDS.txt like
f2 = open('OLDIDS.txt', 'w')
and search for the first id in the first line (which is 12F) find this in the second line of MASTERIDS.txt and write the new ID 1FF to the second line of newFile.txt.
I am converting the data in your MASTERIDS.txt to a dictionary. Key is the old id and value is the new ID. Then search for the dict for the new id using the value from OLDIDS.txt
Demo:
with open("PATH_TO_OLDIDS.txt", "r") as src:
data = src.readlines()
d ={}
with open("PATH_TO_MASTERIDS.txt", "r") as toReplaceSRC:
for i in toReplaceSRC.readlines():
val = i.split()
d[val[0].strip()] = val[1].strip()
for i in data:
toReplace = d.get(i.strip(), None)
if toReplace:
print(i.strip(), " = ", toReplace)
Output:
12F = 1FF
106 = 256
100 = 132
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
This is a snippet of the output:
{...,"resultMap":
{..."SEARCH_RESULTS":
[{..."resultList":[
{"userClientId":"1"","preferenceValues":["48","51","94"],"MyDate":"7/26/2017 8:30:00 AM"},
{"userClientId":"2","preferenceValues":["42","11","84"],"MyDate":"7/26/2017 9:40:00 AM"},
{"userClientId":"3","preferenceValues":["4","16","24"],"MyDate":"7/26/2017 4:20:00 PM"},
{"userClientId":"4","preferenceValues":["7","2","94"],"MyDate":"7/27/2017 8:00:00 AM"},
{"userClientId":"1","preferenceValues":["48","22","94"],"MyDate":"7/27/2017 1:50:00 PM"},
{"userClientId":"2","preferenceValues":["42","11"],"MyDate":"7/27/2017 2:00:00 PM"},
{"userClientId":"3","preferenceValues":["4","24"],"MyDate":"7/27/2017 6:15:00 PM"},
{"userClientId":"4","preferenceValues":"7","MyDate":"7/27/2017 9:30:00 PM"}]
}]
}
}
I am looking to get a variable pageIdCount that is in dictionary format, where the key is page_id and the values are a counts of occurrences of page_id, by user_id. So for userId 1 it should look like:
{"userClientId":"1","preferenceValues":{48:2, 51:1, 94:2, 22:1}}
Note that when there is only 1 variable inside preferenceValues- there are no brackets. There is also a field "preferenceValue" where there are no brackets no matter what and it is identical to "preferenceValues" otherwise.
Is that possible?
In Python 2.7, I specify user, password and url and then I have the following:
req = requests.post(url = url, auth=(user, password))
ans = req.json()
print ["resultMap"]["SEARCH_RESULTS"][0]["resultList"]
Any help is greatly appreciated.
your_data # this is your data
final_data = {}
for line in yourdata:
uid = line["userId"]
pids = line["PageId"]
if uid not in final_data :
final_data[uid] = {}
for pid in pids :
pid = int(pid)
if pid not in final_data[uid]:
final_data[uid][pid]=0
final_data[uid][pid] += 1
res = [{"userId":uid,"PageIDCount":pids} for uid,pids in final_data.items()]
I suppose you are beginning, if so, the most tricky part of this code will probably be the last line, it uses list comprehension. here is a good lesson about it.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Tell me how to replace python data
a.txt
abcd.com 0.0
* 6.6999306E7
asdf.com 1.50744025E8
asfd.df.com 1.93139033E8
fdsa.com 9.07938122E8
bank.com 2.638989462E9
fire.com 4.151822166E9
ms.com 7.026079907E9
How can I read the a.txt file and make it in the following format?
Output result :
['abcd.com', 0],
['*', 66999306],
['asdf.com', 150744025],
['asfd.df.com', 193139033],
['fdsa.com', 907938122],
['bank.com', 2638989462],
['fire.com', 4151822166],
['ms.com', 7026079907]
file = open('a.txt', 'r')
l = []
for line in file:
l.append( line.split())
Then if you want the second part to be integer, you can use list comprehension:
l = [ [i[0], int(float(i[1]))] for i in l]
output
[['abcd.com', 0],
['*', 66999306],
['asdf.com', 150744025],
['asfd.df.com', 193139033],
['fdsa.com', 907938122],
['bank.com', 2638989462],
['fire.com', 4151822166],
['ms.com', 7026079907] ]