How can I pass a filename as a parameter into my module?

后端 未结 1 1293
再見小時候
再見小時候 2020-12-16 19:41

I have the following code in .py file:

import re

regex = re.compile(
    r\"\"\"ULLAT:\\ (?P-?[\\d.]+).*?
    ULLON:\\ (?P-?[\\d.]         


        
相关标签:
1条回答
  • 2020-12-16 20:21

    You need to read the file in and then search the contents using the regular expression. The sys module contains a list, argv, which contains all the command line parameters. We pull out the second one (the first is the file name used to run the script), open the file, and then read in the contents.

    import re
    import sys
    
    file_name = sys.argv[1]
    fp = open(file_name)
    contents = fp.read()
    
    regex = re.compile(
        r"""ULLAT:\ (?P-?[\d.]+).*?
        ULLON:\ (?P-?[\d.]+).*?
        LRLAT:\ (?P-?[\d.]+)""", re.DOTALL|re.VERBOSE)
    
    match = regex.search(contents)
    

    See the Python regular expression documentation for details on what you can do with the match object. See this part of the documentation for why we need search rather than match when scanning the file.

    This code will allow you to use the syntax you specified in your question.

    0 讨论(0)
提交回复
热议问题