0

a datebase contained 80 tables ,every table is a 18000x4 matrix ,how can I translate these to a 80x18000x4 numpy array?

the db : https://drive.google.com/open?id=0B3bNSNFik5wGdm1DYnJwNHBJMVU

I wrote a function. Do you have any better idea?

import sqlite3 as sql
import os
def db_to_array(db,r,c):
  db = sql.connect(db)
  cursor = db.cursor()
  cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
  tables = cursor.fetchall()
  wd=os.getcwd()
  if not os.path .exists(wd +'/temp/'):
    os.makedirs(wd +'/temp/')
  for table_name in tables:
    table_name = table_name[0]
    table = pd.read_sql_query("SELECT * from %s" % table_name, db)
    table.to_csv(wd +'/temp/'+ table_name + '.csv', index_label='index')
  ss=os.listdir(wd +'/temp/')
  print(len(ss))
  dd=np.empty([len(ss),r,c])
  for i in range(len(ss)):
    ddd=np.array(pd.read_csv(wd +'/temp/'+ss[i]))
    print(i)
    print(ddd.shape)
    dd[i,:,:]=ddd[:,0:r]
  return dd
3
  • By table do you mean 4 fields and 1000 records? Show a sample table definition, and how you would select some records. For example, stackoverflow.com/questions/42914215/… Commented Apr 24, 2017 at 3:35
  • what have you done so far? Commented Apr 26, 2017 at 2:26
  • I wrote a function. Do you have any better idea? Commented Apr 26, 2017 at 7:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.