当前位置: 首页>>代码示例>>Python>>正文


Python Common.readJSON方法代码示例

本文整理汇总了Python中common.Common.readJSON方法的典型用法代码示例。如果您正苦于以下问题:Python Common.readJSON方法的具体用法?Python Common.readJSON怎么用?Python Common.readJSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在common.Common的用法示例。


在下文中一共展示了Common.readJSON方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from common import Common [as 别名]
# 或者: from common.Common import readJSON [as 别名]
class Insert_db:

    def __init__(self):
        self.com = Common()

    def loadData(self):
        '''
        #usage: Load data form /home/boyang/Documents/furnitures
        #arg: None
        #return: Data stored at path above in json structure
        '''
        src = '/home/boyang/Documents/furnitures';
        datas = []
        furniture_list = self.com.GetFileList(src)
        for furniture in furniture_list:
            data = self.com.readJSON(furniture)
            datas.append(data)
        return datas

    def insertData(self, datas):
        '''
        #usage: Insert data into data base
        #arg: Data you want to insert
        #rerutn: None
        '''
        db = pymysql.connect(host = '127.0.0.1',
        					 user = 'root',
        					 password = 'Devil123',
        					 db = 'FurnitoData',
        					 charset = 'utf8',
        					 cursorclass = pymysql.cursors.DictCursor)
        cursor = db.cursor()
        for data in datas:
            comments = data['reviews']
            
            for comment in comments:
            	sql = 'INSERT INTO comments (furniture_name, comment, vote_up, vote_down) VALUES (%s, %s, %s, %s)'
            	print sql
                cursor.execute(sql, (data['name'].encode('utf-8'),comment.encode('utf-8'),'0','0'))
                db.commit()
        db.close()
开发者ID:Informationretrieval2016,项目名称:Furnito_web,代码行数:43,代码来源:insert_db.py

示例2: __init__

# 需要导入模块: from common import Common [as 别名]
# 或者: from common.Common import readJSON [as 别名]
class Insert_db:

    def __init__(self):
        self.com = Common()
        self.sentiment_doc = config.sentiment_path

    def loadData(self):
        '''
        #usage: Load data form /home/boyang/Documents/furnitures
        #arg: None
        #return: Data stored at path above in json structure
        '''
        src = '/home/boyang/Documents/json'
        datas = []
        furniture_list = self.com.GetFileList(src)
        for furniture in furniture_list:
            data = self.com.readJSON(furniture)
            datas.append(data)
        return datas

    def insertData(self, datas):
        '''
        #usage: Insert data into data base
        #arg: Data you want to insert
        #rerutn: None
        '''
        db = pymysql.connect(host = '127.0.0.1',
                             user = 'root',
                             password = 'Devil123',
                             db = 'FurnitoData',
                             charset = 'utf8',
                             cursorclass = pymysql.cursors.DictCursor)
        cursor = db.cursor()
        for data in datas:
            comments = data['reviews']
            
            for comment in comments:
                sql = 'INSERT INTO comments (furniture_name, comment, vote_up, vote_down) VALUES (%s, %s, %s, %s)'
                cursor.execute(sql, (data['name'].encode('utf-8'),comment.encode('utf-8'),'0','0'))
                db.commit()
        db.close()

    def csvtoDatebase(self):
        db = pymysql.connect(host = '127.0.0.1',
                             user = 'root',
                             password = 'Devil123',
                             db = 'FurnitoData',
                             charset = 'utf8',
                             cursorclass = pymysql.cursors.DictCursor)
        cursor = db.cursor()
        csv_url = '/home/boyang/Documents/data.csv'
        df = pd.read_csv(csv_url, encoding = "utf-8")
        for index, row in df.iterrows():
            gender = row[1]
            age = row[2]
            favoritestyle = row[3]
            for i in range(4,len(row)-3):
                if row.index[i].encode('utf-8').startswith('Please choose and take a good look at the'):
                    furniture_one = row[i]
                else:
                    if not str(row[i]) == 'nan':
                        furniture_two = row[i]
                        sql = 'INSERT INTO clicks (furniture_one, furniture_two, age, favoritestyle, gender) VALUES (%s, %s, %s, %s, %s)'
                        print sql
                        cursor.execute(sql,(furniture_one.encode('utf-8'), furniture_two.encode('utf-8'), age.encode('utf-8'), favoritestyle.encode('utf-8'), gender.encode('utf-8')))
                        db.commit()
                    else:
                        pass
        db.close()
开发者ID:Informationretrieval2016,项目名称:furnito_webapp,代码行数:71,代码来源:insert_db.py


注:本文中的common.Common.readJSON方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。