<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 stdin And stdout
Data can be piped into Python using sys.stdin and sys.stdout
Step1:  Reading Files
The Basics Of Text Files
Use the open function to open a file
Step2:  Use a with block to ensure that files are closed
Step3:  Delimited Files
It's easy to work with delimited files.
Step5:  HTML And The Parsing Thereof
Step7:  Using APIs
JSON (And XML)
APIs transfer data in a certain format--usually JSON sometimes XML.
Step8:  Using An Unauthenticated API
<ASSISTANT_TASK:>
Python Code:
# egrep.py
import sys, re
# sys.argv is the list of command-line arguments
# sys.argv[0] is the name of the program itself
# sys.argv[1] will be the regex specified at the command line
regex = sys.argv[1]
# for every line passed into the script
for line in sys.stdin:
    # if it matches the regex, write it to stdout
    if re.search(regex, line):
        sys.stdout.write(line)
# line_count.py
import sys
count = 0
for line in sys.stdin:
    count += 1
# print goes to sys.stdout
print(count)
!type SomeFile.txt | python egrep.py "[0-9]" | python line_count.py
Explanation: stdin And stdout
Data can be piped into Python using sys.stdin and sys.stdout:
End of explanation
# 'r' means read-only
file_for_reading = open('reading_file.txt', 'r')
# 'w' is write—will destroy the file if it already exists!
file_for_writing = open('writing_file.txt', 'w')
# 'a' is append—for adding to the end of the file
file_for_appending = open('appending_file.txt', 'a')
# don't forget to close your files when you're done
file_for_writing.close()
Explanation: Reading Files
The Basics Of Text Files
Use the open function to open a file:
End of explanation
with open('SomeFile.txt', 'r') as f:
    for line in f:
        print(line.strip())
# After with block, file is closed
Explanation: Use a with block to ensure that files are closed:
End of explanation
import csv
with open('stocks.csv', 'r') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        date = row[0]
        symbol = row[1]
        closing_price = float(row[2])
        print(date, symbol, closing_price)
with open('stocks-headers.csv', 'r') as f:
    reader = csv.DictReader(f, delimiter=':')
    for row in reader:
        date = row['date']
        symbol = row['symbol']
        closing_price = float(row['closing_price'])
        print(date, symbol, closing_price)
Explanation: Delimited Files
It's easy to work with delimited files.
End of explanation
some_html = 
<html>
<head>
<title>A web page</title>
</head>
<body>
<p id="author">Joel Grus</p>
<p id="subject" class="important">Data Science</p>
</body>
</html>
from bs4 import BeautifulSoup
import requests
html = requests.get('http://www.example.com').text
html = some_html
soup = BeautifulSoup(html, 'html5lib')
first_paragraph = soup.find('p')
first_paragraph
soup.p.text, soup.p.text.split()
soup.p['id']
soup.p.get('id')
soup.find_all('p')
[p for p in soup('p') if p.get('id')]
soup('p', {'class' : 'important'})
soup('p', 'important')
[p for p in soup('p') if 'important' in p.get('class', [])]
Explanation: HTML And The Parsing Thereof
End of explanation
import json
json_string = { "title" : "Data Science Book",
                   "author" : "Joel Grus",
                   "publicationYear" : 2014,
                   "topics" : [ "data", "science", "data science"] }
# parse the JSON into a Python Dictionary
dict = json.loads(json_string)
if 'data science' in dict['topics']:
    print(dict)
Explanation: Using APIs
JSON (And XML)
APIs transfer data in a certain format--usually JSON sometimes XML.
End of explanation
endpoint = 'https://api.github.com/users/joelgrus/repos'
repos = json.loads(requests.get(endpoint).text)
Explanation: Using An Unauthenticated API
End of explanation
<END_TASK>