"PYTHON" matches the entire file in regular expressions

Source: Internet
Author: User

1 #Coding:utf-82 ImportRe3 defIdxtoscs (PATH):#IDX conversion to open-think function4Idxfile=open (Path,'R')5Fileread=Idxfile.readlines ()6 idxfile.close ()7p='"(\w) *", \s+ (\d+\\.\d+), \s+ (\d+\\.\d+), \s+ (\d+\\.\d+), \s+ "(\w*)",'8Data=Re.findall (P,fileread)9     PrintDataTenIdxtoscs ('c:/users/administrator/desktop 2/0409.idx')

The above code is to implement re-match the entire file, using FindAll to find all the strings that match the regular expression

However, it appears after running:

File "C:\Users\Administrator\Desktop 2\idxtoscs.py", line 8, in Idxtoscs
Data=re.findall (P,fileread)
File "C:\Python27\lib\re.py", line 181, in FindAll
return _compile (pattern, flags). FindAll (String)
typeerror:expected string or buffer

The problem is on File.readlines ()

Enter Help in Idle (file.readline)

>>> help (File.readlines)-on Method_descriptor:readlines (...)      from The file.          and return a list of the lines so read.     if  is An approximate bound     on the  in the lines returned.

ReadLines is the list of strings for each line of the file, and the Re.findall () method requires the argument to be a string
Replacing fileread=idxfile.readlines () with fileread=Idxfile.read () will solve the
Because File.read () returns a string of the entire file

"PYTHON" matches the entire file in regular expressions

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.