13.8 Text from a file
- Approved by
- Panaversity ·
In everyday life. Someone types a name on a form and adds a space at the end by mistake. Nobody can see the space, but the name no longer matches the list.
What you will learn. How a file's text becomes values, and how an invisible character makes two values that look the same differ.
Why it matters. A value that does not match is skipped or refused, and nothing on the screen shows why.
How does a file become values?
By cutting. A program reads a file as one long string, like a roll of receipt paper. The code cuts the roll into lines, then cuts each line into values. Here are two rows of Brightline's export as a program first sees them. \n is the mark that ends a line, and split does the cutting. Predict what this prints.
// two rows, as one string
const text = "INV-8301,V001,open\nINV-8302,V002,open";
// cut the roll into lines
const lines = text.split("\n");
// cut the first line at commas
const parts = lines[0].split(",");
console.log(lines.length, parts[2]);Both print:
2 opensplit(",") cuts text at each comma and gives a list of the pieces, so parts[2] is the third value on the first line. Every piece is still text, as 13.3 showed.
Here the two rows were typed into the code. In a real script, one line reads the whole file into a string like text. You only need to recognize that line: it names the file's path, its address in the folders.
| Language | A line that reads a file |
|---|---|
| TypeScript | const text = readFileSync("data/invoices.csv", "utf8"); |
| Python | text = Path("data/invoices.csv").read_text(encoding="utf-8") |
What goes wrong when I read text from a file?
Mostly characters you cannot see. Mac and Linux end each line with \n. Windows adds one more invisible character before it, written \r. A script that splits on \n alone leaves that \r stuck to the last value on each line, so "open\r" is not "open". It looks the same on screen, but a comparison does not match it. Each language has one word that removes blank space, \r included, from both ends of a text.
| Language | The word that cleans both ends |
|---|---|
| TypeScript | trim() |
| Python | strip() |
Here is a Windows line, with \r at its end. The code cuts the line twice: once as it is, and once after cleaning its ends. Predict both values.
const line = "INV-8301,V001,open\r";
// cut at each comma
const parts = line.split(",");
// trim() cleans both ends,
// then split cuts
const cleaned = line.trim().split(",");
console.log(parts[2] === "open", cleaned[2] === "open");false trueTry it
The file in 13-8 cuts a Windows line at its commas, and checks its last value. Run it and investigate, as in 13.1.
Modify. Send: "Clean the Windows line before it is split. Don't run it yet." Say what its last line, the check for open, will print now. Then ask the agent to run it.
Make. Send: "Make a new file in 13-8 called mine that cuts the line V005,Hocking Valley Pallets,Net 30 at its commas and prints the vendor's name. Don't run it yet." Say which position the name is at, and then ask the agent to run it.
What you should see. The check said false before, and now it says true, because the invisible \r is gone. mine prints Hocking Valley Pallets, from position 1.
The operating rule: when two values look the same and do not match, suspect a character you cannot see. Clean each line as the code reads it, and test a Windows line on purpose.
Next. A due date is text from a file too. 13.9 shows how code compares dates and adds days to them.
Check yourself
Question 1 / 2
0 answered
A TypeScript script splits a Windows file on \n alone. What happens to the last value on each line?
- Owner
- Panaversity