如何在文本文件中的单词前添加数字?

时间:2017-11-15 13:17:50

标签: python python-3.x

我在文本文件中有这个菜单:

#Menu 1 file
Jelly Fish Yee Sang with Pear
Dried Seafood with Fish Soup
Steamed Sea Water Grouper

我有这段代码:

menuList = input('enter number:')
def printPackage(menuList):
    if menuList == '1':
        with open('menu/Menu1.txt')as f:
            data = f.read()
            print(data)
printPackage(menuList)

我应该在代码中添加什么内容,以便将文本文件打印出来?

---------
Menu List
---------
1. Jelly Fish Yee Sang with Pear
2. Dried Seafood with Fish Soup
3. Steamed Sea Water Grouper

请帮助。

2 个答案:

答案 0 :(得分:2)

<!-- Trigger/Open The Modal -->
<button id="myBtn">Anthony</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p class="Info">
      Nachname:König<br> Vorname:Anthony
      <br> Geburtstag:24.12.1998
      <br> Herkunft:Deutschland
      <br> Adresse:xxxxxxxxxxxxxxxxx
      <br> Mobilnummer:xxxxxxxxxxxxxx
      <br> E-Mail:xxxxxxxxxxxxxxxxxbr> Fachlicher Schwerpunkt:Programmieren<br> Hobbys:Basketball, Tennis, Krafttraining, Gaming und Fussball.<br> Socialmedia:url Facebook: url Instagram:<br> Snapcode: anthony.k1ng<br>
    </p>
  </div>

</div>
<!-- Trigger/Open The Modal -->
<button id="myBtn">David</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p class="Info">
      Nachname:König<br> Vorname:Anthony
      <br> Geburtstag:24.12.1998
      <br> Herkunft:Deutschland
      <br> Adresse:xxxxxxxxxxxxxxxxxxxxxx
      <br> Mobilnummer:xxxxxxxxxxxxxxxxxx
      <br> E-Mail:xxxxxxxxxxxxxxxx
      <br> Fachlicher Schwerpunkt:Programmieren<br> Hobbys:Basketball, Tennis, Krafttraining, Gaming und Fussball.<br> Socialmedia:url Facebook: url Instagram:<br> Snapcode: anthony.k1ng<br>
    </p>
  </div>

</div>

答案 1 :(得分:0)

尝试这种Pythonic WAY (更快):

get_input = input('Enter Number: ')
if not get_input == "1":
    exit()
write_save = open("menu/result.txt", "a")
read_lines = [write_save.write("{}. {}{}".format(counts, line.rstrip("\n"), "\n")) for counts, line in enumerate(open('menu/Menu1.txt'), 1)]
write_save.close()

甚至更短:

get_input = input('Enter Number: ')
if not get_input == "1":
    exit()
read_lines = [open("result.txt", "a").write("{}. {}{}".format(counts, line.rstrip("\n"), "\n")) for counts, line in enumerate(open('Menu1.txt'), 1)]

JUST ONE LINE(LOOL)

if input("Enter Number: ") == "1" : [open("result.txt", "a").write("{}. {}{}".format(counts, line.rstrip("\n"), "\n")) for counts, line in enumerate(open('Menu1.txt'), 1)]