![](/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.