代码之家  ›  专栏  ›  技术社区  ›  Jim421616

把反斜杠放进一根绳子里

  •  0
  • Jim421616  · 技术社区  · 6 年前

    我有大量的长文本文件,我想转换成乳胶格式的表。以下是其中一个文件的简短示例:

    List of the best combinations (with |r-value| > 0.5)
    Combination &   r value &   no.obs. &   Kendall's tau
    ============================================================
    B - V   &   0.580019    &   11863   &   1.000000
    B - R   &   0.574867    &   11863   &   1.000000
    V - B   &   -0.580019   &   11863   &   1.000000
    R - B   &   -0.574867   &   11863   &   1.000000
    
    Highest r-value of 0.580019 occurred for B - V
    Lowest r-value of -0.580019 occurred for V - B
    

    我需要将其转换为LaTex文档中的一个表,因此需要将其格式化为:

    List of the best combinations (with |r-value| > 0.5)\\
    \hline
    Combination &   r value &   no.obs. &   Kendall's tau\\
    ============================================================\\
    B - V   &   0.580019    &   11863   &   1.000000\\
    \hline
    B - R   &   0.574867    &   11863   &   1.000000\\
    \hline
    V - B   &   -0.580019   &   11863   &   1.000000\\
    \hline
    R - B   &   -0.574867   &   11863   &   1.000000\\
    \hline
    
    Highest r-value of 0.580019 occurred for B - V\\
    Lowest r-value of -0.580019 occurred for V - B\\
    

    真正的文件会有几十行长,所以用手来做是不切实际的。

    我试过了

    filename = file+'.txt'
    with open(filename, 'r') as infile:
        new_filename = file+'_table.txt'
        with open(new_filename, 'w') as outfile:
            lines = infile.readlines()
            for line in lines:
                end_of_line = r'\\'
                outfile.write(line + end_of_line)
                outfile.write(r'\\hline')
    

    以及来自 here ,但我的输出是

    \List of the best combinations (with |r-value| > 0.5)
    \Combination    &   r value &   no.obs. &   Kendall's tau
    \============================================================
    \B - V  &   0.580019    &   11863   &   1.000000
    \B - R  &   0.574867    &   11863   &   1.000000
    \V - B  &   -0.580019   &   11863   &   1.000000
    \R - B  &   -0.574867   &   11863   &   1.000000
    \
    \Highest r-value of 0.580019 occurred for B - V
    \Lowest r-value of -0.580019 occurred for V - B
    \
    \
    

    \\ \hline 进入 outfile

    2 回复  |  直到 6 年前
        1
  •  1
  •   HaoChien Hung    6 年前

    .txt 文件实际上有一个 \n 在句末! 为了在句末加些东西,我们应该注意这一点。

    我想你可以在“for line in lines”中添加另一行来解决这个问题! line = line.replace("\n", " ") end_of_line = r'\\' ... 并跟随

    如果要创建另一行,只需使用: outfile.write('\n') outfile.write(r'\hline') outfile.write('\n')

        2
  •  0
  •   Jim421616    6 年前

    最终解决了这个问题:

    with open(filename, 'r') as infile:
            new_filename = file+'_table.txt'
            with open(new_filename, 'w') as outfile:
                lines = infile.readlines()
                outfile.write(r'\begin{tabular}{|c|c|c|c|}')
                for line in lines[1:-3]:
                    if line.startswith('='):
                        pass
                    else:
                        line = line.replace('\n', ' '+r'\\'+'\n')
                        outfile.write(line)
                        outfile.write(r'\hline' + '\n')
                outfile.write(r'\end{tabular}')