蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验临时目录 Persons.txt 读写、嵌套 foreach 渲染与 CSV 引号字段解析对比

PYTHONWindows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。explore-webpages-files.py
explore-webpages-files.py112 行
"""Offline ASP.NET semantic verification lab for Chapter 10 (webpages-files)."""
import csv
import html
import io
import json
import tempfile
from pathlib import Path

VERSION = "CPython 3.12.14 offline ASP.NET WebPages/Core semantic verifier (IIS/WebMatrix absent)"


def case_persons_txt_appdata_write_and_read():
    content = "George,Lucas\nSteven,Spielberg\nAlfred,Hitchcock\n"
    with tempfile.TemporaryDirectory() as tmp:
        app_data = Path(tmp) / "App_Data"
        app_data.mkdir()
        persons_file = app_data / "Persons.txt"
        persons_file.write_text(content, encoding="utf-8")
        lines = persons_file.read_text(encoding="utf-8").splitlines()
    expected = {
        "line_count": 3,
        "first_line": "George,Lucas",
        "last_line": "Alfred,Hitchcock",
    }
    actual = {
        "line_count": len(lines),
        "first_line": lines[0],
        "last_line": lines[-1],
    }
    return {
        "name": "persons_txt_appdata_write_and_read",
        "summary": "在隔离临时目录模拟 App_Data/Persons.txt 写入与 File.ReadAllLines 逐行读取",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_razor_foreach_split_html_render():
    user_data = ["George,Lucas", "Steven,Spielberg", "Alfred,Hitchcock"]
    chunks = ["<!DOCTYPE html><html><body><h1>Reading Data from a File</h1>"]
    for data_line in user_data:
        for data_item in data_line.split(","):
            chunks.append(f"{html.escape(data_item)} &nbsp;")
        chunks.append("<br />")
    chunks.append("</body></html>")
    rendered = "".join(chunks)
    expected = (
        "<!DOCTYPE html><html><body><h1>Reading Data from a File</h1>"
        "George &nbsp;Lucas &nbsp;<br />"
        "Steven &nbsp;Spielberg &nbsp;<br />"
        "Alfred &nbsp;Hitchcock &nbsp;<br />"
        "</body></html>"
    )
    return {
        "name": "razor_foreach_split_html_render",
        "summary": "模拟 Razor 嵌套 foreach 与 Split(',') 配合 <text>&nbsp;</text> 渲染每行数据项",
        "expected": expected,
        "actual": rendered,
        "passed": rendered == expected,
    }


def case_excel_csv_quoted_field_comparison():
    csv_sample = 'Director,Notable Film\n"Lucas, George","Star Wars"\n"Spielberg, Steven","Jurassic Park"\n'
    naive_fields = csv_sample.splitlines()[1].split(",")
    proper_rows = list(csv.reader(io.StringIO(csv_sample)))
    expected = {
        "naive_split_len": 3,
        "proper_csv_len": 2,
        "proper_row1": ["Lucas, George", "Star Wars"],
    }
    actual = {
        "naive_split_len": len(naive_fields),
        "proper_csv_len": len(proper_rows[1]),
        "proper_row1": proper_rows[1],
    }
    return {
        "name": "excel_csv_quoted_field_comparison",
        "summary": "对比简单 Split(',') 与标准 CSV 解析器在含逗号双引号字段的 Excel .csv 文件上的行为差异",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_flat_file_formats_catalog():
    formats = {".txt": "纯文本平面文件", ".xml": "结构化标记文本", ".csv": "逗号分隔值表格"}
    expected = {"count": 3, "extensions": [".csv", ".txt", ".xml"]}
    actual = {"count": len(formats), "extensions": sorted(formats)}
    return {
        "name": "flat_file_formats_catalog",
        "summary": "核对常见平面文件格式(.txt、.xml、.csv)及服务器端 App_Data 隔离目录规范",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_persons_txt_appdata_write_and_read(),
        case_razor_foreach_split_html_render(),
        case_excel_csv_quoted_field_comparison(),
        case_flat_file_formats_catalog(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。

入口文件:projects/aspnet-webpages-files-lab/explore-webpages-files.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-webpages-files.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:ASP.NET

课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2794.zip

下载课程关联工程包:ASP.NET

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2835.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习