Python fun!

Recently dove into Python and wanted to share the fun!


Playing with the Chuck Norris API and writing chuck quotes to a file:

import requests

URL = ''
outFile = 'chucknorris.html'

open(outFile, 'w').close()
with open(outFile,'a') as o:
    for i in range(1000):
        json_response = requests.get(URL).json()
        o.write('<h4>' + json_response['value'] + '</h4>\n')


Surrounding each line in a file with single quotes:

Execute with python3 input.txt output.txt

import sys
import re

def manipulate_line(in_line):
    res = re.sub(r"(.+)", r"'\g<0>',", in_line)
    return res

if __name__ == '__main__':

    inFile = sys.argv[1]
    outFile = sys.argv[2]

    with open(inFile, 'r') as inf, open(outFile, 'w') as outf:
        for line in inf:
            res = manipulate_line(line)
            outf.write("'%s'\n" % line.rstrip('\n'))


Leave a Reply

Your email address will not be published.