哎呀。我誤解了問題!
不過,如果您剛好想要計算腳註,並且使用的是提供 unix 命令列工具的環境(例如 linux / mac / windows 上的 unix-tools),那麼您可以這樣做:
cat FILENAME | tr '\\' '\n' | awk '/^f /{footnote++;}/^x /{xref++;}/^c /{print chapter, footnote, xref ; chapter=0+$2; footnote=0;xref=0;}END{print chapter, footnote, xref; }'
它的作用是確保每個新的 SFM 都從新的一行開始,然後計算腳註和交叉參考,當出現章節編號(或結束時)時,它會列印出計數。不過我不會寫 python,而且註釋是在 XML 檔案中,用 awk 來解碼實在太複雜了。
Bother. I misunderstood the question!
Anyway, if you happened to want to count footnotes, and are using something with unix command-line tools available (e.g. linux / mac / unix-tools for windows), then you can do something like this:
cat FILENAME | tr '\\' '\n' | awk '/^f /{footnote++;}/^x /{xref++;}/^c /{print chapter, footnote, xref ; chapter=0+$2; footnote=0;xref=0;}END{print chapter, footnote, xref; }'
What it does is make sure that every new SFM starts a new line, and then counts the footnotes, xrefs and when there’s a chapter number (or the end) it prints the counts. I don’t speak python though, and notes are in an XML file which is quite a bit too nasty to decode with awk.
機器翻譯自 English