插入带逗号的整数数据

2024-05-13 23:33:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图插入一个大的字典条目集(430个条目),其中一个键值对有一个与之相关联的薪水,形式是$xx,xxx,xxx。我该如何解决这个问题,因为现在它将接受一个值并将其拆分为3列。你知道吗

编辑:

def process_item(self, item, spider):
    if isinstance(item, TeamStats):
        for key, value in item.iteritems():
            if key == "division":
                print(item.get('division', ""))
                self.cur.execute("INSERT INTO Divs( division ) VALUES(?)", (item.get('division', ""),))
                self.con.commit() 

    if isinstance(item, Player):
        self.cur.execute("INSERT INTO Players(\
            player_name, \
            salary, \
            weight, \
            age, \
            height, \
            position, \
            college \
            ) \
            VALUES( ?, ?, ?, ?, ?, ?, ?)", \
            ( \
            item.get('name', ''),
            item.get('salary', 0),
            item.get('weight', 0),
            item.get('age', 0),
            item.get('height', ''),
            item.get('position', ''),
            item.get('college', '')
            ))

        self.con.commit() 
    return item

我要插入管道的对象如下所示:

60万美元 2015-01-08 14:17:59-0500[nbaStats]调试:从<;200http://espn.go.com/nba/team/roster//name/mil/milwaukee-bucks>; {age':u'21', “大学”:u'LSU, 'height':u'6-9', 'name':u“强尼·奥布莱恩特三世”, '位置':u'PF', “工资”:u“$600000”, 'weight':u'265'} $5,200,000 2015-01-08 14:17:59-0500[nbaStats]调试:从<;200http://espn.go.com/nba/team/roster//name/mil/milwaukee-bucks>; {age':u'30', “学院”:u“\xa0”, 'height':u'6-11', 'name':u'Zaza Pachulia', '位置':u'C', “薪水”:单位:5200000美元, 'weight':u'270'}


Tags: keynameselfagegetif条目item
1条回答
网友
1楼 · 发布于 2024-05-13 23:33:09

您可以修改SQL查询,在插入逗号和美元符号时将它们从工资中删除。你可以通过改变

VALUES( ?, ?, ?, ?, ?, ?, ?)", \

VALUES( ?,    REPLACE(REPLACE(?,',',''),'$',''),   ?, ?, ?, ?, ?)", \

这两个嵌套的REPLACE操作删除了,$字符。你知道吗

或者您可以更改python代码来删除额外的字符:

        item.get('name', ''),
        re.sub(r'[,$]', "", item.get('salary', 0)),
        item.get('weight', 0),

无论哪种方式,清理带注释的数据都是简单的字符串处理。这两种方法都从数字字符串中删除所有的美元符号和逗号。你知道吗

相关问题 更多 >