如何在文件读取期间改回一行

发布于 2024-10-16 07:20:58 字数 331 浏览 5 评论 0原文

在我的代码中,我有一个像这样的行长度打印:

line = file.readline()
print("length = ", len(line))

之后我开始通过执行以下操作来扫描行:

for i in range(len(line)):
        if(file.read(1) == 'b'):
            print("letter 'b' found.")

问题是 for 循环开始读取文件的第 2 行。 如何让它从第 1 行开始读取而不关闭并重新打开文件?

In my code I have a line length print like this:

line = file.readline()
print("length = ", len(line))

after that I start to scan the lines by doing this:

for i in range(len(line)):
        if(file.read(1) == 'b'):
            print("letter 'b' found.")

The problem is that the for loop starts reading on line 2 of the file.
How can I make it start reading at line 1 without closing and reopening the file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兔姬 2024-10-23 07:20:58

可以使用 file.seek 移动下一次读取的位置,但效率很低。您已经阅读了该行,因此您可以直接处理
line 无需再次阅读。

with open(filename,'r') as f:
    line = f.readline()
    print("length = ", len(line))
    if 'b' in line:
        print("letter 'b' found.")

    for line in f: 
    ...

It is possible to use file.seek to move the position of the next read, but that's inefficient. You've already read in the line, so you can just process
line without having to read it in a second time.

with open(filename,'r') as f:
    line = f.readline()
    print("length = ", len(line))
    if 'b' in line:
        print("letter 'b' found.")

    for line in f: 
    ...
凶凌 2024-10-23 07:20:58

看来你需要专门处理第一行。

lineno = 1
found = False
for line in file:
    if 'b' in line:
        found = True

    if lineno == 1:
        print("length of first line: %d" % len(line))
    lineno += 1

if found:
    print("letter 'b' found.")

It seems that you need to handle the first line specially.

lineno = 1
found = False
for line in file:
    if 'b' in line:
        found = True

    if lineno == 1:
        print("length of first line: %d" % len(line))
    lineno += 1

if found:
    print("letter 'b' found.")
昔梦 2024-10-23 07:20:58

听起来你想要这样的东西:

with open('file.txt', 'r') as f:
    for line in f:
        for character in line:
            if character == "b":
                print "letter 'b' found."

或者如果你只需要号码:

with open('file.txt', 'r') as f:
    b = sum(1 for line in f for char in line if char == "b")
print "found %d b" % b

It sounds like you want something like this:

with open('file.txt', 'r') as f:
    for line in f:
        for character in line:
            if character == "b":
                print "letter 'b' found."

or if you just need the number:

with open('file.txt', 'r') as f:
    b = sum(1 for line in f for char in line if char == "b")
print "found %d b" % b
舂唻埖巳落 2024-10-23 07:20:58
#! usr/bin/env python

#Open the file , i assumed its called somefile.txt
file = open('somefile.txt.txt','r')
#Lets loop through the lines ... 
for line in file.readlines():
    #test if letter 'b' is in each line ... 
    if 'b' in line: 
        #print that we found a b in the line
        print  "letter b found"
#! usr/bin/env python

#Open the file , i assumed its called somefile.txt
file = open('somefile.txt.txt','r')
#Lets loop through the lines ... 
for line in file.readlines():
    #test if letter 'b' is in each line ... 
    if 'b' in line: 
        #print that we found a b in the line
        print  "letter b found"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文