Python traverses the database and obtains the key value

Source: Internet
Author: User

Python traverses the database and obtains the key value

This article will share with you how to use Python to traverse the database and obtain the key value, mainly through the for loop. for more information, see.

Traverse the Redis database with keys stored in key-value mode in the format of PREFIX _ * and print their values.

SCAN is used for traversal, because keys prefix _ * may cause Redis to block for a long time.

Query uses pipeline to reduce interaction and improve efficiency.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

Import redis

Import hiredis

 

Pool = redis. ConnectionPool (host = '127. 0.0.1 ', port = 127, db = 0)

R = redis. Redis (connection_pool = pool)

 

Pipe = r. pipeline ()

Pipe_size = 100000

 

Len = 0

Key_list = []

For key in r. scan_iter (match = 'prefix _ * ', count = 100000 ):

Key_list.append (key)

Pipe. get (key)

If len <pipe_size:

Len + = 1

Else:

For (k, v) in zip (key_list, pipe.exe cute ()):

Print k, v

Len = 0

Key_list = []

 

For (k, v) in zip (key_list, pipe.exe cute ()):

Print k, v

Attach the code for other web pages. For more information, see

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

# Filename itertaorfilefolder

Import OS

Import OS. path

 

FilePath = raw_input ('enter filepath :')

 

# Traversing folders

# Three parameters: return 1. parent directory 2. All folder names (excluding paths) 3. All file names

For parent, dirnames, filenames in OS. walk (filePath ):

# Output Folder Information

For dirname in dirnames:

Print 'parent is: '+ parent

Print 'dirname is '+ dirname

# Output file information

For filename in filenames:

Print 'parent is: '+ parent

Print 'filename is: '+ filename

# Output file path information

CurrentPath = OS. path. join (parent, filename)

Print 'the fulll name of the file is: '+ currentPath

Filesize = OS. path. getsize (currentPath)/1024/1024

Print 'The file size is: %. 3f mb' % (filesize)

# Delete files larger than 50 MB

If filesize> 50:

Delete = raw_input ('Are you sure to delete? ')

If delete = 'yes ':

OS. remove (currentPath)

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.