Say I have a text file and inside is the following:
Hello my name is John.
Using the Python function for reading
data = open("test.txt", "r")
print("data.readline(2 - 4))
How to I read only from the second character (e) to the fourth (l). So when I run the program it prints "ell"
Try it like this:
f = open("test.txt", "r")
f.seek(1)
print(f.read(3))
seek(1)
is move to position of the first byte in file, and read(3)
is read the following 3 bytes.
with open("test.txt", "r") as file:
for line in file:
print(line[1:4])
You can use the read()
method to do it like this:
data = open("test.txt", "r")
print(data.read(4)[1:])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.