3

I have two comma delimited text files with content as below:

File1:

TR,23456.23,H,56789

TR,54678.13,F,14567

TR,67889.12,R,89876

File2:

SG,6789.24,J,53452

SG,8909.25,F,56789

SG,5467.11,S,56783

I want to extract sentences from the first and second file and combine them in a new text file in just one line as below:

TR,54678.13,F,14567,SG,8909.25,F,56789

However, in my code, the result obtained is the following:

TR,54678.13,F,14567

,SG,8909.25,F,56789

Why the sentences from the second file are being written below the sentences from the first file?. I need both sentences to be combined on the same line. Does anybody know why is this happening and how could I solve the problem?.

This is my code:

contfil=0
direct=os.listdir(path1)
for file in direct:
  with open(os.path.join(save_path1,file),'r') as Textfile1:
    for eachline1 in Textfile1:
       for field in eachline1.split():
           ID1=field.split(',') [2]
           with open(os.path.join(save_path2,os.listdir(save_path2) [contfil]),'r') as Textfile2:
               for eachline2 in Textfile2:
                   for field in eachline2.split():
                        ID2=field.split(',') [2]
                        if ID2==ID1:
                           fo=open(os.path.join(save_path3,'Matched_Lines.txt'),'a')
                           fo.write('%s,%s\n' %(eachline1,eachline2))
                           fo.close()
  contfil+=1
3
  • Try printing repr(eachline1) and repr(eachline2). See anything on the end? Commented Mar 4, 2014 at 16:39
  • 3
    python has csv module for reading these types of files Commented Mar 4, 2014 at 16:40
  • What does ' '.join(eachline1, eachline2) produce? Commented Mar 4, 2014 at 17:07

2 Answers 2

2

All you need to do is strip each line. Change this line:

fo.write('%s,%s\n' %(eachline1,eachline2))

to this:

fo.write('%s,%s\n' %(eachline1.strip(),eachline2.strip()))

What's happening is that each line you read in has a newline (\n) at the end. When you print them out, that newline is output. strip removes the newline before outputting.

Sign up to request clarification or add additional context in comments.

2 Comments

trim isn't a python builtin. I think you're thinking of JavaScript's String.trim()? A python equivalent would be str.strip or str.rstrip.
Yeah, I realized the mistake almost immediately. It's fixed. I've been using too many languages recently....
0

You might want to try

combinedline = ' '.join([eachline1, eachline2])

and then

 fo.write('%s\n', % combinedline)

3 Comments

I have also tried this way but I get the following error: "join() takes exactly one argument (2 given)". It might not work for my case?. I have copied the code as you wrote it above.
@Sarah thanks for pointing out my typo. join will take each entry in a list and join them with the preceding string. I forgot the square brackets. Sorry about that.
Not a problem. There is another mistake in the code, the comma before the %. I have tried it again, now it works but I obtain the same results as with my initial code (One line below the other one and not their combination).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.