Spyros
Spyros

Reputation: 259

Explicit line joining in Python

I am reading a file, line-by-line and doing some text processing in order to get output in a certain format My string processing code goes as follows:

file1=open('/myfolder/testfile.txt')
scanlines=file1.readlines()
string = ''

 for line in scanlines:
    if line.startswith('>from'):
         continue
    if line.startswith('*'):
        continue
    string.join(line.rstrip('\n')) 

The output of this code is as follows:

abc

def

ghi

Is there a way to join these physical lines into one logical line, e.g:

abcdefghi

Basically, how can I concatenate multiple strings into one large string?

If I was reading from a file with very long strings is there the risk of an overflow by concatenating multiple physical lines into one logical line?

Upvotes: 4

Views: 11965

Answers (4)

phynfo
phynfo

Reputation: 4938

I would prefer:

oneLine = reduce(lambda x,y: x+y, \
                 [line[:-1] for line in open('/myfolder/testfile.txt') 
                            if not line.startswith('>from') and \
                               not line.startswith('*')])
  • line[:-1] in order to remove all the \n
  • the second argument of reduce is a list comprehension which extracts all the lines you are interested in and removes the \n from the lines.
  • the reduce (just if you actually need that) to make one string from the list of strings.

Upvotes: 0

koblas
koblas

Reputation: 27108

Cleaning things up a bit, it would be easiest to append to array and then return the result

def joinfile(filename) :
   sarray = []
   with open(filename) as fd :
       for line in fd :
           if line.startswith('>from') or line.startswith('*'):
               continue
          sarray.append(line.rstrip('\n'))
   return ''.join(sarray)

If you wanted to get really cute you could also do the following:

fd = open(filename)
str = ''.join([line.rstrip('\n') for line in fd if not (line.startswith('>from') or line.startswith('*'))])

Yes of course you could read a file big enough to overflow memory.

Upvotes: 3

Constantinius
Constantinius

Reputation: 35089

there are several ways to do this. for example just using + should do the trick.

"abc" + "def" # produces "abcdef"

If you try to concatenate multiple strings you can do this with the join method:

', '.join(('abc', 'def', 'ghi')) # produces 'abc, def, ghi'

If you want no delimiter, use the empty string ''.join() method.

Upvotes: 5

inspectorG4dget
inspectorG4dget

Reputation: 114035

Use string addition

>>> s = 'a'
>>> s += 'b'
>>> s
'ab'

Upvotes: 2

Related Questions