Skip to content

Instantly share code, notes, and snippets.

@baobao
Last active December 7, 2018 03:05
Show Gist options
  • Save baobao/bff761c7eef52baf6eee9e7d669723cf to your computer and use it in GitHub Desktop.
Save baobao/bff761c7eef52baf6eee9e7d669723cf to your computer and use it in GitHub Desktop.
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