user2662091
user2662091

Reputation: 133

Read a multi-line string as one line in Python

I am writing a program that analyzes a large directory text file line-by-line. In doing so, I am trying to extract different parts of the file and categorize them as 'Name', 'Address', etc. However, due to the format of the file, I am running into a problem. Some of the text i have is split into two lines, such as:

'123 ABCDEF ST
APT 456'

How can I make it so that even through line-by-line analysis, Python returns this as a single-line string in the form of

'123 ABCDEF ST APT 456'?

Upvotes: 9

Views: 35338

Answers (6)

Mehul
Mehul

Reputation: 115

i think i might have found a easy solution just put .replace('\n', " ") to whatever string u want to convert

Example u have

my_string = "hi i am an programmer\nand i like to code in python"

like anything and if u want to convert it u can just do

my_string.replace('\n', " ")

hope it helps

Upvotes: 0

PyNEwbie
PyNEwbie

Reputation: 4940

Assuming you are using windows if you do a print of the file to your screen you will see

'123 ABCDEF ST\nAPT 456\n'

the \n represent the line breaks.

so there are a number of ways to get rid of the new lines in the file. One easy way is to split the string on the newline characters and then rejoin the items from the list that will be created when you do the split

 myList = [item for item in myFile.split('\n')]
 newString = ' '.join(myList)

Upvotes: 4

perreal
perreal

Reputation: 97938

import re

def mergeline(c, l): 
    if c: return c.rstrip() + " " + l 
    else: return l

def getline(fname):
    qstart = re.compile(r'^\'[^\']*$')
    qend   = re.compile(r'.*\'$')
    with open(fname) as f:
        linecache, halfline = ("", False)
        for line in f:

            if not halfline: linecache = ""  
            linecache = mergeline(linecache, line)

            if halfline: halfline = not re.match(qend, line)
            else: halfline = re.match(qstart, line)

            if not halfline: 
                yield linecache
        if halfline: 
            yield linecache

for line in getline('input'):
    print line.rstrip()

Upvotes: 1

Kevin London
Kevin London

Reputation: 4728

To replace the newlines with a space:

address = '123 ABCDEF ST\nAPT 456\n'
address.replace("\n", " ")

Upvotes: 3

g.d.d.c
g.d.d.c

Reputation: 47978

Assuming you're iterating through your file with something like this:

with open('myfile.txt') as fh:
  for line in fh:
    # Code here

And also assuming strings in your text file are delimited with single quotes, I would do this:

while not line.endswith("'"):
  line += next(fh)

That's a lot of assuming though.

Upvotes: 0

theodox
theodox

Reputation: 12208

if you want to remove newlines:

"".join( my_string.splitlines())

Upvotes: 16

Related Questions