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
Overview of Alibaba Cloud Security Group Sharing ...
Preface: Timestamp fields are often used in MySQL...
This article shares the specific code of Vue to r...
Table of contents Preface vue-cli 2.0 version vue...
This article example shares the specific code of ...
background The popularity of Docker is closely re...
Preface If the query information comes from multi...
1. CPU utilization sar -p (view all day) sar -u 1...
I haven't used mysql on my computer for a lon...
Table of contents APIs used Simple Example person...
There are two types of html tags, inline elements...
What is the purpose of this sentence? Copy code Th...
1. Add fields: alter table table name ADD field n...
There are two common ways to download files in da...
1. Use the shortcut Ctrl + Shift + P to call out ...