【问题标题】:How do I write the paragraph the file as I want in Python?如何在 Python 中根据需要编写文件段落?
【发布时间】:2020-09-02 00:55:48
【问题描述】:

您好,“1. hafta”旁边的“16. hafta”部分和“2. hafta”旁边的“17. hafta”部分怎么写。

dosya = open('deneme.txt', 'w')

def rövanslar(array,hafta_numarasi):
    yeni_hafta = swap_cols(array, 0, 1)
    rövans_haftasi=hafta_numarasi+15
    count = 0
    count2 = 0
    for i in range(1, 9):
        dosya.write("%d. hafta %d. maç: %d. Takım vs %d. Takım \n" % (rövans_haftasi, i, yeni_hafta[count, count2], yeni_hafta[count, count2 + 1]))
        count += 1

    dosya.write("\n")

for i in range(1,9):
    dosya.write("%d. hafta %d. maç: %d. Takım vs %d. Takım \n" % (hafta_numarasi, i, hafta1_eslesmeler[count, count2], hafta1_eslesmeler[count, count2 + 1]))
    count += 1

dosya.write("\n")

rövanslar(hafta1_eslesmeler,hafta_numarasi)

这是我代码的文件操作部分。首先,我正在编写“1. hafta”部分。之后我写“16. hafta 部分,但我想把它写在”1. hafta”部分。

编辑:我使用 zip 方法。这里是新图像。 :(

【问题讨论】:

  • 你需要对这个问题更加清楚。你把上面的文字写在文件里了吗?这是您需要读取然后以不同顺序写入另一个文件的文件吗?
  • 您现有的用于写入文件的代码是什么样的?
  • 我现在将编辑问题。对不起。
  • 请不要发布代码、数据或 Tracebacks 的图像。将其复制并粘贴为文本,然后将其格式化为代码(选择它并输入ctrl-k)...Discourage screenshots of code and/or errors

标签: python numpy file text


【解决方案1】:
  • 读取所有以 1 开头的行
  • 读取所有以 16 开头的行
  • 将它们压缩在一起并制作字符串
  • 重复所有其他配对

如果您有或制作字符串列表

a = ['1 abcd1',
     '1 abcd2',
     '1 abcd3']

b = ['16 efgh1',
     '16 efgh2',
     '16 efgh3']

zipjoin 他们的台词

for lines in zip(a,b):
    new = ' '.join(lines)
    print(new)

>>>
1 abcd1 16 efgh1
1 abcd2 16 efgh2
1 abcd3 16 efgh3
>>> 

【讨论】:

  • 你能做 1 对吗?我不太了解zip方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-12
  • 2012-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多