コード例 #1
0
ファイル: csv-5.py プロジェクト: kashewnuts/python-school-ja
def csv2tsv(fname):
    """Convert a CSV format file to TSV format string.
    """
    with open(fname) as reader:
        stream = UnicodeReader(reader)
        # Read a header line.
        fields = stream.next()
        for row in stream:
            record = dict(zip(fields, row))
            print record['rank'] + '\t' + \
                  record['team'] + '\t' + \
                  record['point'] + '\t' + \
                  record['match'] + '\t' + \
                  record['goaldiff']
コード例 #2
0
ファイル: csv-6.py プロジェクト: kashewnuts/python-school-ja
def csv2sqlite(fname, output):
    """Import a CSV format file to SQLite database.
    """
    dsl = 'sqlite:///' + output
    engine = create_engine(dsl, echo=True)
    Base.metadata.create_all(engine)
    Session.configure(bind=engine)
    session = Session()
    with open(fname) as reader:
        stream = UnicodeReader(reader)
        # Read a header line.
        fields = stream.next()
        for row in stream:
            record = dict(zip(fields, row))
            r = LeagueStats(team=record['team'],
                    rank=int(record['rank']),
                    point=int(record['point']),
                    match=int(record['match']),
                    goaldiff=int(record['goaldiff']))
            session.add(r)
    session.commit()