Python Programming help please - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Python Programming help please

Vikram_cycloneVikram_cyclone Posts: 1Member
Hi there,

I got this txt file with following contents:



My Glossary
stuff

The above text is saved in test.txt file.

I need to write a python script that would load the above file as list of strings. I then need display only the text from to in python.

If anybody can help me please with the code. I've tried searching a lot on net but haven't found anything.

Thank You

Comments

  • bubbatremellbubbatremell Posts: 39Member
    [code]
    import re

    name_block = re.compile('(?P.*)')

    def get_name_block():
    #open the file for reading
    file_name = "test.txt"
    f = open(file_name, 'r')

    #look through it one line at a time
    name_block = ""
    search_result = None
    for l in f:
    search_result = name_block.match(l)
    if search_result:
    name_block = search_result.groups[0]
    return name_block
    [/code]

    That will have a problem if ... spans over multiple lines, because "for l in f" pulls one line at a time. In that case, no one line would ever match the regex. Hope that works for you!

Sign In or Register to comment.