You are on page 1of 6

Tarea 3

Primera parte
1. Código:
with open('dog_breeds.txt') as f:
lines = f.readlines()
print(len(lines))

2. Código

import sqlalchemy
#a>
#Conectando a base de datos
from sqlalchemy import create_engine
engine = create_engine('sqlite:///:memory:', echo=True)
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
#b>
#Creando formato de tabla
from sqlalchemy import Column, Integer, String
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
nombre = Column(String)
apellidos = Column(String)
edad = Column(String)
DNI = Column(String)
def __repr__(self):
return "<User(nombre='%s', apellidos='%s', edad='%s', DNI='%s')>"
% (self.nombre, self.apellido, self.edad, self.DNI)
#crear tablas
Base.metadata.create_all(engine)
#Creando sesión
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()
#c>
#Añadiendo usuario
ed_user = User(nombre=input("Ingrese su nombre: "),
apellidos=input("Ingrese sus apellidos: "), edad=input("Ingrese su edad:
")
,DNI=input("Ingrese su DNI "))
session.add(ed_user)
#d>
for nombre, apellidos, edad, DNI in session.query(User.nombre,
User.apellidos, User.edad, User.DNI):
print(nombre, apellidos, edad, DNI)
3. Código

import json, csv, requests

response =
requests.get("https://jsonplaceholder.typicode.com/todos")
data = json.loads(response.text)

#Creamos un archivo csv donde se escribirá


data_file = open('todos_datos.csv','w')
csv_writer = csv.writer(data_file)

#contador para escribir en el archivo csv


count = 0
for tod in data:
if count == 0:
header = tod.keys()
csv_writer.writerow(header)
count += 1
csv_writer.writerow(tod.values())
data_file.close()
Segunda parte
1.
2.
import requests, json
people = requests.get('http://api.open-notify.org/iss-pass.json?
lat=45.0&lon=-122.3&alt=20&n=10&callback=')
#people_json = people.json()
people_json = json.loads(people.text)
print(len(people_json))
print(people_json)
print(type(people_json))
total = 0
for p in people_json['response']:
dur = int(p['duration'])
print(dur, 'segundos')
total += dur
print('El tiempo total es:',total, 'segundos')
3.
import requests
from bs4 import BeautifulSoup
url = 'https://www.imdb.com/list/ls024149810/'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')

movie_gross = []
gross = soup.find_all('span',{'name':'nv'})
count = 0

for g in gross:
if count % 2 != 0:
movie_gross.append(g.text.strip())
count += 1
from re import split
total=0
for e in movie_gross:
result = ''.join([i for i in e if i.isdigit() and '.'])
r=int(result)/100
total+=r
print('El total es: $',total,'M')

You might also like