ข้ามไปยังเนื้อหา

Records in a text file

English

Records in a text file

  • Paper 4 often gives a text file where each line is one record, with the fields separated by commas: Ava,72.
  • The task is usually: read every line, split it into fields, store them in lists (or objects), then process them or write some back.
  • Three tools do the work: .strip() removes the newline, .split(",") cuts the line into fields, and int() turns a number field into a number.

The line Ava,72 with its newline is read from Scores.txt, .strip() removes the newline, .split(",") gives the fields "Ava" and "72", and int() turns "72" into 72

เข้าสู่ระบบหรือสร้างบัญชี

IGCSE, A-Level & AP