如何在文本文件的特定位置插入字符
How to insert characters at particular locations in text file
免责声明:我对Python知之甚少。
在我的大学实验室课程中,在实验过程中捕获了大量数据,我需要将其插入 table 并将其绘制在 Latex 文件中,数据记录在 excel 中,我从那里复制它并将其存储在一个 .txt 文件中,如下所示:
4.86 0.23
4.83 0.27
4.78 0.39
4.66 0.66
4.5 1.02
4.4 1.23
4.25 1.52
4.11 1.78
3.99 2
3.81 2.29
3.57 2.64
3.45 2.79
3.43 2.82
Latex代码中对应部分为:
\begin{longtable}{|c | c |}
\hline
$V_{out}$(in V) & $I_{out}$(in mA) \ \hline
4.86 & 0.23\ \hline
4.83& 0.27\ \hline
4.78& 0.39\ \hline
4.66& 0.66\ \hline
4.5 &1.02\ \hline
4.4 &1.23\ \hline
4.25& 1.52\ \hline
4.11& 1.78\ \hline
3.99& 2\ \hline
3.81& 2.29\ \hline
3.57& 2.64\ \hline
3.45& 2.79\ \hline
3.43& 2.82\ \hline
\caption{\Output Characteristics for low input}
\label{tab:output@low}
\end{longtable}
所以在我的 .txt 文件的每一行中,我必须在每一行中手动插入 & 和 \\ \hline,但是由于有很多这样的数据文件,这个手动过程消耗了大量时间,有人可以建议一个 python 代码,它可以读取 txt 文件,插入所需的符号,然后 return 返回给我?
假设您有 data.txt
所有实验结果:
4.86 0.23
4.83 0.27
4.78 0.39
4.66 0.66
4.5 1.02
4.4 1.23
4.25 1.52
4.11 1.78
3.99 2
3.81 2.29
3.57 2.64
3.45 2.79
3.43 2.82
您可以阅读它并逐行构建您的输出文件:
with open('data.txt', 'r') as f:
lines = f.readlines()
result = '''\begin{longtable}{|c | c |}
\hline
$V_{out}$(in V) & $I_{out}$(in mA) \ \hline
'''
for line in lines:
val1, val2, = line.split()
result += f'{val1}&{val2}\\ \hline\n'
result += '''\caption{\Output Characteristics for low input}
\label{tab:output@low}
\end{longtable}'''
with open('result.txt', 'w') as f:
f.write(result)
输出文件包含:
egin{longtable}{|c | c |}
\hline
$V_{out}$(in V) & $I_{out}$(in mA) \ \hline
4.86&0.23\ \hline
4.83&0.27\ \hline
4.78&0.39\ \hline
4.66&0.66\ \hline
4.5&1.02\ \hline
4.4&1.23\ \hline
4.25&1.52\ \hline
4.11&1.78\ \hline
3.99&2\ \hline
3.81&2.29\ \hline
3.57&2.64\ \hline
3.45&2.79\ \hline
3.43&2.82\ \hline
\caption{\Output Characteristics for low input}
\label{tab:output@low}
\end{longtable}
import io
# watch the `r` in header, footer & adding to raw_data lines, `r` is raw, it's meant to take strings as is
header= r'''\begin{longtable}{|c | c |}
\hline
$V_{out}$(in V) & $I_{out}$(in mA) \ \hline'''
footer = r'''\caption{\Output Characteristics for low input}
\label{tab:output@low}
\end{longtable}'''
with open('raw_data.txt', encoding='utf8') as raw_data, open('result.txt', 'w', encoding='utf8') as result:
result.write(header)
for line in raw_data.readlines():
datapoint1, datapoint2 = line.split()
result.write(datapoint1 + '& ' + datapoint2 + r'\ \hline' + '\n')
result.write(footer)
免责声明:我对Python知之甚少。
在我的大学实验室课程中,在实验过程中捕获了大量数据,我需要将其插入 table 并将其绘制在 Latex 文件中,数据记录在 excel 中,我从那里复制它并将其存储在一个 .txt 文件中,如下所示:
4.86 0.23
4.83 0.27
4.78 0.39
4.66 0.66
4.5 1.02
4.4 1.23
4.25 1.52
4.11 1.78
3.99 2
3.81 2.29
3.57 2.64
3.45 2.79
3.43 2.82
Latex代码中对应部分为:
\begin{longtable}{|c | c |}
\hline
$V_{out}$(in V) & $I_{out}$(in mA) \ \hline
4.86 & 0.23\ \hline
4.83& 0.27\ \hline
4.78& 0.39\ \hline
4.66& 0.66\ \hline
4.5 &1.02\ \hline
4.4 &1.23\ \hline
4.25& 1.52\ \hline
4.11& 1.78\ \hline
3.99& 2\ \hline
3.81& 2.29\ \hline
3.57& 2.64\ \hline
3.45& 2.79\ \hline
3.43& 2.82\ \hline
\caption{\Output Characteristics for low input}
\label{tab:output@low}
\end{longtable}
所以在我的 .txt 文件的每一行中,我必须在每一行中手动插入 & 和 \\ \hline,但是由于有很多这样的数据文件,这个手动过程消耗了大量时间,有人可以建议一个 python 代码,它可以读取 txt 文件,插入所需的符号,然后 return 返回给我?
假设您有 data.txt
所有实验结果:
4.86 0.23
4.83 0.27
4.78 0.39
4.66 0.66
4.5 1.02
4.4 1.23
4.25 1.52
4.11 1.78
3.99 2
3.81 2.29
3.57 2.64
3.45 2.79
3.43 2.82
您可以阅读它并逐行构建您的输出文件:
with open('data.txt', 'r') as f:
lines = f.readlines()
result = '''\begin{longtable}{|c | c |}
\hline
$V_{out}$(in V) & $I_{out}$(in mA) \ \hline
'''
for line in lines:
val1, val2, = line.split()
result += f'{val1}&{val2}\\ \hline\n'
result += '''\caption{\Output Characteristics for low input}
\label{tab:output@low}
\end{longtable}'''
with open('result.txt', 'w') as f:
f.write(result)
输出文件包含:
egin{longtable}{|c | c |}
\hline
$V_{out}$(in V) & $I_{out}$(in mA) \ \hline
4.86&0.23\ \hline
4.83&0.27\ \hline
4.78&0.39\ \hline
4.66&0.66\ \hline
4.5&1.02\ \hline
4.4&1.23\ \hline
4.25&1.52\ \hline
4.11&1.78\ \hline
3.99&2\ \hline
3.81&2.29\ \hline
3.57&2.64\ \hline
3.45&2.79\ \hline
3.43&2.82\ \hline
\caption{\Output Characteristics for low input}
\label{tab:output@low}
\end{longtable}
import io
# watch the `r` in header, footer & adding to raw_data lines, `r` is raw, it's meant to take strings as is
header= r'''\begin{longtable}{|c | c |}
\hline
$V_{out}$(in V) & $I_{out}$(in mA) \ \hline'''
footer = r'''\caption{\Output Characteristics for low input}
\label{tab:output@low}
\end{longtable}'''
with open('raw_data.txt', encoding='utf8') as raw_data, open('result.txt', 'w', encoding='utf8') as result:
result.write(header)
for line in raw_data.readlines():
datapoint1, datapoint2 = line.split()
result.write(datapoint1 + '& ' + datapoint2 + r'\ \hline' + '\n')
result.write(footer)