Skip to content

Instantly share code, notes, and snippets.

@mgeraci
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save mgeraci/a4d49352dbd00e444f73 to your computer and use it in GitHub Desktop.

Select an option

Save mgeraci/a4d49352dbd00e444f73 to your computer and use it in GitHub Desktop.
Overlay and combine text files
#!/usr/bin/env python
# given two files, combine them line by line, character by character, such that
# the output takes a character from whichever file iesn't blank (defaulting to
# the character in file1)
import linecache
import os
path = os.path.dirname(os.path.realpath(__file__))
file1 = "%s/text.txt" % path
file2 = "%s/earth.txt" % path
open_file1 = open(file1, "r")
open_file2 = open(file2, "r")
line_count1 = len(open_file1.readlines())
line_count2 = len(open_file2.readlines())
line_count = max(line_count1, line_count2)
# for each line in the files
for j in range(0, line_count):
line1 = linecache.getline(file1, j)
line2 = linecache.getline(file2, j)
line_length = max(len(line1), len(line2))
line_res = []
# for each character in the line, add the one that isn't blank to the results
for i in range(0, line_length):
char1 = False
char2 = False
# assign the characters
if i < len(line1):
char1 = line1[i]
if i < len(line2):
char2 = line2[i]
# kill newlines
if char1 == "\n":
char1 = " "
if char2 == "\n":
char2 = " "
# has neither
if char1 == False and char2 == False:
res = ""
# has one
elif char1 and char2 == False:
res = char1
# has the other
elif char2 and char1 == False:
res = char2
# has both
elif char1 and char2:
if char1 == " " and char2 != " ":
res = char2
else:
res = char1
line_res.append(res)
print "".join(line_res)
_ooppH[`MMMD::---_
.oHMMMR:"&MZ\ `"' |$-_
..dMMMMMMMMdMMM#9\ `'HHo.
. ,dMMMMMMMMMMM"`' ` ?MP?.
. |MMMMMMMMMMM' `"$b&\
- |MMMMHH##M' HMMH?
- TTMM| >.. \MMMMMH
: |MM\,#-""$~b\. `MMMMMM+
. ``"H&# &MMMMMM|
: *\v,#MHddc. `9MMMMMb
. MMMMMMMM##\ `"":HM
- .HMMMMMMMMMMRo_. |M
: |MMMMMMMMMMMMMMMM#\ :M
- `HMMMMMMMMMMMMMMM' |T
: `*HMMMMMMMMMMMM' H'
: MMMMMMMMMMM| |T
. MMMMMMMM?' ./
`. MMMMMMH' ./
-. |MMMH#' .
. `MM* . '
-. #M: .-
` . ., .-
'-.-~ooHH__,,v~--`
C$$\ 'b$$$\
b&$$$#2 !$$$$#Bb
B$$$$$$$& 2$$$$$$$B
!$$$&B#$$$! B$$&!&$$$- B!\
c2B $$$B &$$# &$$! $$$c &$$$&
c$$$' -$$$c b$$$- #$$&!&$$# 2$$$$$b
'$$$! c$$$- '$$$b #$$$$$$&- c$$$&$$c
cc B$$& -$$$c '$$$b '$$$#$$$&- '$$$2!$$c
!#$& b$$$' '$$$! '$$$b -$$$ B$$& &$$# 2$$c b#b
'&$$$# '$$$! &$$#' !$$$' c$$$ b$$$ 2$$$c B$$c '$$$!
'#$$$2c B$$& b$$$#2B$$$& b$$$BB$$$# c$$$$Bc&$$c B$$$c
B$$&' B b$$$' 'b B$$$$$$$#' !$$$$$$$$ '$$$$$$$$$$c b$$$2
'$$$' b#$# '$$$2b&$$c 2#$$$#2' c2B&#$#& &$$$B&$$$$$c '$$$#
b$$# &$$$$! B$$$$$$$2 '2&$B c2$$$c B$$$-
c$$$ $$$$$$' b$$$$$Bb' c' '$$$- b$$$2
'$$$2 c2 B$$& $$&b' _ooppH[`MMMD::---_ -$$$''$$$#
B$$$c 2$$$ c' .oHMMMR:"&MZ\ `"' |$-_ c2#-B$$$'
-$$$$2!&$$$! ..dMMMMMMMMdMMM#9\ `'HHo. c$$$#'
b$$$$$$$$b . ,dMMMMMMMMMMM"`' ` ?MP?. b$$$$!
!$$$$$&- . |MMMMMMMMMMM' `"$b&\ 'B$$$&'
'!22c - |MMMMHH##M' HMMH? b$$$'
- TTMM| >.. \MMMMMH '&b
: |MM\,#-""$~b\. `MMMMMM+
. ``"H&# &MMMMMM|
: *\v,#MHddc. `9MMMMMb
. MMMMMMMM##\ `"":HM
- .HMMMMMMMMMMRo_. |M
cc : |MMMMMMMMMMMMMMMM#\ :M bB
-$$c - `HMMMMMMMMMMMMMMM' |T b$$c
#$$$ : `*HMMMMMMMMMMMM' H' '$$! !&
#$$$& : MMMMMMMMMMM| |T 2$2 c$$#-
&$#&$c . MMMMMMMM?' ./ #$c $$$$$c
B$#b$& c&2 `. MMMMMMH' ./ #$B 2!'$$!
2$$'B$-'B$$$2 -. |MMMH#' . 'Bb !$$2 !$#
c# '$&b$$#$$& c . `MM* . ' !$$ &$$$#$$c
&$$$#!#$& -$&c -. #M: .- &$B B$$$$b
&$$2'#$#' &$$$B- ` . ., .- b$&c '$$b c!!'
'2 #$#' b$$2#$2 '-.-~ooHH__,,v~--` 2$$$$B'b$$
#$$- #$$' !##2- 'B#'-$$#$$$!&$B
B$c !$$$$! c$$$$$! c!! '$$2 !$&c&$$$$$b
V '$$B!$& 2$#'2$!'&$$&b cbc b#$$$b 2$# #$2 b#$$$b
B$$b b$$b &$$#$$' '$$$B b$$&22' -$$b c$$c 2B'
b#$$2 B$$b $$2 b! b$#$$ #$B &$& 2$#'
!$$c B$$ B$$2 B$2&$c '$$c B#$! b$$' #$!
'b c$&cb$$ '&$$& '$$'2$2 '$$b $$$B '$$2 c!
-#$$$$# !$$b b$$-!$# &$& b$& B$#
!&#B' &&c-$$! B$$$$$$- !$$BbB$$' c2-
&$$$$$''$$#&&$$! B$$$$$B'
'bB&B- b$$- B$# B2!-
C$$\ 'b$$$\
b&$$$#2 !$$$$#Bb
B$$$$$$$& 2$$$$$$$B
!$$$&B#$$$! B$$&!&$$$- B!\
c2B $$$B &$$# &$$! $$$c &$$$&
c$$$' -$$$c b$$$- #$$&!&$$# 2$$$$$b
'$$$! c$$$- '$$$b #$$$$$$&- c$$$&$$c
cc B$$& -$$$c '$$$b '$$$#$$$&- '$$$2!$$c
!#$& b$$$' '$$$! '$$$b -$$$ B$$& &$$# 2$$c b#b
'&$$$# '$$$! &$$#' !$$$' c$$$ b$$$ 2$$$c B$$c '$$$!
'#$$$2c B$$& b$$$#2B$$$& b$$$BB$$$# c$$$$Bc&$$c B$$$c
B$$&' B b$$$' 'b B$$$$$$$#' !$$$$$$$$ '$$$$$$$$$$c b$$$2
'$$$' b#$# '$$$2b&$$c 2#$$$#2' c2B&#$#& &$$$B&$$$$$c '$$$#
b$$# &$$$$! B$$$$$$$2 '2&$B c2$$$c B$$$-
c$$$ $$$$$$' b$$$$$Bb' c' '$$$- b$$$2
'$$$2 c2 B$$& $$&b' -$$$''$$$#
B$$$c 2$$$ c' c2#-B$$$'
-$$$$2!&$$$! c$$$#'
b$$$$$$$$b b$$$$!
!$$$$$&- 'B$$$&'
'!22c b$$$'
'&b
cc bB
-$$c b$$c
#$$$ '$$! !&
#$$$& 2$2 c$$#-
&$#&$c #$c $$$$$c
B$#b$& c&2 #$B 2!'$$!
2$$'B$-'B$$$2 'Bb !$$2 !$#
c# '$&b$$#$$& c !$$ &$$$#$$c
&$$$#!#$& -$&c &$B B$$$$b
&$$2'#$#' &$$$B- b$&c '$$b c!!'
'2 #$#' b$$2#$2 2$$$$B'b$$
#$$- #$$' !##2- 'B#'-$$#$$$!&$B
B$c !$$$$! c$$$$$! c!! '$$2 !$&c&$$$$$b
V '$$B!$& 2$#'2$!'&$$&b cbc b#$$$b 2$# #$2 b#$$$b
B$$b b$$b &$$#$$' '$$$B b$$&22' -$$b c$$c 2B'
b#$$2 B$$b $$2 b! b$#$$ #$B &$& 2$#'
!$$c B$$ B$$2 B$2&$c '$$c B#$! b$$' #$!
'b c$&cb$$ '&$$& '$$'2$2 '$$b $$$B '$$2 c!
-#$$$$# !$$b b$$-!$# &$& b$& B$#
!&#B' &&c-$$! B$$$$$$- !$$BbB$$' c2-
&$$$$$''$$#&&$$! B$$$$$B'
'bB&B- b$$- B$# B2!-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment