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

Usage of mysql timestamp

Preface: Timestamp fields are often used in MySQL...

Vue realizes the percentage bar effect

This article shares the specific code of Vue to r...

Example of pre-rendering method for Vue single page application

Table of contents Preface vue-cli 2.0 version vue...

JavaScript canvas realizes colorful sun halo effect

This article example shares the specific code of ...

Tutorial on importing and exporting Docker containers

background The popularity of Docker is closely re...

What kinds of MYSQL connection queries do you know?

Preface If the query information comes from multi...

Linux sar command usage and code example analysis

1. CPU utilization sar -p (view all day) sar -u 1...

How to solve the problem of forgetting the root password of Mysql on Mac

I haven't used mysql on my computer for a lon...

Detailed explanation of angular parent-child component communication

Table of contents APIs used Simple Example person...

Set an icon for the website to be displayed on the far left of the browser tab

What is the purpose of this sentence? Copy code Th...

How to add fields and comments to a table in sql

1. Add fields: alter table table name ADD field n...

Vue project implements file download progress bar function

There are two common ways to download files in da...

vsCode generates vue templates with one click

1. Use the shortcut Ctrl + Shift + P to call out ...