pandas读Excel写库总结

import pandas
import pymongo

# pandas读Excel,读出的数据类型是
data = pandas.read_excel('信立坦病例分配.xlsx', sheet_name=0)
# data_frame = data.head()
# print(data_frame)  这里想打印出读到的数据,结果只打印了5行,查了下head()这个方法,详见下文
all=[]
#pandas.DataFrame.itertuples() 将DataFrame迭代为元祖
for row in data.itertuples():
    each ={}
    
    each['点评医生']=getattr(row, '点评医生')
    each['点评医生ID'] = str(getattr(row, '点评医生ID'))
    each['被点评医生ID'] = str(getattr(row, '被点评医生ID'))
    each['content_uid'] = getattr(row, 'content_uid')
    all.append(each)

print('待分配病例个数:',len(all),'待分配病例的数据是:',all)

# 连接Mongo数据库
host = '略'
username = '略'
password = '略'
port = '27017'
db = '略'
mongo_url = 'mongodb://{0}:{1}@{2}:{3}/?authSource={4}&authMechanism=SCRAM-SHA-1'.format(username, password, host, port, db)
client = pymongo.MongoClient(mongo_url)

# 找到对应的表
collects = client["platform"]['xinlitan_collects']
found_list=[]
# 写入数据库
for e in all:
    print('将医生%s的病例%s分配给%s'%(e['被点评医生ID'],e['content_uid'],e['点评医生ID']))
    collects.update({'content_uid': e['content_uid'], 'doctor_id': e['被点评医生ID']},{'$set': {'expert_id':e['点评医生ID'] }})
# 检查是否绑定成功
#     found_list.append(collects.find({'content_uid': e['content_uid'], 'doctor_id': e['被点评医生ID']}))
    for ea in collects.find({'content_uid': e['content_uid'], 'doctor_id': e['被点评医生ID']}):
        print('医生%s的病例%s分配给了%s'%(e['被点评医生ID'],e['content_uid'],ea['expert_id']))
        if ea['expert_id']==e['点评医生ID']:
            print('绑定成功')
        else:
            print('医生%s的病例%s绑定失败'%(e['被点评医生ID'],e['content_uid']))

head()的具体实现:

def head(self, n=5):
        '''
        Return the first `n` rows.
        This function returns the first `n` rows for the object based
        on position. It is useful for quickly testing if your object
        has the right type of data in it.
        Parameters
        ----------
        n : int, default 5
            Number of rows to select.
        Returns
        -------
        obj_head : same type as caller
            The first `n` rows of the caller object.
        '''
        return self.iloc[:n]

head() 不传参数时,默认n是5,返回前五行
如果指定返回多少行,比如10行,传具体行数 head(10)

你可能感兴趣的:(pandas读Excel写库总结)