开发者

Read a multielement list, look for an element and print it out in python

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?

开发者_StackOverflowThis 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.


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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜