Python uses xlrd to retrieve a column in excel containing a specified string record

Source: Internet
Author: User

Python uses xlrd to retrieve a column in excel containing a specified string record

This article mainly introduces how to use xlrd in python to retrieve a column containing a specified string record in excel. It involves the skills of using xlrd module to retrieve Excel, which is very useful, for more information, see

This example describes how to use xlrd in python to retrieve a column containing a specified string record in excel. Share it with you for your reference. The specific analysis is as follows:

Xlrd is used to retrieve records containing the specified string from a column of data in excel and generate a txt file named after this string.

?

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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

Import OS

Import xlrd, sys

# Input the excel file

Filename = raw_input ('input the file name & path :')

If not OS. path. isfile (Filename ):

Raise NameError, "% s is not a valid filename" % Filename

# Open the excel file

Bk = xlrd. open_workbook (Filename)

# Get the sheets number

Shxrange = range (bk. nsheets)

Print shxrange

# Get the sheets name

For x in shxrange:

P = bk. sheets () [x]. name. encode ('utf-8 ')

Print "Sheets Number (% s): % s" % (x, p. decode ('utf-8 '))

# Input your sheets name

Sname = int (raw_input ('Choose the sheet number :'))

Try:

Sh = bk. sheets () [sname]

Except t:

Print "no this sheet"

# Return None

Nrows = sh. nrows

Ncols = sh. ncols

# Return the lines and col number

Print "line: % d col: % d" % (nrows, ncols)

# Input the check column

Columnnum = int (raw_input ('which column you want to check pls input the num (the first colnumn num is 0 ):'))

While columnnum + 1> ncols:

Columnnum = int (raw_input ('your num is out of range, pls input again :'))

# Input the searching string and column

Testin = raw_input ('input the string :')

# Find the cols and save to a txt

Outputfilename = testin + '.txt'

Outputfile = open (outputfilename, 'w ')

# Find the rows which you want to select and write to a txt file

For I in range (nrows ):

Cell_value = sh. cell_value (I, columnnum)

If testin in str (cell_value ):

Outputs = sh. row_values (I)

For tim in outputs:

Outputfile. write ('% s' % (tim ))

Outputfile. write ('% s' % (OS. linesep ))

Outputfile. close ()

I hope this article will help you with Python programming.

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.