XML, JSON, YAML, CSV 파일을 읽고 저장하고 분석하는 방법에 대해서 공부하였다.

여기 나오는 내용은 "파이썬을 이용한 머신러닝, 딥러닝, 실전 개발 입문"에 나오는 내용이다.



========================== Python ==========================

### 텍스트 데이터와 바이너리 데이터

# 파일 이름과 데이터

file_name = 'b.txt'

data = 100

# 쓰기

with open(file_name, 'wb') as f:

    a = f.write(bytearray([data]))

    print(a)






### 파이썬으로 XML 분석하기

from bs4 import BeautifulSoup

import urllib.request as req

import os.path


# XML 다운로드

url = "http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108"

savename = "forecast.xml"

if not os.path.exists(savename):

    req.urlretrieve(url, savename)


# BeautifulSoup으로 분석하기

xml = open(savename, "r", encoding="utf-8").read()

soup = BeautifulSoup(xml, "html.parser")


# 각 지역 확인하기

info = {}

for location in soup.find_all("location"):

    name = location.find('city').string

    weather = location.find('wf').string

    print(weather in info)

    if not (weather in info):

        info[weather] = []

    info[weather].append(name)


# 각 지역의 날씨를 구분해서 출력하기

for weather in info.keys():

    print("+", weather)

    for name in info[weather]:

        print("| - ", name)






# json 데이터 분석하기

import urllib.request as req

import os.path, random, json


# json 데이터 내려받기

url = "https://api.github.com/repositories"

savename = "repo.json"

if not os.path.exists(savename):

    req.urlretrieve(url, savename)


# json 파일 분석하기

s = open(savename, "r", encoding="utf-8")

items = json.load(s)

    # print(type(s))  # <class '_io.TextIOWrapper'>

    # print(type(items))  # <class 'list'>

# 또는

s = open(savename, "r", encoding="utf-8").read()

items = json.loads(s)

    # print(type(s))  # <class 'str'>

    # print(type(items))  # <class 'list'>


# 출력하기

for item in items:

    print(item["name"] + " - " + item["owner"]["login"])




### json 형식으로 출력하기

import json

price = {

    "date": "2017-05-10",

    "price": {

        "Apple": 80,

        "Orange": 55,

        "Banana": 40

    }

}

s = json.dumps(price)

print(s)






### YAML 읽기

import yaml


# YAML 정의하기

yaml_str = """

Date: 2017-03-10

PriceList:

    -

        item_id: 1000

        name: Banana

        color: yellow

        price: 800

    -

        item_id: 1001

        name: Orange

        color: orange

        price: 1400

    -

        item_id: 1002

        name: Apple

        color: red

        price: 2400

"""


# YAML 분석하기

data = yaml.load(yaml_str)


# 이름과 가격 출력하기

for item in data["PriceList"]:

    print(item["name"], item["price"])




### YAML 쓰기

import yaml

# 파이썬 데이터를 YAML 데이터로 출력하기

customer = [

    {"name": "InSeong", "age": "24", "gender": "man"},

    {"name": "Akatsuki", "age": "22", "gender": "woman"},

    {"name": "Harin", "age": "23", "gender": "man"},

    {"name": "Yuu", "age": "31", "gender": "woman"}

]


# 파이썬 데이터를 YAML 데이터로 변환하기

yaml_str = yaml.dump(customer)

print(yaml_str)

print("--- --- ---")


# YAML 데이터를 파이썬 데이터로 변환하기

data = yaml.load(yaml_str)


# 이름 출력하기

for p in data:

    print(p["name"])




### YAML Alias

import yaml


# 문자열로 YAML을 정의합니다.

yaml_str = """

# 정의

color_def:

    - &color1 "#FF0000"

    - &color2 "#00FF00"

    - &color3 "#0000FF"

# 별칭 테스트

color:

    title: *color1

    body: *color2

    link: *color3

"""


# YAML 데이터 분석하기

data = yaml.load(yaml_str)


# 별칭이 전개됐는지 테스트하기

print("title=", data["color"]["title"])

print("body=", data["color"]["body"])

print("link=", data["color"]["link"])






# CSV 파일 읽기

import codecs


# EUC_KR로 저장된 csv 파일 읽기

filename = "list-euckr.csv"

csv = codecs.open(filename, "r", "euc_kr").read()


# csv를 파이썬 리스트로 변환하기

data = []

rows = csv.split("\r\n")

for row in rows:

    if row == "":

        continue

    cells = row.split(",")

    data.append(cells)


# 결과 출력하기

for c in data:

    print(c[1], c[2])


========================== Python ==========================

+ Recent posts