Preface When I was writing a small project yesterday, I encountered a requirement: import the data of a txt document into a mysql database. At first, I wanted to use Mysql Workbench to import the TXT file directly, but in the end I found that it did not support TXT import. As a result, I converted the TXT to Excel, and when I imported it on Linux, I found various garbled code problems. Holding the principle that there is nothing a programmer cannot do, I wrote a Python code to directly operate the file for import. The result is a file with more than 10,000 records, and the import time is about two minutes. Here is the specific code:
mysqlpython.py file from pymysql import * class Mysqlpython: def __init__(self,database,host="localhost", user="root",password="123456", charset="utf8",port=3306): self.database = database self.host = host self.user = user self.password = password self.charset = charset self.port = port # Create data connection and cursor object def open(self): self.db = connect(host=self.host, user=self.user, password=self.password, port=self.port, database=self.database, charset = self.charset) self.cur = self.db.cursor() # Close the cursor object and database connection object def close(self): self.cur.close() self.db.close() # Execute sql command def zhixing(self,sql,L=[]): self.open() self.cur.execute(sql,L) self.db.commit() self.close() # Query function def all(self,sql,L=[]): self.open() self.cur.execute(sql,L) result = self.cur.fetchall() return result if __name__ == "__main__": sqlh = Mysqlpython("dictionary") sel = "select * from user" r = sqlh.all(sel) print(r) importtxt.py file import re import sys from mysqlpython import Mysqlpython sqlh = Mysqlpython("dictionary") def insert(data): arr = data.split() name = arr[0] description = " ".join(arr[1:]) ins = "insert into words(name,description) values(%s,%s)" sqlh.zhixing(ins,[name,description]) def get_addr(): f = open('./dict.txt') lines = f.readlines() for line in lines: insert(line) f.close() return '' if __name__ == '__main__': print(get_addr()) dict.py file (I copied several files) an indef art one abacus n.frame with beads that slide along parallel rods, used for teaching numbers to children, and (in some countries) for counting abandon v. go away from (a person or thing or place) not intending to return; forsake; desert abandonment n. abandoning abase v. ~ oneself/sb lower oneself/sb in dignity; degrade oneself/sb ; abash to destroy the self-possession or self-confidence of:disconcert abashed adj. ~ embarrassed; ashamed abate v. make or become less abattoir n. = slaughterhouse (slaughter) Just modify the regular expression to match different delimiters. All the codes are pasted here. You can just copy and modify the database configuration and run it. Summarize: The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM. You may also be interested in:
|
<<: In-depth understanding of MySQL self-connection and join association
>>: Complete steps to use element in vue3.0
Table of contents Preface MySQL master-slave repl...
The layout problem of irregular picture walls enc...
This article records the detailed process of down...
I believe that everyone needs to copy and paste d...
Table of contents Overview 1. Menu and routing pr...
This time I will talk about the skills of develop...
Table of contents Changes in the life cycle react...
This article uses the deep learning framework ker...
Table of contents 1. Basic event handling 2. Send...
Table of contents 1. prototype (explicit prototyp...
In the previous article, we explained how nginx r...
1. Installation 1. Download MySQL Download addres...
1. Overview The so-called life cycle function is ...
Preface Before talking about covering index, we m...
html <!DOCTYPE html> <html lang="en...