LoginSignup
0
0

More than 1 year has passed since last update.

Mac, Python3 環境で csv の内容を Python の dict リテラルで出力するワンライナー

Last updated at Posted at 2022-08-16
python3 -c "csv_file_path = '<path_to_csv>.csv'; import csv; print([row_dict for row_dict in csv.DictReader(open(csv_file_path))]);" | pbcopy

ex.

csv を作成

echo "#,Name,Type 1,Type 2,Total,HP,Attack,Defense,Sp. Atk,Sp. Def,Speed,Generation,Legendary\n1,Bulbasaur,Grass,Poison,318,45,49,49,65,65,45,1,False\n2,Ivysaur,Grass,Poison,405,60,62,63,80,80,60,1,False\n3,Venusaur,Grass,Poison,525,80,82,83,100,100,80,1,False" >> pokemon.csv

csv to Python dict

$ python3 -c "csv_file_path = 'pokemon.csv'; import csv; print([row_dict for row_dict in csv.DictReader(open(csv_file_path))]);" | pbcopy

結果

[{'#': '1', 'Name': 'Bulbasaur', 'Type 1': 'Grass', 'Type 2': 'Poison', 'Total': '318', 'HP': '45', 'Attack': '49', 'Defense': '49', 'Sp. Atk': '65', 'Sp. Def': '65', 'Speed': '45', 'Generation': '1', 'Legendary': 'False'}, {'#': '2', 'Name': 'Ivysaur', 'Type 1': 'Grass', 'Type 2': 'Poison', 'Total': '405', 'HP': '60', 'Attack': '62', 'Defense': '63', 'Sp. Atk': '80', 'Sp. Def': '80', 'Speed': '60', 'Generation': '1', 'Legendary': 'False'}, {'#': '3', 'Name': 'Venusaur', 'Type 1': 'Grass', 'Type 2': 'Poison', 'Total': '525', 'HP': '80', 'Attack': '82', 'Defense': '83', 'Sp. Atk': '100', 'Sp. Def': '100', 'Speed': '80', 'Generation': '1', 'Legendary': 'False'}]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0