Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

joining only lines with spaces in python

I have the below data.. where you can see spaces between 2 lines in the beginning and no spaces in between some other lines:

Report Area

Total Population

Total Land Area
(Square Miles)

Population Density 
(Per Square Mile)


Report Area 37,325,068 155,738.02 239.67 
Alameda County, CA 1,515,136 738.82 2,050.75 
Alpine County, CA 1,197 738.13 1.62 
Amador County, CA 37,764 594.43 63.53 
Butte County, CA 220,101 1,636.03 134.53 
Calaveras County, CA 45,507 1,019.74 44.63 
Colusa County, CA 21,329 1,150.43 18.54 

I need the final output as this:

Report Area  Total Population  Total Land Area(Square Miles)  Population Density(Per Square Mile)

Report Area 37,325,068 155,738.02 239.67 
Alameda County, CA 1,515,136 738.82 2,050.75 
Alpine County, CA 1,197 738.13 1.62 
Amador County, CA 37,764 594.43 63.53 
Butte County, CA 220,101 1,636.03 134.53 
Calaveras County, CA 45,507 1,019.74 44.63 
Colusa County, CA 21,329 1,150.43 18.54 

I have tried using output = " ".join(line.strip( ) for line in f) but it doesnt work as I need.

this is my output:

Report Area  Total Population  Total Land Area (Square Miles)  Population Density (Per Square Mile)   Report Area 37,325,068 155,738.02 239.67 Alameda County, CA 1,515,136 738.82 2,050.75 Alpine County, CA 1,197 738.13 1.62 Amador County, CA 37,764 594.43 63.53

all the lines in a single line..

like image 347
Pavan Chakravarthy Avatar asked Nov 12 '14 06:11

Pavan Chakravarthy


2 Answers

    import re
p = re.compile(ur'\n\n|\n(?=[^\n]*\n\n)', re.IGNORECASE)
test_str = u"Report Area\n\nTotal Population\n\nTotal Land Area\n(Square Miles)\n\nPopulation Density \n(Per Square Mile)\n\n\nReport Area 37,325,068 155,738.02 239.67 \nAlameda County, CA 1,515,136 738.82 2,050.75 \nAlpine County, CA 1,197 738.13 1.62 \nAmador County, CA 37,764 594.43 63.53 \nButte County, CA 220,101 1,636.03 134.53 \nCalaveras County, CA 45,507 1,019.74 44.63 \nColusa County, CA 21,329 1,150.43 18.54 "
subst = u" "

result = re.sub(p, subst, test_str)

You can try via re.

like image 131
vks Avatar answered Sep 21 '22 17:09

vks


Using conditional list comprehension:

output = os.linesep.join(line for line in f if " " in line)
like image 29
Łukasz Rogalski Avatar answered Sep 20 '22 17:09

Łukasz Rogalski