Merging contents of two files

Kragen Sitaker kragen at pobox.com
Wed May 15 17:04:19 EDT 2002


Katharina.Pergens at dlr.de writes:
> Sorry sending a html file. Once again.
> So what I have is a text file (.txt) like the following:
> 
> Abbreviation	Explanation		File			Line
> ------------	-----------		----			----
> 	xyz	comment for class	/home/de/abc.java	13
> 	...		...			...		...
> 
> The txt file contains of several lines and rows. The rows are seperated
> by tabs.
> The source file (mentioned as row 'File' in txt file) are java files. I
> search for firstname.secondname in the header:
> 	* Created: dd/mm/yyyy firstname.secondname
> 
> How to write the script. Thanks for your help!!

Something like this (untested):
import linecache, re
headerre = r"reated: \d\d/\d\d/\d\d\d\d (?P<firstname>.*)\.(?P<secondname>.*)"
fileinfo = [line.split("\t") for line in open("infofile.txt").readlines()]
for abbrev, explanation, filename, line in fileinfo:
    line = int(line)
    createdline = linecache.getline(filename, line)
    match = re.search(headerre, createdline)
    if not match:
        print "Line %s of %s didn't look right; it said %s" % (line, filename,
                                                               createdline)
    else: 
        print "abbrev %s was by first name %s second name %s" % (abbrev, 
                         match.group("firstname"), match.group("secondname"))
    
I'm not entirely sure what output you want and whether the line number
in your text file has the Created: comment in it.  If not, it might be
better to just read the file and use re.search to find the Created:
line.  Also, I don't know how accurately folks follow the Created:
convention, an I haven't tested the code.  But I hope this gets you
started.




More information about the Python-list mailing list