【发布时间】:2012-11-12 14:13:17
【问题描述】:
我有一个 Latex 表,用于每周的课程计划,我想从教学大纲中提取数据,并将其存储为列表。(它们将从 csv 中读取)所以教学大纲是 50 章的列表,而乳胶有四年级每周 2 节课的空间,六年级每周 3 节课的空间,我想咬第一节课并坚持第一个令牌,然后是下一个。 . . 现在我的代码只会在星期一、星期三和星期五给我第 1 章,而不是 ch1、ch2、ch3
math6 = ['chapter1', 'chapter2', 'chapter3', 'chapter1-3test']
math4= ['chapter1.1', 'chapter1.2-3', 'chapter2']
\begin{tabular}{|p{0.7in}|p{0.8in}|p{2.2in}|p{.9in}|p{2.6in}|p{1.6in}|}
6${}^{th}$ Math \newline M\newline & _math6_&
6${}^{th}$ Math \newline W \newline & _math6_ &
6${}^{th}$ Math \newline F \newline & _math6_ &
4${}^{th}$ Math \newline M\newline & & _math4_ &
4${}^{th}$ Math \newline W\newline & & _math4_ &
\end{tabular}
这是蟒蛇
import re
template = file('file1.txt', 'r').read()
lost= ["geography", "physics", "hairdressing", "torah"]
n =0
while n<len(lost):
temp=lost[n]
page= re.sub(r'_thing_', temp, template)
print page
n+=1
#page= re.sub(r'_thing_', "martha", template)
#file('result.txt', 'w').write(page)
这给了我
#contents of file1
# Really long Latex
#File that has
# geography, geography, mary, tom, susan, geography
#that I want to replace
#read file1 in as a string, replace, save again
#contents of file1
# Really long Latex
#File that has
# physics, physics, mary, tom, susan, physics
#that I want to replace
#read file1 in as a string, replace, save again
#contents of file1
# Really long Latex
#File that has
# hairdressing, hairdressing, mary, tom, susan, hairdressing
#that I want to replace
#read file1 in as a string, replace, save again
#contents of file1
# Really long Latex
#File that has
# torah, torah, mary, tom, susan, torah
#that I want to replace
#read file1 in as a string, replace, save again
【问题讨论】:
-
您的具体问题是什么?如果你说清楚了,回答起来会容易得多。此外,您应该尽量将您的具体问题与您正在工作的领域分开 - 我们不想研究您的大局(计划创建)只是为了回答一些小问题(例如字符串拆分) .
标签: python regex file-io replace latex