How to modify a JSON file in a script? - python

I am trying to edit Chrome's preferences using command line. The file is a JSON file and I want to edit the data below:
{"browser":{"last_redirect_origin":""}}
to
{"browser":{"enabled_labs_experiments":["ssl-version-max#2"],"last_redirect_origin":""}}
I was using sed command earlier to accomplish this but want to know how this can be done using python. This was the command I was using:
sed -i '.bak' -e 's|\(\"browser\"\):{\(\".*origin\":\"\"\)}|\1:{\"enabled_labs_experiments\":[\"ssl-version-max#2\"],\2}|' ~/Library/Application\ Support/Google/Chrome/Local\ State
The reason I can't use jq is that it is not native to macs and will need installation. I am not able to understand how to do this with python.
I will really appreciate it if someone could help me with this or point me in the right direction.
EDIT
This is what my python script looks like:
import json
jsonData = json.loads(open('/Users/username/Library/Application Support/Google/Chrome/Local state').read())
if 'enabled_labs_experiments' in jsonData['browser']:
if 'ssl-version-max#2' in jsonData['browser']['enabled_labs_experiments']:
print('Exist')
else:
jsonData['browser']['enabled_labs_experiments'] = ['ssl-version-max#2']
print('Added')
After the changes are made, I would like to commit the changes to the file.

Why are you using regex why are you not using the built in json module in python?
import json
d = json.loads(path_to_your_file)
d["browser"]["enabled_labs_experiments"] = ["ssl-version-max#2"]

import json
with open(path_to_json_file) as f:
data = f.read()
d = json.loads(data)
d["browser"]["enabled_labs_experiments"] = ["ssl-version-max#2"]
with open(path_to_json_file, 'w') as f:
f.write(json.dumps(d))

Related

Curl command with options in Python 3 [duplicate]

This question already has answers here:
Python basics - request data from API and write to a file
(2 answers)
Closed 2 years ago.
In requests library, if you need to execute
curl http://www.example.com/file.xlsx
The command is
response = requests.get('http://www.example.com/file.xlsx')
What If I want to execute the command with -O option?
curl http://www.example.com/file.xlsx -O
How do I achieve that?
There's no explicit "-O" = write to the same filename.
You if you need that, you can store the url in a variable
and use several ways of getting it. A lazy way is using rpartition('/')[2] on
the url string.
The rest of the code to quickly save the result is here:
import requests
from pathlib import Path
response = requests.get('http://www.example.com/file.xlsx')
Path('file.xlsx').write_bytes(response.content)
# or if you want to have the file name extracted use this less readable line
# Path(response.url.rpartition('/')[2]).write_bytes(response.content)
To achieve the use case of CURL's -O option in Python, you have to write few lines of code like,
import requests
r = requests.get('https://your.url.com/path')
with open('path.txt','w') as fd:
fd.write(r.text)
I'm not sure requests supports a flag for this purpose. However, you can try doing something like:
from pathlib import Path
p = Path('http://www.example.com/file.xlsx')
r = requests.get(str(p))
Path(p.name).write_bytes(r.content)

How to read and change BAM files from a Python script?

I'm planning on using a Python script to change different BAM (Binary Alignment Map) file headers. Right now I am just testing the output of one bam file but every time I want to check my output, the stdout is not human readable. How can I see the output of my script? Should I use samtools view bam.file on my script? This is my code.
#!/usr/bin/env python
import os
import subprocess
if __name__=='__main__':
for file in os.listdir(os.getcwd()):
if file == "SRR4209928.bam":
with open("SRR4209928.bam", "r") as input:
content = input.readlines()
for line in content:
print(line)
Since BAM is a binary type of SAM, you will need to write something that knows how to deal with the compressed data before you can extract something meaningful from it. Unfortunately, you can't just open() and readlines() from that type of file.
If you are going to write a module by yourself, you will need to read Sequence Alignment/Map Format Specification.
Fortunately someone already did that and created a Python module: You can go ahead and check pysam out. It will surely make your life easier.
I hope it helps.

How to execute a python file using txt file as input (to parse data)

I tried looking inside stackoverflow and other sources, but could not find the solution.
I am trying to run/execute a Python script (that parses the data) using a text file as input.
How do I go about doing it?
Thanks in advance.
These basics can be found using google :)
http://pythoncentral.io/execute-python-script-file-shell/
http://www.python-course.eu/python3_execute_script.php
Since you are new to Python make sure that you read Python For Beginners
Sample code Read.py:
import sys
with open(sys.argv[1], 'r') as f:
contents = f.read()
print contents
To execute this program in Windows:
C:\Users\Username\Desktop>python Read.py sample.txt
You can try saving the input in the desired format (line-wise) file, and then using it as an alternative to STDIN (standard input file) using the file subcommand with Python
python source.py file input.txt
Note: To use it with input or source files in any other directory, use complete file location instead of file names.

How to run another python file and store/search it's output?

So I want to make an automatic SQL injector using sqlmap.
What I want to do is after a SQL vulnerable link found it will launch sqlmap with parameters[ -u %s(vulnerable link) ] That's good. I think I'm going to do it with os.system(if there is better way please tell me). That part is ok.
But after that the sqlmap gives you database names. How can I catch that database names? Or maybe save all the output to a txt file and make it search for database names.(but it could be bad)
Don't sure It will help you but you can export all Python output of a Python script. To do that you can set sys.stdout to an file object. Here an example:
import sys
sys.stdout = open('file', 'w')
print 'test'

Conversion of shell script to python script

Say I have the following HTML script:
<head>$name</head>
And I have the following shell script which replaces the variable in the HTML script with a name
#/bin/bash
report=$(cat ./a.html)
export name=$(echo aakash)
bash -c "echo \"$report\""
This works.
Now I have to implement the shell script in Python so that I am able to replace the variables in the HTML file and output the replaced contents in a new file. How do I do it?
An example would help. Thanks.
It looks like you're after a templating engine, but if you wanted a straight forward, no thrills, built into the standard library, here's an example using string.Template:
from string import Template
with open('a.html') as fin:
template = Template(fin.read())
print template.substitute(name='Bob')
# <head>Bob</head>
I thoroughly recommend you read the docs especially regarding escaping identifier names and using safe_substitute and such...
with open('a.html', 'r') as report:
data = report.read()
data = data.replace('$name', 'aakash')
with open('out.html', 'w') as newf:
newf.write(data)
Firstly you could save your html template like:
from string import Template
with open('a.html') as fin:
template = Template(fin.read())
Then if you want to substitute variables one at a time, you need to use safe_substitute and cast the result to a template every time. This wont return a key error even when a key value is not specified.
Something like:
new=Template(template.safe_substitute(name="Bob"))
After this , the new template is new , which needs to be modified again if you would want.

Categories

Resources