python - 将行写入文档时遇到问题
问题描述
我需要这个脚本来显示每个单词出现在哪一行。问题是我无法在文档中写入行
def WordOnLine():
file = open("tekstbestand.txt","r")
file2 = open("aantalwoorden","a")
lineN = 0
for line in file:
sent = line.split()
lineN += 1
for word in sent:
if len(word) >= 4:
if word in file2:
word = word.replace(word,word+" " +lineN + "\n")
else:
file2.append(word + " " + str(lineN) + "\n")
file.close()
file2.close()
WordOnLine()
解决方案
尝试:
file2 = open("aantalwoorden","a+")
和
file2.write(word + " " + str(lineN) + "\n")
这是一些解释: https ://www.guru99.com/reading-and-writing-files-in-python.html
推荐阅读
- ibm-cloud - 如何仅从变量中提取数字
- python - Python pdf2txt 清理问题
- python - 有没有办法用 matplotlib 自动制作“拼贴”的图?
- sql-server - 在 SQL CLR 中使用套接字 API
- laravel - Laravel How to use external variable on join
- javascript - 如何使用 JQuery 替换脚本标签中的文本
- python - 如何在没有明显 url 的情况下从搜索结果中抓取 pdf 和 html
- javascript - Angular Reactive Form Validator.min/max 在检查值之前检查执行类型转换吗?
- javascript - 在 reactjs 中,如何从其他文件中的 fetch() 获取响应
- linux - 使用 sed 按符号替换模式