Hi
I am trying to pass 2 input parameters from shell script to python API end point ,but not passing what i expected when print those inputs .Please advise
I'm assuming you don't want the quotes around variable id but you do want to keep any leading (or internal) spaces(s).
Remove echo if python invocation is how you want.
sort $sites/data.txt | uniq | while IFS=$',\t\n' read id tid
do
# remove quotes from around id
id=${id#*\"}
id=${id%\"*}
echo "id: \"${id}\""
echo "tid: \"${tid}\""
echo python test.py /data'{" Id":"'"${id}"'", "group":"get", "item":"'"${tid}"'"],"fields":[" Id","type"," Parameters","xx"]}'
done
Python does not need help from the shell or another utilities to do what it is shown:
Modify test.py to handle the parsing, the sorting, the processing of uniqueness and the tokenization of the data.txt.
Here's an example using a print() for visualization.
with open('data.txt') as f:
records = set()
for line in f:
id, tid = line.strip().split('" ,')
id = id[1:]
records.add((id,tid))
for r in sorted(records):
id, tid = r
print("id: {}, tid: {}".format(id, tid))
Hi ,
When extracting data from API,there are 2 nested groups having same group name [group:url,title and item with different values].I want to merge these 2 nested groups to 1 or individual nested groups otherwise for each unique site more than 2 records .Please advise
Hi ,
I have tried with python json library,but getting the following error
Traceback (most recent call last):
File "a11.py", line 4, in <module>
data = json.load(data_file)
File "/usr/lib/python2.7/json/__init__.py", line 291, in load
**kw)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 367, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
test.py
import json
with open('/home/test/a.json') as data_file:
data = json.load(data_file)
for a in data['events']:
a['customParameters'] = a['customParameters'][1]
new_some_json_string = json.dumps(data, indent=2)
print(new_some_json_string)
data file :a.json
"events": [
{
"time": 1522224314,
"customParameters": [
{
"group": "url",
"item": "https://www.xxx.a/3990154.html"
},
{
"group": "title",
"item": "a90189275"
}
],
"site": "xx877",
"type": "test",
"userId": "xx1"
}
]
expecting the output
site type userId group item group1 item1
xx877 test xx1 url https... title a90189275