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

Error in accessing file uploaded file in google colab

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

DatabaseError: database disk image is malformed

我在googlecolab中上传文件如下

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

更改这些行

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')

文件已保存,无需重新写入