python - Pulling the Next Value Under the Same Column Header -


i using python's csv module read ".csv" files , parse them out mysql insert statements. in order maintain syntax statements need determine type of values listed under each column header. however, have run problem of rows start null value.

how can use csv module return next value under same column until value returned not null? not have accomplished csv module; open solutions. after looking through documentation not sure csv module capable of doing need. thinking along these lines:

if rowvalue == '':     rowvalue = nextrowvalue(row) 

obviously next() method returns next value in csv "list" rather returning next value under same column want, , nextrowvalue() object not exist. demonstrating idea.

edit: add context, here example of doing , problems running into.

if table follows:

id date time  voltage current watts 0  7/2  11:15         0       0 0  7/2  11:15         0       0 0  7/2  11:15  380    1       380 

and here slimmed down version of code using read table, column headers , determine type of values first row. put them separate lists , use deque add them insert statements in separate function. not of code featured , might have left crucial parts out, here example:

import csv, os collections import deque  def findtype(rowvalue):     if rowvalue == '':         rowvalue =      if '.' in rowvalue:         try:             rowvalue = type(float(rowvalue))         except valueerror:             pass     else:         try:             rowvalue = type(int(rowvalue))         except:             rowvalue = type(str(rowvalue))     return rowvalue  def createtable():     inputpath = 'c:/users/user/desktop/test_input/'     outputpath = 'c:/users/user/desktop/test_output/'     file in os.listdir(inputpath):         if file.endswith('.csv'):             open(inputpath + file) infile:                 open(outputpath + file[:-4] + '.sql', 'w') outfile:                     csvfile = csv.reader(infile)                     columnheader = next(csvfile)                     firstrow = next(csvfile)                     clist = deque(columnheader)                     rlist = deque(firstrow)                     hlist = []                     value in firstrow:                         valuetype = findtype(firstrow)                         if valuetype == str:                             try:                                 val = '`' + clist.popleft() + 'varchar(255)'                                 hlist.append(val)                             except indexerror:                                 pass                         etc. 

and forth rest of value types returned findtype function. problem when adding values rlist using deque skips on null values number of items in list column headers 6, example, , number of items in list rows 5 not line up.

a drawn out solution scan each row null values until 1 found using this:

for value in firstrow:      if value == '':          firstrow = next(csvfile) 

and continuing loop until row found no null values. seems drawn out solution slow down program, hence why looking different solution.

rather pull next value column title suggests, found easier skip rows contained null values. there 2 different ways this:

use loop scan each row , see if contains null value, , jump next row until 1 found contains no null values. example:

temprow = next(csvfile) value in temprow:     if value == '':        temprow = next(csvfile)     else:        row = temprow 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -