Project

General

Profile

« Previous | Next » 

Revision 5591

sql_io.py: import_csv(): Take a reader and header rather than a stream to allow callers to pass in a wrapped CSV reader for filtering, etc.

View differences:

lib/sql_io.py
256 256
    try: db.db.cursor().copy_expert(copy_from, stream)
257 257
    except Exception, e: sql.parse_exception(db, e, recover=True)
258 258

  
259
def import_csv(db, table, stream):
259
def import_csv(db, table, reader, header):
260 260
    def log(msg, level=1): db.log_debug(msg, level)
261 261
    
262 262
    # Get format info
263
    reader, header = csvs.reader_and_header(stream)
264 263
    col_names = map(strings.to_unicode, header)
265 264
    for i, col in enumerate(col_names): # replace empty column names
266 265
        if col == '': col_names[i] = 'column_'+str(i)
bin/csv2db
9 9

  
10 10
sys.path.append(os.path.dirname(__file__)+"/../lib")
11 11

  
12
import csvs
12 13
import exc
13 14
import opts
14 15
import sql
......
52 53
        in_ = proc.stdout
53 54
        
54 55
        # Import data
55
        try: sql_io.import_csv(db, table, in_)
56
        try: sql_io.import_csv(db, table, *csvs.reader_and_header(in_))
56 57
        finally:
57 58
            in_.close() # also closes proc.stdout
58 59
            proc.wait()

Also available in: Unified diff