简体   繁体   中英

Automatically remove characters with regex in sql file with python

I'm having some trouble working on a SQL file which is an eport from my database
Here are the details of the export :

-- MySqlBackup.NET 2.0.9.2
-- Dump Time: 2019-06-07 02:53:03
-- --------------------------------------
-- Server version 5.7.19 MySQL Community Server (GPL)

My problem is that while running a regex over this sql file the regex finds nothing even if the regex is fine and tested online here : https://regex101.com/r/iIEKQh/1/

The regex is: (using re )

pattern2 = re.compile(r"(FOREIGN_KEY_CHECKS)")
print(pattern2.search(FILE))

and the result is always None .

This is the text I'm running over:

-- MySqlBackup.NET 2.0.9.2
-- Dump Time: 2019-06-07 02:53:03
-- --------------------------------------
-- Server version 5.7.19 MySQL Community Server (GPL)
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

Because i want to import this file in my database without the foreign key check i want to uncomment this line /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; in the file so that it works well.

I couldn't find anything online to help me and MySQL doesn't want to add --disable-foreign-key option in command line ...

EDIT: The problem was that in pattern2.search(FILE) as I looked for the python docs , search() looks for the pattern on the FILE (which is c:/....). So that's why it doesn't find anything because it doesn't go through the file itself.

My question is: How can I remove the comments from the SQL file then ?

You could use re.sub to replace the matched object.

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> pattern = re.compile(r'/\*(.*FOREIGN_KEY_CHECKS.*)\*/')
>>>
>>> sample_string = """
... -- MySqlBackup.NET 2.0.9.2
... -- Dump Time: 2019-06-07 02:53:03
... -- --------------------------------------
... -- Server version 5.7.19 MySQL Community Server (GPL)
... /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
... /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
... /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
... /*!40101 SET NAMES utf8 */;
... /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
... /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
... /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
... /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
... """
>>>
>>> def uncomment(match_obj):
...     return match_obj.group(1)
...
>>> print(re.sub(pattern, uncomment, sample_string))

-- MySqlBackup.NET 2.0.9.2
-- Dump Time: 2019-06-07 02:53:03
-- --------------------------------------
-- Server version 5.7.19 MySQL Community Server (GPL)
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 ;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM