Counting characters and separated fields

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.


Python
"A1, 8.40 \n\nB2,12.05"


The following Python 3 code processes the file:


Python
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.

Record numberCharactersFields
1
2
3

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Text File Operations Questions: