ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Template, Csv
    python/기본 2022. 5. 1. 21:19
    # template
    import string
    
    s = """\
    Hi $name.
    
    $contents
    
    Have a good day
    """
    
    t = string.Template(s)
    c = t.substitute(name='Mike', contents='How are you?')
    print(c)
    
    ####################################################################
    # csv
    import csv
    
    # windows 의 경우 \r\n 때문에 newline 을 '' 로 설정
    with open('test.csv', 'w', newline='') as csv_file:
        fieldnames = ['Name', 'Count']
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerow({'Name': 'A', 'Count': 1})
        writer.writerow({'Name': 'B', 'Count': 2})
    
    with open('test.csv', 'r') as csv_file:
        reader = csv.DictReader(csv_file)
        for row in reader:
            print(row['Name'], row['Count'])

    'python > 기본' 카테고리의 다른 글

    tar / zip  (0) 2022.05.02
    os, pathlib, glob, shutil  (0) 2022.05.01
    File  (0) 2022.05.01
    Class  (0) 2022.05.01
    __name__ 과 __main__  (0) 2022.05.01

    댓글

Designed by Tistory.