Hello all,

just a quick question! I was wondering how can you pace lines together in python. In my case i have a file like this:

<word> <word> <number>.txt
<word> <word> <number>.txt
<word> <word> <word>  <number>.txt
<word> <word><word><word>   <number>.txt
<word> 
<word> <number>.txt
<word> <number>.txt
<word> 
<word> <number>.txt

Does anyone know how can i make the lines with the solo <word> to be attached to the next ones with <word> <number>.txt?

Something like this:

<word> <word> <number>.txt
<word> <word> <number>.txt
<word> <word> <word>  <number>.txt
<word> <word><word><word>   <number>.txt
[B]<word> <word> <number>.txt
[/B]<word> <number>.txt
[B]<word> <word> <number>.txt
[/B]

Can't think of something and i have to admit i am stuck to probably an easy issue to solve. Any ideas?

Many thanks.

Just for possible future help! i found a way of doing that! Here is the code just in case somebody may need it-it is not perfect but can be adjusted to someone's needs :)

numberofspans = 0
spans = open('spans.txt','a')
for line in open('study.labels').readlines()[:-1]:   #read the file except the last line who is empty
    if line.split()[0]=="addToType":
        if line.split()[4]=="study":            #if the line has the string "addtotype" as the first token, then
	    numberofspans = numberofspans + 1              #to count the number of lines
            abname = line.split()[1]                 #split the first one which is the name of the document
	    drdoom= open('docs2/'+abname).read()	#open the folder containing the abstracts
            index = int(line.split()[2])		#split the line further so you have the token where the span starts
            index2 = int(line.split()[3])		#and split the line on the 4rth part to get the whole span
            galactus = drdoom[index:index+index2].replace("\n"," ")       #in drdoom which is the doc2 folder and the respective file name, find the indexes and replace any possible \n with an empty space
            print galactus, abname
	    spans.write(galactus + " " + abname +"\n")
spans.close()

print ""
print "the total number of spans is: ",numberofspans                 #this shows you the total number of spans inside the produced label file from MT

You are doing split multiple times to lines. Better way would be to to open the file in with statement, which garantees closing of the file automatically, split each line once to variable and do the slicing from there. If files are small your code is 'good enough'

Same way as you open a file and close it in the end, it is also good as OP to mark your thread solved after sharing the solution, as you did.

It is good enough as you say for the time being but still requires some 'polishing'. ^_^ thanks for taking the time to reply.

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.