在google colab中访问文件上传文件时出错

时间:2018-05-16 06:27:20

标签: python-3.x jupyter-notebook google-colaboratory google-notebook

我在google colab上传 final_half.sqlite 文件。在阅读文件时,它给出了如下错误。有谁能告诉我如何解决这个问题?

DatabaseError: database disk image is malformed

我在google colab中上传了该文件,如下所示

import pandas as pd
import sqlite3
from google.colab import files
uploaded = files.upload()

我检查了上传文件的状态

for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))

用户上传的文件" final_half.sqlite"长度为7208960字节

for name, data in uploaded.items():
  with open('final_half.sqlite', 'wb') as f:
    f.write(data)
    print ('saved file', name)
    con = sqlite3.connect(name)
    print(con)
    sorted_data = pd.read_sql_query("""SELECT * FROM Reviews_half""", con)

错误:

DatabaseError                             Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pandas/io/sql.py in execute(self, *args, **kwargs)
   1408             else:
-> 1409                 cur.execute(*args)
   1410             return cur

DatabaseError: database disk image is malformed

1 个答案:

答案 0 :(得分:0)

更改这些行

for name, data in uploaded.items():
  with open('final_half.sqlite', 'wb') as f:
    f.write(data)
    print ('saved file', name)
    con = sqlite3.connect(name)

只是

con = sqlite3.connect('final_half.sqlite')

该文件已保存,您无需再次编写。