Py学习  »  Python

在python中逐行分析文本

Samuurai • 4 年前 • 726 次点击  

以下是我目前掌握的情况:

with open(str(filepath), r) as fp:
    for line in fp:
        rdline = fp.readline() 
        doSomething(rdline)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54604
 
726 次点击  
文章 [ 3 ]  |  最新文章 4 年前
Praveen Kumar
Reply   •   1 楼
Praveen Kumar    4 年前

with open(str(filepath), 'r') as fp:
    lines = fp.readlines() 
    for line in lines:
        do_something(line)

将open函数的第二个参数作为字符串。

(或)

如果文件大小很大,可以使用这种方法

with open(str(filepath), 'r') as fp:
    line = fp.readline() 
    while line:
        do_something(line)
        line = fp.readline()
accdias
Reply   •   2 楼
accdias    4 年前

正如我在评论中所说 rdline = fp.readline() fp 在你的for循环中。

with open(filepath) as f:
    for line in f: 
        do_something(line)
Pablo Anaquín
Reply   •   3 楼
Pablo Anaquín    4 年前

试试这个:

with open('filepath') as infile:
  for row in infile:
    do_somthing(row.strip())