How to import txt into mysql in Linux

How to import txt into mysql in Linux

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: Custom class for connecting to mysql database
  • importtxt.py file: read TXT files and insert them
  • dict.txt file: TXT file to be operated

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:
  • Python import txt data to mysql method
  • How to import sql files in linux (using command line to transfer mysql database)
  • How to implement import and export mysql database commands under linux
  • Import and export of mysql database in linux system
  • Several ways to easily import and export MySQL databases on Linux VPS/servers

<<:  In-depth understanding of MySQL self-connection and join association

>>:  Complete steps to use element in vue3.0

Recommend

Simple example of adding and removing HTML nodes

<br />Simple example of adding and removing ...

Summary of common Nginx techniques and examples

1. Priority of multiple servers For example, if e...

A brief discussion on Linux virtual memory

Table of contents origin Virtual Memory Paging an...

Two ways to introduce svg icons in Vue

How to introduce svg icons in Vue Method 1 of int...

HTML page jump and parameter transfer issues

HTML page jump: window.open(url, "", &q...

Are you still Select *?

There are many reasons why an application is as s...

Steps to install superset under win10 system

Superset is a lightweight self-service BI framewo...

What to do if the auto-increment primary key in MySQL is used up

In the interview, you should have experienced the...

Summary of Linux file directory management commands

touch Command It has two functions: one is to upd...

mysql trigger creation and usage examples

Table of contents What is a trigger Create a trig...

Summary of related functions for Mysql query JSON results

The JSON format field is a new attribute added in...

Detailed explanation of the installation steps of the MySQL decompressed version

1. Go to the official website: D:\mysql-5.7.21-wi...