Hi All
I am working on the project detailed below and need some help please.
The flow diagram is the best I could do, I'm afraid.
I need some direction as to what function to use to undertake the pointing to various lines of text and also the instruction to use to address that text file line.
I appreciate that this is probably not the best approach to the problem, but would like to try my way first, if its possible, as a learning exercise.
I will then approach the group when I have completed the project ( I hope, lol ) to request input with regards to a better, neater approach.

Many thanks in anticipation.
Graham

#A text file that is an English to Greek  dictionary and has 2 coulombs. 
# As there are many entries it takes up 25 or so pages. The object is to 
# produce a file that has 2 x 2 coulombs and so producing half the number 
#of pages.
# 
# A sample of the original file is shown below
#
#August                          Αύγουστος (ο)                        
#aunt                              θεία  η                              
#autumn                         φθινόπωρο (το)                       
#bad, ugly (adj.)              άσχημος, -η, -ο                      
#bad, wicked, evil (adj.)   κακός, -ή, -ό                        
#bank                              τράπεζα (η)                          
#basket                           καλάθι (το)     




#otigDict.txt   original File
#newDict.txt    new dictionary
lif = 1        # Lines_in_File 
lpp = 1        # Lines per page, around 72
pc  = 1        # Pages completed
lpa = 1        # Line pointer a 
lpb = lpa + lpp # Line pointer b
	
								
#Open text files , the original to read the other to append						
#Calc. Lines in origDict.txt  = lif						
#Take lines per page from user  = lpp (default = 72)						
#
#					Q			
#                       								
#Read line  and delete cr = n (using lpa)						
#								
#Read line = m (using lpb)						
#add strings so  n + m 	( need to look at tab here)[B][/B]			
#append to new file						
#Print new line						
#increment lpa and lpb						
#								
#lpa = lpp * pc		no		(loop back to Q)		
#
#     	yes					
#
#
#increment pc						
#lpa = pc x lpa						
#lpb = lpa + lpp						
#								
#lpb = lif +1		no		(loop back to Q)		
# 	yes					
#								
#Print 'Job Done'

Recommended Answers

All 3 Replies

Here are somme comments on what you ask.
This is not a working program but gives you functions you can use for the different steps using your way.
I don't understand everything you are doing and this seems very complicated to me. Very old style to deal with files.
Python offers tools that ease this kind of jobs very much. I propose you a working code after yours. It could be more efficient but would be less readable by using list comprehension so i wrote it this way.

#otigDict.txt   original File
#newDict.txt    new dictionary
lif = 1        # Lines_in_File
lpp = 1        # Lines per page, around 72
pc  = 1        # Pages completed
lpa = 0        # Line pointer a >> list first element is indexed 0
lpb = lpa + lpp # Line pointer b


#Open text files , the original to read the other to append
originalFile=open(otigDict,'r')
newDictionnary=open(newDict,'w')
#Calc. Lines in origDict.txt  = lif
# > for this, you have to load the whole file
lines=originalFile.readlines() # load the file in a list
nbLines=len(lines) # length of the list
#Take lines per page from user  = lpp (default = 72)
lpp=input("number of lines") # ask for the number of lines
if lpp=='':
    lpp=72
#
#					Q
#
#Read line  and delete cr = n (using lpa)
# > I don't understant what you mean with cr=n
lineA=lines[lpa] # read the line number lpa
#
#Read line = m (using lpb)
lineB = lines[lpb]
#add strings so  n + m 	( need to look at tab here)[B][/B]
newString="%s\t%s" % (lineA, lineB)
#append to new file
newDictionnary.write(newString)
#Print new line
#increment lpa and lpb
lpa+=1
lpb+=1
#
# > I don't understand what you try to do from here
# > This seems very complicated to me
#lpa = lpp * pc		no		(loop back to Q)
#
#     	yes
#
#
#increment pc
#lpa = pc x lpa
#lpb = lpa + lpp
#
#lpb = lif +1		no		(loop back to Q)
# 	yes
#
#Print 'Job Done'

So here is the solution I would use.
Your dico is to be called "dicoFile.txt" but you can change that in the code

nbLinesPerPage=input("number of lines")
if nbLinesPerPage=='':
    nbLinesPerPage=72
counter=0 # to count the lines
colnum=0 # in which column will I put the line
newPages=[] # Will store ([col0], [col1]) lists
newPage=[[],[]] # a page consists of 2 columns
for line in open #('dicoFile.txt'):
    if counter == 2*nbLinesPerPage: # I reinit the counters
        counter=0
        colnum=0
        newPages.append(newPage[:]) # Copy of the newPage
        newPage=[[],[]]
    if counter == nbLinesPerPage: # I store the following lines in the second column
        colnum=1
    newPage[colnum].append(line.strip()) # append the line in the right column of the new page. strip the '\n'
    counter+=1

outfile=file('dico2cols.txt','w')
for page in newPages:
    outfile.write("------------------------------------------------------------------\n")
    for i, line in enumerate(page[0]):
        try:
            outfile.write("%s\t%s\n" % (line, page[1][i])) # columns are separated with a \t
        except IndexError: # 2d column doesn't have as many lines as column 1
            outfile.write("%s\n" % (line))

outfile.close()

Hi jice

Thanks for you assistance with this project.
cr=carrage return, delete after reading from original file before adding the 2nd coulomb.
The end part of the flow diagram, ;- once the first 72 lines as directed by pointer a and the corresponding 72 lines from pointer b have been almalgamated into two colombs, the pointer a needs to be 72 *2 and pointer c 72*3 for the next group to be made into two coulombs.
Thanks again for your help, you have given me plenty of direction and I can now learn these new functions that I am unfamiliar with and proceed.
Graham

Ok.
You can delete cr by doing

lineA=lines[lpa].strip()
lineB=lines[lpb].strip()

For the end of your code, i think you can do the calculation by yourself :-)

BTW, i saw a mistake in my code :

for line in open('dicoFile.txt'): # I don't know why i have put a comment here
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.