Find all strings in python code files

后端 未结 6 1134
清歌不尽
清歌不尽 2021-01-11 23:26

I would like to list all strings within my large python project.

Imagine the different possibilities to create a string in python:

mystring = \"hello         


        
6条回答
  •  孤街浪徒
    2021-01-11 23:54

    Gettext might help you. Put your strings in _(...) structures:

    a = _('Test')
    b = a
    c = _('Another text')
    

    Then run in the shell prompt:

    pygettext test.py
    

    You'll get a messages.pot file with the information you need:

    # SOME DESCRIPTIVE TITLE.
    # Copyright (C) YEAR ORGANIZATION
    # FIRST AUTHOR , YEAR.
    #
    msgid ""
    msgstr ""
    "Project-Id-Version: PACKAGE VERSION\n"
    "POT-Creation-Date: 2009-02-25 08:48+BRT\n"
    "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
    "Last-Translator: FULL NAME \n"
    "Language-Team: LANGUAGE \n"
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=CHARSET\n"
    "Content-Transfer-Encoding: ENCODING\n"
    "Generated-By: pygettext.py 1.5\n"
    
    
    #: teste.py:1
    msgid "Test"
    msgstr ""
    
    #: teste.py:3
    msgid "Another text"
    msgstr ""
    

提交回复
热议问题