读取多元素列表,查找元素并在 python 中打印出来

发布于 2024-11-15 21:00:01 字数 899 浏览 5 评论 0原文

我正在编写一个 python 脚本来编写一个 tex 文件。但我必须使用另一个文件中的一些信息。这样的文件在每一行都有我需要使用的菜单名称。我使用 split 来为“菜单”的每一行提供一个列表。 例如,我必须用列表中的每个第二个元素编写一个部分,但运行后,我得到了任何东西,我能做什么?

这大致就是我正在做的事情:

    texfile = open(outputtex.tex', 'w')
    infile = open(txtfile.txt, 'r')
        for line in infile.readlines():
            linesplit = line.split('^')
            for i in range(1,len(infile.readlines())):
               texfile.write('\section{}\n'.format(linesplit[1])) 
               texfile.write('\\begin{figure*}[h!]\n')
               texfile.write('\centering\n')
               texfile.write('\includegraphics[scale=0.95]{pg_000%i.pdf}\n' %i)
               texfile.write('\end{figure*}\n')
               texfile.write('\\newpage\n')
    texfile.write('\end{document}')
    texfile.close()

顺便说一下,在 inclugraphics 行中,我必须将 pg_ 之后的数字从“0001”增加到“25050”。有什么线索吗??

我真的很感谢你的帮助。

I am writing a python script in order to write a tex file. But I had to use some information from another file. Such file has names of menus in each line that I need to use. I use split to have a list for each line of my "menu".
For example, I had to write a section with the each second element of my lists but after running, I got anything, what could I do?

This is roughly what I am doing:

    texfile = open(outputtex.tex', 'w')
    infile = open(txtfile.txt, 'r')
        for line in infile.readlines():
            linesplit = line.split('^')
            for i in range(1,len(infile.readlines())):
               texfile.write('\section{}\n'.format(linesplit[1])) 
               texfile.write('\\begin{figure*}[h!]\n')
               texfile.write('\centering\n')
               texfile.write('\includegraphics[scale=0.95]{pg_000%i.pdf}\n' %i)
               texfile.write('\end{figure*}\n')
               texfile.write('\\newpage\n')
    texfile.write('\end{document}')
    texfile.close()

By the way, in the inclugraphics line, I had to increace the number after pg_ from "0001" to "25050". Any clues??

I really appreciate your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱武穆 2024-11-22 21:00:01

我不太明白你的问题。但我在您的代码中看到了几个错误。最重要的是:

    for line in infile.readlines():
    ...
    ...
        for i in range(1,len(infile.readlines())):

一旦你读取了一个文件,它就消失了。 (您可以取回它,但在这种情况下没有意义。)这意味着第二次调用 readlines 不会产生任何结果,因此 len(infile.readlines()) == 0 。假设您在此处编写的内容确实是您想要执行的操作(即编写 file_len * (file_len - 1) + 1 行?),那么也许您应该将文件保存到列表中。另外,您没有在文件名周围加上引号,并且您的缩进很奇怪。试试这个:

with open('txtfile.txt', 'r') as infile:    # (with automatically closes infile)
    in_lines = infile.readlines()
in_len = len(in_lines)

texfile = open('outputtex.tex', 'w')
for line in in_lines:
    linesplit = line.split('^')
    for i in range(1, in_len):
        texfile.write('\section{}\n'.format(linesplit[1])) 
        texfile.write('\\begin{figure*}[h!]\n')
        texfile.write('\centering\n')
        texfile.write('\includegraphics[scale=0.95]{pg_000%i.pdf}\n' %i)
        texfile.write('\end{figure*}\n')
        texfile.write('\\newpage\n')
texfile.write('\end{document}')
texfile.close()

也许您实际上并不想要嵌套循环?

infile = open('txtfile.txt', 'r')
texfile = open('outputtex.tex', 'w')
for line_number, line in enumerate(infile):
    linesplit = line.split('^')
    texfile.write('\section{{{0}}}\n'.format(linesplit[1])) 
    texfile.write('\\begin{figure*}[h!]\n')
    texfile.write('\centering\n')
    texfile.write('\includegraphics[scale=0.95]{pg_000%i.pdf}\n' % line_number)
    texfile.write('\end{figure*}\n')
    texfile.write('\\newpage\n')
texfile.write('\end{document}')
texfile.close()
infile.close()

I don't quite follow your question. But I see several errors in your code. Most importantly:

    for line in infile.readlines():
    ...
    ...
        for i in range(1,len(infile.readlines())):

Once you read a file, it's gone. (You can get it back, but in this case there's no point.) That means that the second call to readlines is yielding nothing, so len(infile.readlines()) == 0. Assuming what you've written here really is what you want to do (i.e. write file_len * (file_len - 1) + 1 lines?) then perhaps you should save the file to a list. Also, you didn't put quotes around your filenames, and your indentation is strange. Try this:

with open('txtfile.txt', 'r') as infile:    # (with automatically closes infile)
    in_lines = infile.readlines()
in_len = len(in_lines)

texfile = open('outputtex.tex', 'w')
for line in in_lines:
    linesplit = line.split('^')
    for i in range(1, in_len):
        texfile.write('\section{}\n'.format(linesplit[1])) 
        texfile.write('\\begin{figure*}[h!]\n')
        texfile.write('\centering\n')
        texfile.write('\includegraphics[scale=0.95]{pg_000%i.pdf}\n' %i)
        texfile.write('\end{figure*}\n')
        texfile.write('\\newpage\n')
texfile.write('\end{document}')
texfile.close()

Perhaps you don't actually want nested loops?

infile = open('txtfile.txt', 'r')
texfile = open('outputtex.tex', 'w')
for line_number, line in enumerate(infile):
    linesplit = line.split('^')
    texfile.write('\section{{{0}}}\n'.format(linesplit[1])) 
    texfile.write('\\begin{figure*}[h!]\n')
    texfile.write('\centering\n')
    texfile.write('\includegraphics[scale=0.95]{pg_000%i.pdf}\n' % line_number)
    texfile.write('\end{figure*}\n')
    texfile.write('\\newpage\n')
texfile.write('\end{document}')
texfile.close()
infile.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文