Skip to content

Instantly share code, notes, and snippets.

Last active December 7, 2018 03:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
import urllib.request as req
import os.path, random
import json
import yaml
import codecs
import openpyxl
print("===== JSONのパース =====")
url_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("===== CSVのパース =====")
filename = "csv.csv"
csv =, "r", "utf-8").read()
data = []
rows = csv.split("\r\n")
for row in rows:
if row == "":continue
cells = row.split(",")
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:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment