我在csv
文件中有一个单词列表,每个单词在单独的行中。我想阅读15行并将它们连接成一个句子,然后将它们写入新的csv
文件中。然后,对接下来的15行重复此过程,并在新行上添加新句子,直到所有单词都被使用为止。
我已经能够在中创建单词列表,但是由于我是python的新手,所以我不知道如何循环遍历给定的行数并将句子连接到一个新文件中。
不胜感激。
我使用以下代码从文件中创建了包含大量文本的单词列表:
with open("outfile11.csv", encoding = 'UTF_8') as f:
for line in f:
for word in line.split():
print(word)
with open("words.csv","a", encoding = 'UTF_8') as f1:
f1.write(word + "\n")
然后,我使用以下代码从创建的列表文件中删除所有空白行:
with open("words.csv","r", encoding='UTF_8') as f, open("cleanedWords.csv","w", encoding='UTF_8') as outfile:
for i in f.readlines():
if not i.rstrip():
continue
if i:
outfile.write(i)
答案 0 :(得分:0)
如果outfile.csv中的每一行都是一个单词,那么只需在第二个代码示例中即可实现。
with open("words.csv", encoding='UTF_8') as f:
# Create a txt file (or what ever type you want)
with open('sentences.txt', "a", encoding='UTF_8') as sent:
# declare an empty list
words = []
# loop through lines
for line in f:
# add word to the word list
# replace the line break with an empty string
words.append(line.replace('\n', ''))
# check to see if the list length is 15.
if len(words) == 15:
# join all words in list separated by a space
# put a . at the end
# add new line
sent.write("{}.\n".format(" ".join(words)))
# or without a .
# sent.write("{}\n".format(" ".join(words)))
# empty the list
words = []
我希望这会有所帮助。