爱吃菜包因为好吃 2022-11-30 23:03 采纳率: 50%
浏览 32

python中怎么将数据存入sqlite数据库

如以下代码,怎么将输出的列表数据存入sqlite数据库中


import re
# 打开文件
with open(file=r"C:\Users\666666\Desktop\python课设\html50000-57010\data\50017.html", mode="r", encoding="utf-8") as d:
    data = d.read()
# 获取文件中的有用数据
result = re.findall(r'("hometeam_name":"\w+"),"awayteam_id":\w+,("awayteam_name":"\w+"),"matchtime":\d+,'
                   r'("homescore":\d+,)("awayscore":\d+),"',data)
print(result)

输出数据:
[('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"亚达纳邦U19"', '"homescore":0,', '"awayscore":4'), ('"hometeam_name":"米瓦迪U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":4,', '"awayscore":0'), ('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"茨瓦卡平联U19"', '"homescore":0,', '"awayscore":2'), ('"hometeam_name":"马圭U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":1,', '"awayscore":2'), ('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"实皆联U19"', '"homescore":0,', '"awayscore":1'), ('"hometeam_name":"雅玛FCU19"', '"awayteam_name":"治尼兰德U19"', '"homescore":1,', '"awayscore":5'), ('"hometeam_name":"南缅甸U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":6,', '"awayscore":0'), ('"hometeam_name":"伊洛瓦底U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":5,', '"awayscore":0'), ('"hometeam_name":"拉科尼联U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":3,', '"awayscore":1'), ('"hometeam_name":"拉科尼联U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":2,', '"awayscore":0'), ('"hometeam_name":"伊洛瓦底U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":4,', '"awayscore":0'), ('"hometeam_name":"马圭U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":4,', '"awayscore":1'), ('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"南缅甸U19"', '"homescore":0,', '"awayscore":3'), ('"hometeam_name":"汉达瓦底联U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":2,', '"awayscore":2'), ('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"仰光联U19"', '"homescore":1,', '"awayscore":2'), ('"hometeam_name":"实皆联U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":2,', '"awayscore":1'), ('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"拉科尼联U19"', '"homescore":2,', '"awayscore":1'), ('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"亚达纳邦U19"', '"homescore":0,', '"awayscore":4')]

  • 写回答

1条回答 默认 最新

  • chuifengde 2023-02-16 14:11
    关注
    import sqlite3
    
    path = r'C:\Users\Administrator\Desktop\pro.db'
    
    data = [('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"亚达纳邦U19"', '"homescore":0,', '"awayscore":4'),
            ('"hometeam_name":"米瓦迪U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":4,', '"awayscore":0'),
            ('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"茨瓦卡平联U19"', '"homescore":0,', '"awayscore":2'),
            ('"hometeam_name":"马圭U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":1,', '"awayscore":2'),
            ('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"实皆联U19"', '"homescore":0,', '"awayscore":1'),
            ('"hometeam_name":"雅玛FCU19"', '"awayteam_name":"治尼兰德U19"', '"homescore":1,', '"awayscore":5'),
            ('"hometeam_name":"南缅甸U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":6,', '"awayscore":0'),
            ('"hometeam_name":"伊洛瓦底U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":5,', '"awayscore":0'),
            ('"hometeam_name":"拉科尼联U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":3,', '"awayscore":1'),
            ('"hometeam_name":"拉科尼联U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":2,', '"awayscore":0'),
            ('"hometeam_name":"伊洛瓦底U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":4,', '"awayscore":0'),
            ('"hometeam_name":"马圭U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":4,', '"awayscore":1'),
            ('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"南缅甸U19"', '"homescore":0,', '"awayscore":3'),
            ('"hometeam_name":"汉达瓦底联U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":2,', '"awayscore":2'),
            ('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"仰光联U19"', '"homescore":1,', '"awayscore":2'),
            ('"hometeam_name":"实皆联U19"', '"awayteam_name":"治尼兰德U19"', '"homescore":2,', '"awayscore":1'),
            ('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"拉科尼联U19"', '"homescore":2,', '"awayscore":1'),
            ('"hometeam_name":"治尼兰德U19"', '"awayteam_name":"亚达纳邦U19"', '"homescore":0,', '"awayscore":4')]
    
    data = [tuple(eval('{' + ','.join(map(lambda x: x.replace(',', ''), i)) + '}').values()) for i in data]
    
    c = sqlite3.connect(path)
    c.execute("CREATE TABLE IF NOT EXISTS info(hometeam_name NVARCHAR(20) ,awayteam_name NVARCHAR(30),homescore INT(5),awayscore INT(5))")
    c.commit()
    c.executemany("INSERT INTO info(hometeam_name,awayteam_name,homescore,awayscore) VALUES(?,?,?,?)",data)
    c.commit()
    
    

    img

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 11月30日

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色