#-*-Coding:utf-8-*-"" "Created on Fri June 15:49:06 2016 @author: User" "" Import OS #从文件中读取某一行 LINECACHE.CHECKCAC He can refresh the cache, Linecache can cache a line of information import Linecache def getfilenameandext (filename): (Filepath,tempfilena
me) = Os.path.split (filename);
(shotname,extension) = Os.path.splitext (TempFileName); Return shotname filelist=[] fileoutlist=[] for filename in Os.listdir (R ' D:\input '): print filename pa= ' D:\input\ %s '%filename filelist.append (PA) name=getfilenameandext (PA) name+= '. csv ' pa= ' D:\output\%s%name ' Fileou Tlist.append (PA) Print filelist print ' ****try to open file ' LineCount = Len (open (R ' D:\input\000.txt ', ' RU '). Readli NES ()) print ' This file%d lines '%linecount s1= ' maincontent= ' ' print ' ===final data===== ' for Var in range (14,linec
OUNT+1): Global maincontent theline = Linecache.getline (R ' D:\input\000.txt ', var) s= theline[15:13+104] s = S.replace (', ', ') # replaces all k in the string with 8 s = S.REplace (', ', ') # replaces all k in the string with 8 if var%2 = = 0:s+= (', ') s1= s else:string =s1.st RIP (' \ n ') + s+ ' \ n ' maincontent +=string print s print ' ===final data===== ' Print maincontent # open a file FO =
Open (Fileoutlist[0], "WB") Fo.write (maincontent);
# Close Open File Fo.close ()