0

我目前正在努力解决这个错误试图告诉我的方向。我不知道它是指我输入的一些数据,还是指我创建的数据字典。我什至很难提出一个让我感到困惑的问题。

Traceback (most recent call last):
  File "push data.py", line 78, in <module>
   uploadFile(filename, uri, dbname)
  File "push data.py", line 69, in uploadFile
   docs = upload(db,docs)
  File "push data.py", line 27, in upload
   db.bulk_save(docs)
  File "C:\Python27\lib\site-packages\couchdbkit\client.py", line 564, in save_docs
payload=payload, **params).json_body
  File "C:\Python27\lib\site-packages\restkit\resource.py", line 144, in post
headers=headers, params_dict=params_dict, **params)
  File "C:\Python27\lib\site-packages\couchdbkit\resource.py", line 105, in request
payload = json.dumps(payload).encode('utf-8')
  File "C:\Python27\lib\json\__init__.py", line 244, in dumps
return _default_encoder.encode(obj)
  File "C:\Python27\lib\json\encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
  File "C:\Python27\lib\json\encoder.py", line 270, in iterencode
return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x92 in position 3: invalid start byte

编辑

这是我的代码,我实际上是从这个问题的第一个答案中偷来的 ->如何将 CSV/TSV 数据导入 Couch DB?. 我的困难是试图找到文档以确保我没有做错任何事情。如果有人知道我在哪里可以找到 couchdbkit 文档,那就太好了!他们的官方网站已关闭。

#!/usr/bin/env python

from couchdbkit import Server, Database
from couchdbkit.loaders import FileSystemDocsLoader
from csv import DictReader
import sys, subprocess, math, os



def parseDoc(doc):
    for k,v in doc.items():
        if (isinstance(v,str)):
            print k, v, v.isdigit()
            # #see if this string is really an int or a float
            if v.isdigit()==True: #int
                doc[k] = int(v)
            else: #try a float
                try:
                    if math.isnan(float(v))==False:
                        doc[k] = float(v) 
                except:
                    pass            
    return doc


def upload(db, docs):
    db.bulk_save(docs)
    del docs
    return list()


def uploadFile(fname, uri, dbname):


  print 'Upload contents of %s to %s/%s' % (fname, uri, dbname)

  # #connect to the db
  theServer = Server(uri)
  db = theServer.get_or_create_db(dbname)

  #loop on file for upload
  reader = DictReader(open(fname, 'rU'), dialect = 'excel')  #see the python csv module 
         #for other options, such as using the tab delimeter. The first line in your csv 
         #file should contain all of the "key" and all subsequent lines hold the values 
         #for those keys.

  #used for bulk uploading
  docs = list()
  checkpoint = 100

  for doc in reader:
    newdoc = parseDoc(doc) #this just converts strings that are really numbers into ints and floats

    #Here I check to see if the doc is already on the database. If it is, then I assign
    #the _rev key so that it updates the doc on the db.
    print('\n')
    print(newdoc)
    print('\n')

    if db.doc_exist(newdoc.get('_id')):
      newdoc['_rev'] = db.get_rev(newdoc.get('_id'))

    docs.append(newdoc)

    if len(docs)%checkpoint==0:
      docs = upload(db,docs)

  #don't forget the last batch        
  docs = upload(db,docs)



if __name__=='__main__':
  filename = "Kardiology Data.csv"
  uri = "http://localhost:5984"
  dbname = "kardiology"

  uploadFile(filename, uri, dbname)
4

1 回答 1

0

有问题:

reader = DictReader(open(fname, 'rU'), dialect = 'excel')

尝试添加 UTF-8 编码:

reader = DictReader(open(fname, 'rU'),encoding='utf-8', dialect = 'excel')
于 2018-05-03T09:49:12.007 回答