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