1

I want to merge 2 files. First file (co60.txt) contains only integer values and the second file (bins.txt) contains float numbers.

co60.txt:

11
14
12
14
18
15
18
9

bins.txt:

0.00017777777777777795
0.0003555555555555559
0.0005333333333333338
0.0007111111111111118
0.0008888888888888898
0.0010666666666666676
0.0012444444444444456
0.0014222222222222236

When I merge those two file with this code:

with open("co60.txt", 'r') as a:
    a1 = [re.findall(r"[\w']+", line) for line in a]
with open("bins.txt", 'r') as b:
    b1 = [re.findall(r"[\w']+", line) for line in b]
with open("spectrum.txt", 'w') as c:
    for x,y in zip(a1,b1):
        c.write("{} {}\n".format(" ".join(x),y[0]))

I get:

11 0
14 0
12 0
14 0
18 0
15 0
18 0
9 0

It appears that when I merge these 2 files, this code only merges round values of the file bins.txt.

How do I get that files merge like this:

11 0.00017777777777777795
14 0.0003555555555555559
12 0.0005333333333333338
14 0.0007111111111111118
18 0.0008888888888888898
15 0.0010666666666666676
18 0.0012444444444444456
9 0.0014222222222222236
2
  • 3
    Why do you need the RegEx? Anyway, your problem is that your regex can't match the '.' character, consider using [0-9\.]+ for pattern Commented Jul 30, 2017 at 9:46
  • could you please write an answer with correct code? i'm new to programing. Commented Jul 30, 2017 at 9:53

2 Answers 2

3

You can do it without regex::

with open("co60.txt") as a, open("bins.txt") as b, \
    open("spectrum.txt", 'w') as c:
    for x,y in zip(a, b):
        c.write("{} {}\n".format(x.strip(), y.strip()))

Content of spectrum.txt:

11 0.00017777777777777795
14 0.0003555555555555559
12 0.0005333333333333338
14 0.0007111111111111118
18 0.0008888888888888898
15 0.0010666666666666676
18 0.0012444444444444456
9 0.0014222222222222236
Sign up to request clarification or add additional context in comments.

Comments

2

As mentioned by @immortal, if you want to use regex then use -

b1 = [re.findall(r"[0-9\.]+", line) for line in b]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.