This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.request as req | |
import os.path, random | |
import json | |
import yaml | |
import codecs | |
import openpyxl | |
print("===== JSONのパース =====") | |
url_json = "http://api.aoikujira.com/hyakunin/get.php?fmt=json" | |
filename = "json.json" | |
if not os.path.exists(filename): | |
req.urlretrieve(url_json, filename) | |
obj = open(filename, "r", encoding="utf-8") | |
json_data = json.load(obj) | |
# 要素をランダムに選ぶ | |
data = random.choice(json_data) | |
print(data["no"], data["sakusya"]) | |
print("===== YAMLのパース =====") | |
# jsonをYAMLに変換 | |
yaml_str = yaml.dump(json_data) | |
yaml_data = yaml.load(yaml_str) | |
# # 百人一首の作者だけを出力 | |
print(yaml_data[0]["sakusya"]) | |
print("===== CSVのパース =====") | |
filename = "csv.csv" | |
csv = codecs.open(filename, "r", "utf-8").read() | |
data = [] | |
rows = csv.split("\r\n") | |
for row in rows: | |
if row == "":continue | |
cells = row.split(",") | |
data.append(cells) | |
print(data[0][1], data[0][2]) | |
print("===== Excelのパース =====") | |
filename = "excel.xlsx" | |
book = openpyxl.load_workbook(filename) | |
# 先頭のシートを得る | |
sheet = book.worksheets[0] | |
data = [] | |
for row in sheet.rows: | |
# 1行目 | |
r0 = row[0].value | |
# 3行目 | |
r2 = row[2].value | |
# 空セルを除く | |
if not r0 is None and not r2 is None: | |
data.append([ | |
row[0].value, | |
row[2].value | |
]) | |
print(data[0]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment