python 通过正则表达式替换1.txt中的部分字符后,写入到sqlite数据库新建表当中
时间: 2023-05-21 10:05:07 浏览: 74
可以使用python自带的re和sqlite3库,先读取1.txt的内容,然后使用正则表达式进行替换,最后插入到sqlite数据库新建的表中。下面是代码示例:
```python
import re
import sqlite3
# 读取1.txt的内容
with open('1.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 使用正则表达式进行替换
new_text = re.sub(r'要替换的正则表达式', '替换后的字符', text)
# 连接到sqlite数据库
conn = sqlite3.connect('database.db')
c = conn.cursor()
# 新建表
c.execute('''CREATE TABLE IF NOT EXISTS my_table
(id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT)''')
# 插入数据
c.execute("INSERT INTO my_table (text) VALUES (?)", (new_text, ))
# 提交事务并关闭连接
conn.commit()
conn.close()
print('操作成功!')
```
需要根据实际情况更改替换的正则表达式和替换后的字符,以及数据库名称和表名。
相关问题
python 通过正则表达式替换1.txt中的部分字符后,写入到新建的sqlite数据库的新建表当中
可以使用以下代码实现:
```python
import re
import sqlite3
# 读取1.txt文件内容
with open('1.txt', 'r') as f:
text = f.read()
# 使用正则表达式替换字符
pattern = re.compile(r'old_text')
new_text = pattern.sub('new_text', text)
# 连接数据库
conn = sqlite3.connect('mydatabase.db')
# 创建新表
conn.execute('''CREATE TABLE mytable (id INT PRIMARY KEY NOT NULL, text VARCHAR(255));''')
# 插入新数据
conn.execute('''INSERT INTO mytable (id, text) VALUES (?, ?);''', (1, new_text))
# 提交更改并关闭连接
conn.commit()
conn.close()
```
其中,将"old_text"替换为"new_text"是利用正则表达式实现的,可以根据实际需要修改。运行代码后,会将替换后的内容插入到新建的名为"mytable"的表中。
。python读取excel数据写入到sqlite数据库中
可以使用Python内置的xlrd库读取Excel文件,使用Python内置的sqlite3库连接SQLite数据库,然后将读取到的Excel数据写入到SQLite数据库中。
以下是一个示例代码:
```python
import xlrd
import sqlite3
# 打开Excel文件
workbook = xlrd.open_workbook('data.xlsx')
sheet = workbook.sheet_by_index(0)
# 连接SQLite数据库
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
# 创建表格
cursor.execute('CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)')
# 写入数据
for i in range(1, sheet.nrows):
id = int(sheet.cell(i, 0).value)
name = sheet.cell(i, 1).value
age = int(sheet.cell(i, 2).value)
cursor.execute('INSERT INTO data (id, name, age) VALUES (?, ?, ?)', (id, name, age))
# 提交更改并关闭连接
conn.commit()
conn.close()
```
其中,`data.xlsx`是Excel文件名,`example.db`是SQLite数据库文件名,`data`是数据表名,数据表包含`id`、`name`和`age`三个字段。代码中通过循环读取Excel文件中的每一行数据,然后将数据插入到SQLite数据库中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)