如何将一个文件中的每 3 个字符写入另一个文件 python3

How to write every 3rd character from one file to a different file python3

这是作业的一部分。我想用 ulaz 变量读取输入文件,并将该文件中的每 3 个字符写入输出文件。我正在考虑将第一个文件写入列表,而不是先删除 '\n'。在那之后,当写入一个新文件时,我只能想到使用 [::3] 来取每第 3 个字符。目前,我仍在为输入数据而苦苦挣扎。每当我写一个 lista(列表)时,我都会得到所有的行,但不是第一行。我不确定为什么会这样?

ulaz = input("Unesite ime fajla:")
# izlaz = input("Unesite ime fajla:")

with open(ulaz) as existing_file:
    lista = []
    for line in existing_file:
         lista.append([s.rstrip('n') for s in existing_file])
    print(lista)

existing_file.read() 将为您提供一个包含文件中所有文本的字符串。然后,您可以使用问题中提到的下标。

ulaz = input("Unesite ime fajla:")

with open(ulaz) as existing_file:
    text = existing_file.read()
    print(text[::3])