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

Detailed explanation of MySQL semi-synchronization

Table of contents Preface MySQL master-slave repl...

Analysis and application of irregular picture waterfall flow principle

The layout problem of irregular picture walls enc...

Detailed explanation of Linux redirection usage

I believe that everyone needs to copy and paste d...

Introduction to the common API usage of Vue3

Table of contents Changes in the life cycle react...

Using keras to judge SQL injection attacks (example explanation)

This article uses the deep learning framework ker...

Vue3 Vue Event Handling Guide

Table of contents 1. Basic event handling 2. Send...

JavaScript prototype and prototype chain details

Table of contents 1. prototype (explicit prototyp...

Detailed explanation of nginx request header data reading process

In the previous article, we explained how nginx r...

Detailed explanation of Vue3 life cycle functions and methods

1. Overview The so-called life cycle function is ...

MySQL uses covering index to avoid table return and optimize query

Preface Before talking about covering index, we m...