有人可以帮我将数字列表打印到现有的文本文件中吗?

时间:2019-05-28 07:31:53

标签: python-3.x list printing range numeric

我正在尝试在Python3中创建一个文件,该文件包含特定范围内的完整数字列表(也许是一个字母或两个字母),或者是否更容易将输出写入现有文本文件。我希望列表看起来像这样:
...

27768A123
27768A124
27768A125
27768A126
27768A127
27768A128
27768A129
27768A130
27768A131....

依次类推,直到打印出我范围内的每个数字为止。

4 个答案:

答案 0 :(得分:0)

此代码应为您提供帮助:

if __name__ == '__main__':
    with open("output.txt", "w+") as newFile:
        newFile.write("This line is added to new file\n")
        newFile.write("This is the second line added to new file\n\n\n")

    with open("output.txt", "a+") as existingFile:
        existingFile.write("From Here lines are appended to file\n")
        for i in range(10):
            existingFile.writelines("%d\n" % (i))

答案 1 :(得分:0)

使用f-string构造来构建值:

prefix = '27768A'
values = []
for idx in range(100, 110, 1):
    values.append(f'{prefix}{idx}')

使用with open将值写入文件:

with open(filename, 'w') as f:
    for value in values:
        f.write(value)

答案 2 :(得分:0)

numbers = ["27768A123", "27768A124", "27768A125", "27768A126", "27768A127", "27768A128", "27768A129", "27768A130", "27768A131", "..."]
f = open("output.txt", "w")
for i in numbers:
    f.write(i + "\n")
f.close()

答案 3 :(得分:0)

您可以使用简单的字符串连接来实现:

<body>
  <div class="area-x"></div>
  <nav class="main-menu" id="accordian">
     <div>
        <ul>
           <li id="menu1">
              <h3><i class="fa fa-user fa-2x"></i><span class="nav-text">Client</span></h3>
              <ul>
                 <li><a href="#" id="menu2">test</a>
                    <ul>
                       <li id="menu3">
                          <a href="#">test11111111111</a>
                          <a href="#">test1</a>
                          <a href="#">test1</a>
                          <a href="#">test1</a>
                          <a href="#">test1</a>
                          <a href="#">test1</a>
                          <a href="#">test1</a>
                          <a href="#">test1</a>
                          <a href="#">test1</a>
                          <a href="#">test1</a>
                       </li>
                    </ul>
                 </li>
                 <li><a href="#" id="menu2">test</a>
                    <ul>
                       <li id="menu3">
                          <a href="#">test1</a>
                          <a href="#">test1</a>
                          <a href="#">test1</a>
                          <a href="#">test1</a>
                       </li>
                    </ul>
                 </li>
              </ul>
           </li>