Question: Write a Python program that reads a file each line of which contains three values separated by any amount of whitespace. There may also be

 Write a Python program that reads a file each line ofwhich contains three values separated by any amount of whitespace. There mayalso be any amount of whitespace (including, in this case, none) before

Write a Python program that reads a file each line of which contains three values separated by any amount of whitespace. There may also be any amount of whitespace (including, in this case, none) before the first value and after the last. The first value on a line should be in a floating-point format (explained in detail below), the second value should be a signed or unsigned decimal integer (see below), and the third value should be one of the single letters 'x', 'y', or 'z'. You write a regular expression intended to match an entire line (so it begins with ' and ends with 'S'). Keep running sums of the floating-point numbers represented by the first values and of the integers represented by the second values in a line. Also maintain a string onto which the third values are concatenated. If one or more of the values in a line are improperly formatted, output that line preceded by the string "No match with "; in this case, the entire line is ignored (making no contribution to the accumulated values). The format for the integer value is simple: either (1) an optional sign followed by a string of digits beginning with a digit other than 'O' or (2) the string consisting only of 'o'. The format for floating-point values is very versatile. There is an optional initial sign. Then, before the decimal point (if there is one), there is either the character 'O' or a string of digits beginning with a digit other than 'O'. There are two possibilities for the remainder. One possibility is a decimal point followed by one or more digits. The other possibility is an optional decimal point and one or more digits, then 'E' or 'e', and then a string of one or more digits optionally preceded by a sign. The following are examples of valid floating-point representations. 12.34 -3.5 32.2E2 4.23e-10 62E+2 0.0 x 23 Y +45 The following are examples of representations that fail to meet this format. 12.E2 (There is no digit after the decimal point.) 062.42 (The initial O' is invalid.) The following is the content of file data.txt, which you can download from the assignment site. 12.34 -79 -3.5 32.2E2 2 4.23e-10 62E+2 -4 0.0 12.E2 -125 062.42 3.45 12.5 -45.2 06 43.6 72 yz -2.63 -10 x The last six lines are invalid. The first two of these six contain the invalid floating-point forms just mentioned. The next two lines contain invalid integer representations as the second values): x Y 0 N X 12987 Z Z x 12.5 (not an integer) and 06 (invalid initial O'). The third values in the last two lines are invalid: yz (only single letters are allowed) and x (must be lower case). The following is the output of the program using data.txt as input. No match with 12.E2 -125 x No match with 062.42 12987 Y No match with 3.45 12.5 No match with -45.2 06 No match with 43.6 yz No match with -2.63 -10 9428.84 -13 xyzxyz To extract the values from the lines, in your regular expression, you will have to do grouping to capture sub-expressions (surrounding sub-expressions to be remembered with parentheses). When you need parentheses for grouping but not capturing, use non-capturing groups, which, recall, are of the form (?:...). 72 X

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!