使用两个文本文件(Python)重新创建句子

使用两个文本文件(Python)重新创建句子,python,Python,我有两个文本文件。一个叫“textfile”,另一个叫“textfile1”。这两个文本文件包含组成句子的单词,另一个包含组成句子的单词在原始句子中的位置。两者都由两个列表组成 sentence = "This and that, and this and this." textfile = ['This', 'and', 'that,', 'this', 'this.' textfile1 = [0, 1, 2, 1, 3, 1, 4] 我用列表重新创建了这个句子。但是我想用文本文件重新创建

我有两个文本文件。一个叫“textfile”,另一个叫“textfile1”。这两个文本文件包含组成句子的单词,另一个包含组成句子的单词在原始句子中的位置。两者都由两个列表组成

sentence = "This and that, and this and this."
textfile = ['This', 'and', 'that,', 'this', 'this.'
textfile1 = [0, 1, 2, 1, 3, 1, 4]
我用列表重新创建了这个句子。但是我想用文本文件重新创建它。我试过:

wrd = []
num = []
textfile =  open ("words.txt", "r")
for row in textfile:
    wrd.append(row.split())

textfile1 =  open ("positions.txt", "r")
for i in textfile1:
    num.append(int(i.strip()))

recreated = ' '.join(wrd[pos] for pos in num)
textfile2 = open ("recreated.txt", "wt") #opening a new text file in write mode
textfile2.write (recreated)
textfile2.close() #closing the text file

然而,这并没有起作用。我想使用两个文本文件的内容重新创建原始句子。谢谢。

您从
textfile1
中读取字符串(如果您确实从文件中读取字符串,而不是从您发布的代码中读取字符串,其中
textfile1
是一个列表)。简短的版本可能如下所示:

wrd = open('textfile', 'r').read().strip().split()
num = [int(token) for token in open('textfile1', 'r').read().strip().split()]

with open('textfile2', 'w') as f:
    f.write(' '.join([wrd[i] for i in num))

嗨,我看到两个错误

首先,循环是在行上迭代,而不是在单词上迭代

for row in textfile:
    wrd += row.split()
这是一个很好的方式,让你所有的话

然后将一些字符串附加到num,而不是int

for i in textfile1:
    row = [int(j) for j in i.split()]
    num += row

是一个拥有整数索引并坚持代码的方法,即使我会考虑重构它。

这里有一个想法:

words = list()
num = list()
with open("words.txt", "r") as f:
    for line in f:
        words += line.split()

with open("positions.txt", "r") as f:
    for line in f:
        num += [int(i) for i in line.split()]

recreated = ' '.join(words[pos] for pos in num)

with open("recreated.txt", "wt") as f:
    f.write(recreated)

如果你有一个类似这样的文件

This
and
that
this
this
你可以这样做

file1 = open('file.txt', 'r')
print list(l.strip() for l in file1)
它打印

['This', 'and', 'that', 'this', 'this']
如果你有这样的文件

This and that this this
你可以

file1.readline().split()
这基本上给出了相同的输出。您可以对另一个文件执行相同的操作,并获得两个列表,就像您在问题中指定的那样。现在,你可以继续重复你的句子

textfile = ['This', 'and', 'that,', 'this', 'this.']
textfile1 = ['0', '1', '2', '1', '3', '1', '4']
recreated = ' '.join(textfile[int(pos)] for pos in textfile1)
最后写回另一个文件

open("recreated.txt", "wt").write(recreated)

您忘记将句子写入
textfile2
。如果将textfile1中的标记用作列表索引,还可以将其转换为整数!这个错误已经纠正了。但是,当我运行代码时,我得到一个错误:“列表索引必须是整数或片而不是字符串@carsten您的第一个循环是在行上迭代,而不是在单词上迭代。@Hackndo如何解决这个问题?我以为这个问题可以用.strip解决。它给出了一个错误:'invalid literal for int(),以10为底'。但是,如果文件确实每行只包含一个数字,那么它应该可以工作。
textfile1
实际上看起来如何?因为程序崩溃,所以为空。对不起,我的意思是
textfile1
这段代码正是我想要的工作方式。但是,打开文件时是否需要“with”?你能不能简单描述一下为什么我的代码不起作用。非常感谢。
with
语句为您处理开盘和收盘。它改进了代码维护和可读性。对于你的代码,我想我的答案的开头会回答你的问题。当您在文件句柄(
textfile
textfile1
textfile2
)上迭代时,它会在各行上迭代
strip
仅删除字符串开头和结尾的选定字符<代码>拆分(分隔符)遇到
分隔符时,将字符串拆分为一个列表。因此,您需要循环遍历每一行,并使用空格(默认)分隔符拆分它们。代码的结尾是正确的,只是对其进行了重构。进一步阅读,我已经尝试完全按照您在此处显示的方式运行代码,但它不会创建重新创建的.txt文本文件。你能帮忙吗?我可以试试。您使用的是哪个python版本?哪个编辑器/IDE?如何运行此代码?文件recreated.txt是否已经存在?您还可以删除
open(“rescreated.txt”、“wt”)
中的
t
,这样您就可以打开
open(“rescreated.txt”、“w”)
。如果recreated.txt已经存在,请检查您是否有权写入(修改)它。