Hello everyone,

I am writting a code where result is not coming correct because I am not able to join 2 lines to make a single line.

Please help me

e.g my input file is:

AATTCCGGTTT
CCTTAACCCCC

I want my code to first join them together as
AATTCCGGTTTCCTTAACCCCC

and then do what my rest code is doing

so please tell me what python code can do this...!!

It will be a great help please suggest..

Thanks in advance

Hi see if this help you.

>>> str_input = '''AATTCCGGTTT
CCTTAACCCCC'''
>>> str_input
'AATTCCGGTTT\nCCTTAACCCCC'
>>> x = str_input.replace ('\n', '')
>>> x
'AATTCCGGTTTCCTTAACCCCC'
>>>

Thanks for the reply but
I am reading lines in a file so i guess i ned to
do something while reading lines..
eg:

with open('filename') as fil:
	f = fil.readlines()
	for line in f:

I do not know what to do from here...

Please suggest

Many Thanks

Somthing like this.
readlines() return a list,so we just use join().
Something with a for loop should also be possibly.
See if this solve it.

>>> my_file = open('test.txt', 'r')
>>> x = my_file.readlines()
>>> x
['AATTCCGGTTT\n', 'CCTTAACCCCC']
>>> y = ''.join(x)
>>> y
'AATTCCGGTTT\nCCTTAACCCCC'
>>> finish = y.replace('\n', '')
>>> finish
'AATTCCGGTTTCCTTAACCCCC'
>>>

If you want to concatenate (join) every two lines in your file, then you can use something simple like this ...

names = """\
Valerie
Vanessa
Velvet
Venice
Venus
Verena
"""

# create a test file
filename = "mynames.txt"
fout = open(filename, "w")
fout.write(names)
fout.close()

mod_list = []
count = 1
for line in file(filename):
    # strip ending whitespaces including newline char
    line = line.rstrip()
    # concatenate every two lines
    if count % 2 == 0:
        mod_list.append(old_line+line)
    else:
        old_line = line
    count += 1

print(mod_list)

"""
my result -->
['ValerieVanessa', 'VelvetVenice', 'VenusVerena']
"""

Hello,

Thanks for replying to the post but this is getting very complicated for me to use this in my code:

here is what I am trying to do:

My input file is:

>test100-1
TTGACGAGCAATGACAC
CGGAGTGGGATC
>test100-2
TGGCAATATTCTGT
CAAGTGGGTA
>test100-3
ttccgg
what i am trying to do is:
making complements of the sequence and save in a file with the same format.

result file should be:
>test100-1
AACTGCTCGTTACTGTG
GCCTCACCCTAG
>test100-2
ACCGTTATAAGACA
GTTCACCCAT
>test100-3
AAGGC

PROBLEM IN MY CODE:

In the file where i have 2 lines I am not able to join them so that i get the results correct.

here is my code:

from __future__ import with_statement

def lukup(comp):
	complement= {'A':'T','T':'A','C':'G','G':'C','a':'T','t':'A','c':'G','g':'C'}
	return complement[comp]
	
 #defining forward complement and saving in a file.

with open ('C:\\Documents and Settingsdna.txt','r') as fil:
    f = fil.readlines()
    result = []
    listheader = []
    for lin in f:
        if lin[0]=='>':
            listheader = lin
            #print listheader
        if ">" not in lin:
                line1 = ""
                line1 = lin[0:-1]
                end = len(line1)
                dna = [lukup(line1[i:i+1])for i in range(0,end)]
                # calling above defined directory
                dna = (''.join(dna))# converting in string
                dna = listheader + dna
                result.append(dna)
                print result

                   
with open ('C:\\Documents and Settings\\dna1.txt','w') as resultfil:
    resultfil.write('\n'.join(result))

What I am getting from above code is:

>test100-1
AACTGCTCGTTACTGTG
>test100-1
GCCTCACCCTAG
>test100-2
ACCGTTATAAGACA
>test100-2
GTTCACCCAT
>test100-3
AAGGC

PLEASE HELP ME I NEED TO FIX THIS CODE ...!!!

MANY THANKS..!!

Hi,

Did you figure out this problem. I a also facing same problem. In my case, I also want to remove header line (first line) and want to catenate rest of the lines.

Thanks
Sudipta

I also want to remove header line (first line) and want to catenate rest of the lines.

This code removes the first line of a file and prints the rest, concatenated

whith open("myfile.txt", "rb") as ifh:
    next(ifh) # skip first line
    print(''.join(x.rstrip('\n') for x in ifh)) # read the rest, concatenate and print
# Heading Here #
Write a Python program to create a text file of multiple lines. Display the following:  
1. Entire text file  
2. 1st lines of the text file.  
3. m lines starting from the nth line  
4. number of words in each line 
with open("FilemultiLines.txt") as f :
    print('Entire Content of File is :: \n')
    print(f.read().strip())
    f.seek(0)
    print('\n 1st n Line of Text File :: \n')
    print(f.readline().strip())
    f.seek(0)
    m = int(input("\nenter line no. from where you want to start : "))
    n = int(input("enter line no. upto where you want to end : "))
    print("\nm lines starting from the nth line  :: \n")
    for i in (f.readlines() [m-1:n]) :
        print(i,end = '' )
    print('\n')
    print('number of words in each line : \n')
    f.seek(0)
    for i in f.readlines() :
        words = len(i.split(' '))
        print('Line  ',i,' is :: ',words) 
commented: 11 years later. Please be timely. -3
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.