求教 Python 读文件,每次读 1w 行,有没有可以直接用的函数呢?

2018-07-25 02:00:05 +08:00
 sjmcefc2

用了 readlines ( 10000 ) Returns the next 10000 bytes of line. Only complete lines will be returned. 有没有可以直接用的函数呢?可以顺序每次读 1w 行,最后不足 1w 也能正常返回?

3129 次点击
所在节点    Python
10 条回复
noqwerty
2018-07-25 02:24:32 +08:00
sjmcefc2
2018-07-25 02:37:48 +08:00
@noqwerty 非常感谢。请教如何才能找到自己需要的轮子呢。这个太棒了。
noqwerty
2018-07-25 02:44:05 +08:00
@sjmcefc2 #2 善用 Google 和 StackOverflow 呗,我觉得我这半桶水能问出来的问题基本都是别人问过的……
luzhongqiu
2018-07-25 08:42:08 +08:00
建议迭代器返回,上限用参数传过去不就好了么-。-
qsnow6
2018-07-25 09:36:27 +08:00
linecache 模块了解下
wwwyiqiao
2018-07-25 09:37:45 +08:00
自己封装一个呀
huahuajun9527
2018-07-25 09:58:28 +08:00
```python
def rows_reader(filepath, size=10):
with open(filepath) as f:
tmp = []
for line in f:
tmp.append(line)
if len(tmp) >= size:
yield tmp
tmp = []
if tmp:
yield tmp


for rows in rows_reader('m.md', size=10):
print(rows)

```
necomancer
2018-07-25 10:33:11 +08:00
要是读数据的话可以试试 pandas
import pandas as pd
f = pd.read_csv(<file_name>, chunksize =10000, ...)
更多参数看 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html
然后
for chunk in f:
do_sth(chunk)
sjmcefc2
2018-07-25 11:31:18 +08:00
@wwwyiqiao 觉得还是站在各位巨人肩膀上更好。
sjmcefc2
2018-07-25 11:34:42 +08:00
@luzhongqiu 不知道每个文件有多少行啊,迭代器我再学习下。

@necomancer 非常感谢。不过我这个是个 txt,要按照行来读取。

@huahuajun9527 这个太棒了我,试一下。
另外大家都怎么找到这些常用的模块,而不用自己封装一个呢?当然,高手,封装一个也是一秒钟的事儿。现成的模块对我这种外行来说,如何找到呢?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/473873

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX