0

私は、Python の TeamTreehouse.com 技術学位のセクション 4 のプロジェクトを行っています。私は 2 日以内にセクション 4 を完了し、すべてのクイズに合格し、すべてのコード チャレンジを行いましたが、CSV ファイルから情報を読み取った後、peewee とorderedDict を使用して DB ファイルにデータを書き込む方法をまだ見つけることができません。私は、orderedDict のドキュメントと peewee を読みましたが、OrderedDict を DB に書き込む方法が見つかりません。

ドキュメントを検索し、teamtreehouse.com のトレーニング ビデオを再訪しようとしましたが、このような例はまったく見つかりません。

from collections import OrderedDict
import datetime
import sys
import csv

from peewee import *

db = SqliteDatabase('inventory.db')


class Product(Model):
    content = TextField()
    id = PrimaryKeyField()
    product_name = TextField(unique=True)
    product_price = TextField()
    product_quantity = TextField()
    date_updated = DateTimeField(datetime.datetime.now)

    class Meta:
        database = db

def migrate_data():
    with open('inventory.csv') as csvfile:
        reader = csv.reader(csvfile, delimiter=",")
        keys = next(reader)
        #ordered = ([OrderedDict(zip(keys,row)) for row in reader ])
        print('\n\n')
        print([OrderedDict(zip(keys,row)) for row in reader ])
        print('\n\n')



def initialize():
    """ Initialize an Sqlite database called inventory.db."""
    db.connect()
    db.create_tables([Product], safe=True)


def create_model():
    """ Create a model called Product that the Peewee ORM will use to 
build the database. 
The Product model should have five attributes: product_id, product_name, 
product_quantity, 
product_price. Use PeeWee's built in primary_key functionality for the 
product_id field,
 so that each product will have an automatically generated unique 
identifier."""
    productname = ('product_name')

この質問の予想される結果は、CSV ファイルを読み取って構築された OrderedDict を peewee を使用して sqlite ファイルに書き込むことです。私のgithub(Yeransian)から「Treehouse-Project4」の下にCSVファイルと完全なpythonコードをダウンロードできます

このスランプを乗り越えるための助けは素晴らしいでしょう!学習に戻るのが早ければ早いほどよい。:)

4

1 に答える 1