- Goal
PythonでCSVファイルを読み込んで、List型と辞書型、集合型に格納する - How
CSVモジュールのreaderメソッドを使う
マスターファイルを作る。 テキストファイル menu.csv
UDON001,500UDON0012,700SUSHI0123,1200TONKATSU001,2000RAMEN10,1000This 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# -*- coding: utf-8 -*- import csv #list作成する場合 with open('menu.csv', 'r') as f: csvdata = csv.reader(f) data = [ x for x in csvdata ] #集合にセットする場合 with open('menu.csv', 'r') as f: csvdata = csv.reader(f) dataset = set(dict([ x for x in csvdata ])) #listを辞書型に変更 datadic = dict(data) #checking print('List') print(data) print(len(data)) print('Dic') print(datadic) print(len(datadic)) print('Set') print(dataset) print(len(dataset)) - Result
(pytest2.7.13)~/pytest$ python CSVtoListDic.pyList[['UDON001', '500'], ['UDON0012', '700'], ['SUSHI0123', '1200'], ['TONKATSU001', '2000'], ['RAMEN10', '1000']]5Dic{'TONKATSU001': '2000', 'UDON001': '500', 'RAMEN10': '1000', 'UDON0012': '700', 'SUSHI0123': '1200'}5Setset(['TONKATSU001', 'UDON001', 'SUSHI0123', 'UDON0012', 'RAMEN10'])
5
Oracle Application Express Notes | Apps development Notes | Google Cloud Platform | Python | apps test | Cool Beans | English | Books
2017/10/28
CSVファイルを読み込んでlist型と辞書型に格納 - Python
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿