A sensor export contains exactly the text represented by this Python string literal. The spaces inside the literal belong to the file, and the last record has no final newline.
"A1, 8.40 \n\nB2,12.05"
The following Python 3 code processes the file:
with open("sensor.txt", "r", encoding="utf-8") as source: for number, raw in enumerate(source, start=1): clean = raw.rstrip("\n") characters = len(clean) fields = len(clean.split(","))
Complete the table with the values of characters and fields for each record. Enter exact integers, counting spaces as characters.
Unlock full course notes by signing up
Averaging accepted force records
An instrument exports a UTF-8 text file with the following physical lines. An entry labelled blank represents an empty line; all other entries show the literal line contents without their newline characters.
The processing specification says to skip the header, ignore blank lines, and exclude records whose force field is exactly NA. All remaining force fields are converted to numbers. Calculate the arithmetic mean of the accepted force measurements. Answer in N, to one decimal place.
Rejecting malformed records explicitly
A Python parser processes engineering records after checking the header time_s,force_N. The specification permits blank lines, but every nonblank data line must contain exactly two comma-separated fields that both convert with float(). Any malformed record must stop processing and identify its physical line number.
The current implementation is:
Potential malformed records include "1.0,7.2,extra" and "1.5,error". Which revision satisfies the complete specification?
Replacing a report only after validation
An engineering analysis script reads measurements.txt and produces report.txt, which already contains a previous valid report. The requirements are:
A successful run replaces the entire previous report with a new header and mean force.
If any input record is malformed, the previous report remains unchanged.
Reconciling written characters and stored bytes
An engineer generates a temperature report with the following Python 3 code. All writes succeed, and the file is closed before its size is measured.
With this newline setting, each \n passed to write is stored as \r\n. In UTF-8, ° occupies two bytes; every other character used in this output occupies one byte. The file contains no additional prefix bytes.
Find the final value of reported, measured in characters, and the resulting file size, measured in bytes. Give both answers as exact integers.