![](/img/trans.png)
[英]How to read text file and save specific words into csv or another text file using python
[英]How to read specific words in text file using Python
假设我有一个文本文件,里面是以下内容:
Hello my name is John.
使用 Python 函数进行读取
data = open("test.txt", "r")
print("data.readline(2 - 4))
如何仅从第二个字符 (e) 读取到第四个 (l)。 所以当我运行程序时它会打印“ell”
像这样尝试:
f = open("test.txt", "r")
f.seek(1)
print(f.read(3))
seek(1)
移动到文件中第一个字节的位置, read(3)
读取接下来的 3 个字节。
with open("test.txt", "r") as file:
for line in file:
print(line[1:4])
您可以使用read()
方法来做到这一点:
data = open("test.txt", "r")
print(data.read(4)[1:])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.