python - read a text file and interpret the data -
I have to write a program that reads in a text file named "terms.txt" and then changes the file and It prints it with reference to the first page number. This is what the file looks like:
3: degree 54: connected 93: impending 54: top 19: side 64: neighboring 72: path 55: at least on the route 127: Tree 99: Spread Trees 19: Path 28: Connected 3: Degree 55: Graph 64: Approx 44: Width First Search 77: Neighborhood 55: Degree 55: Depth First Search 19: Degree 27: Neighborhood 16: Tree Tree And after running through the program, it should do this:
imminent, 64 at 9.3 o'clock ago, 44 added, 28, 54 degrees, 3, 19, 55 Deep search before depth, 55 edges, 19 graphs, 55 neighbors, 27, 64, 77 paths, 19,272 minimum routes, 55 spreading trees, 16 trees, 127 trees, 127 tops, 54 Right now, I have it and this page is printing a list of numbers and a list of contexts ... I'm not sure where to go from here to the file in the file: ==] = [] File = open ('terms.txt', 'r'): page number, reference ==
def bookIndex2 (): indexList = Line.split (':') pageNum .append (page number) indexList.append (reference) indexList.sort () Prints (Page New) Print (indexList)
You basically use any such dictionary Need to do, which shows the references of the list of page numbers in the map. As you repeat the file, add the page number to the list for that reference. Once you have processed the entire file, sort the software's contents and print them. I recommend using the standard dictionary instead because it is good enough to produce a dictionary of lists.
import from the archive with the default decode index = default (list) open ('terms.txt') as f: in line f: page, reference = line.strip () . Less (). Division (':') Index [Reference] .append (int (page)) For reference, sorted pages (index.items ()): Print "{}, {}". Format (context, ',' for sorted pages) (i HTML>
Comments
Post a Comment