简体   繁体   中英

Combining lines using reg ex in python

If wanted to combine six lines (each containing 3 elements) so that the final outcome is a single line with three elements so that the first is the addition of all the first elements, the second is the addition of all the second elements and the third is the concatenation of all the third elements.

For example,

We have,

12.34  -79   x
-3.5    23      y
32.2E2   2   z
4.23e-10   +45  x
62E+2    -4     y
0.0    0        z

and we need

9428.84 -13 xyzxyz

Here is my current code:

f = open('data.txt', 'r')
""" opens the file """
import re
""" Imports the regular expressions module"""
# lines = f.readlines ()
lines = list(f)
""" Reads all the lines of the file """


p = re.compile(r'\s*^([-]?([1-9]\d|\d)[E|e]?[+\d]?(.)(\d+(E|e)[-]?\d+|\d+))\s*([-,+]?([1-9]\d+|\d))\s*([x|y|z])$')

for x in lines:
       m = p.match(x)
       if m:
           print (x)

You can do this by zipping the contents of the file so that all number of the first column are on first list, all number of the second column on second list and finally all characters on the third list. Then all you do is simply sum the first two lists and join the third list that contains the characters:

sum1 = 0
sum2 = 0
finalStr = ""

with open("data.txt", "r") as infile:

   lines = list(zip(*[line.split() for line in list(infile)]))

   sum1 = sum(map(float,lines[0]))
   sum2 = sum(map(float,lines[1]))
   finalStr = "".join(lines[2])

   # Some formatting for float numbers
   print("{:.2f}".format(sum1), end=" ") 
   print("{:.0f}".format(sum2), end=" ")

   print(finalStr)

Output:

9428.84 -13 xyzxyz

There is no need for a regex in your case. Regular expressions are used to deconstruct strings, not to combine them. If you do not mind using pandas, the solution takes two lines:

import pandas as pd
data = pd.read_table("data.txt", sep='\s+', header=None)

df.sum().values.tolist()
#[9428.840000000422, -13, 'xyzxyz']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM