将来自两个不同文件的输入连接到一个单独的文件

时间:2016-06-02 14:53:42

标签: string file python-3.5 linecache

我想生成以下行

1 & {\QPCSymbols\XeTeXglyph 2}  & \textarabic{الحمدلله} & \texttt{!} & \verb$\XeTeXglyph 2$  \\
\hline

假设第一个数字是n,第二个数字是n+1\textarabic的内容来自f_nusoos,内容\texttt来自f_keys。两个文件的行数相同。

我已经创建了一个Python3脚本,可以让我得到我想要的东西,但我最终得到了新的线符号(\n)。

这是我到目前为止所拥有的,

#! python3
import linecache

f_nusoos = 'symbol_nass_list.txt'
f_keys   = 'symbol_key_list.txt'
contentfile = open('content.tex', 'w+', encoding="utf-8")

theLine = " "

for x in range(3):
  contentfile.write('{} & {{\QPCSymbols\XeTeXglyph {}}}  & \\textarabic{{{}}} & \\texttt{{{}}} & \\verb$\XeTeXglyph {}$  \\\\'.format(x+1, x+2,linecache.getline(f_nusoos, x+1).rstrip('\n'), linecache.getline(f_keys, x+1), x+2).rstrip('\n'))
  contentfile.write('\hline')

执行后content.tex的内容是,

1 & {\QPCSymbols\XeTeXglyph 2}  & \textarabic{بسم الله الرحمن الرحيم} & \texttt{!
} & \verb$\XeTeXglyph 2$  \\\hline2 & {\QPCSymbols\XeTeXglyph 3}  & \textarabic{بسم الله الرحمن الرحيم} & \texttt{"
} & \verb$\XeTeXglyph 3$  \\\hline3 & {\QPCSymbols\XeTeXglyph 4}  & \textarabic{بسم الله الرحمن الرحيم} & \texttt{#
} & \verb$\XeTeXglyph 4$  \\\hline

然而,我的期望

1 & {\QPCSymbols\XeTeXglyph 2}  & \textarabic{بسم الله الرحمن الرحيم} & \texttt{!} & \verb$\XeTeXglyph 2$\\
\hline
2 & {\QPCSymbols\XeTeXglyph 3}  & \textarabic{بسم الله الرحمن الرحيم} & \texttt{"} & \verb$\XeTeXglyph 3$\\
\hline
3 & {\QPCSymbols\XeTeXglyph 4}  & \textarabic{بسم الله الرحمن الرحيم} & \texttt{#} & \verb$\XeTeXglyph 4$\\
\hline

问题似乎是在texttt{CONTENT之后插入新行。我不知道为什么会这样。

f_nusoos.txt的内容:

بسم الله الرحمن الرحيم
بسم الله الرحمن الرحيم
بسم الله الرحمن الرحيم

f_keys.txt的内容:

!
"
#

1 个答案:

答案 0 :(得分:0)

我长时间盯着这个。

而不是

contentfile.write('{} & {{\QPCSymbols\XeTeXglyph {}}}  & \\textarabic{{{}}} & \\texttt{{{}}} & \\verb$\XeTeXglyph {}$  \\\\'.format(x+1, x+2,linecache.getline(f_nusoos, x+1).rstrip('\n'), linecache.getline(f_keys, x+1), x+2).rstrip('\n'))

应该是

contentfile.write('{} & {{\QPCSymbols\XeTeXglyph {}}}  & \\textarabic{{{}}} & \\texttt{{{}}} & \\verb$\XeTeXglyph {}$  \\\\'.format(x+1, x+2,linecache.getline(f_nusoos, x+1).rstrip('\n'), linecache.getline(f_keys, x+1).rstrip('\n'), x+2))

rstrip()应该是来自文件的输入而不是来自计数器。

相关问题