【问题标题】:How to insert characters at particular locations in text file如何在文本文件的特定位置插入字符
【发布时间】:2020-01-21 08:57:29
【问题描述】:

免责声明:我对 Python 的了解微不足道。

在我的大学实验室课程中,在实验过程中捕获了很多数据,我需要将其插入到表格中,并将其绘制在 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 文件,插入所需的符号并将其返回给我?

【问题讨论】:

  • 你用的是哪个操作系统?
  • 我用的是windows
  • 好的,编写脚本逐行读取文件中的数据并附加您的句子,最后将您的行添加到变量中并保存或重写到另一个文件中。
  • 免责声明:我对python知之甚少

标签: python string file-io


【解决方案1】:
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)

【讨论】:

    【解决方案2】:

    假设您有 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}
    

    【讨论】:

    • 我们不需要将字符串结果写入某个文件吗?
    猜你喜欢
    • 2020-01-20
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    相关资源
    最近更新 更多