file processing - Handle Commas While Reading Text in Python -
my code trying read log files throughout specified directory in rootdir
, write pieces of information log file outputfile
the issue i'm having searchobj_archive_date.group()
, fullpath
,zdiscsvar
,zcopiesvar
, , searchobj_year_3or6.group()
aren't being read file lines within log files. happens 10% of total outputted lines of text, i'm confused why it's happening of time, instead of e:\filepath\text.txt | 5/23/2015 12:00 | c:\anotherfilepath\text.txt | 23 | 23 | 5year
, e:\filepath\text.txt | | | | |
any insight why error occuring appreciated. code below:
after doing researched, found what's causing error whenever line has comma ,
in it. stops reading line @ comma , skips next line, know workaround this?
an example of input text that's giving me problems: 11/23/2015 12:34:58 adding file d:\fp\fp1\fp2\text, text, text.txt
normally these lines don't have commas, know of way handle commas when reading in lines of text?
import os import re fo = open('outputfile', 'w') fo.write("col|col|col|col|col|col \n") # 1.walk around directory , find log file in 1 of folders rootdir = "c:\\users\\" path, dirs, files in os.walk(rootdir, topdown=false): filename in files: fullpath = os.path.join(path, filename) if (filename=="text.txt"): # 2.open file. read file fi2 = open(fullpath, 'r+') fi2content = fi2.read() zdiscs = re.search(r'(\snumber of copies: (\d{1,2}))', fi2content, re.m|re.i) if zdiscs: zdiscsvar = str(zdiscs.group(2)) zcopies = re.search(r'(number of discs in set: (\d{1,2}))', fi2content, re.m|re.i) if zcopies: zcopiesvar = str(zcopies.group(2)) fi = open(fullpath, 'r') # 3.parse text in incoming file , use regex find path line in fi: #4.write path , info outgoing file m = re.search(r'(adding file(.*))',line) if m: searchobj_adding_file = re.search(r'[a-z]:\\.+', line, re.m|re.i) searchobj_archive_date = re.search(r'^\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}:\d{2}', line, re.m|re.i) searchobj_year_3or6 = re.search(r'\dyear', line, re.m|re.i) if searchobj_adding_file: fo.write(searchobj_adding_file.group() + "|") fo.write(searchobj_archive_date.group() + "|") fo.write(fullpath + "|") fo.write(zdiscsvar + "|") fo.write(zcopiesvar + "|") fo.write(searchobj_year_3or6.group() + '\n') #5. close file fo.close() fi.close() fi2.close()
i removed commas before searching line of text. this, inserted linewocommas = line.replace(',', '')
after if: m
Comments
Post a Comment