- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一些非常大的文本文件 (>2g),我想逐字处理。这些文件是以空格分隔的文本文件,没有换行符(所有单词都在一行中)。我想获取每个单词,测试它是否是字典单词(使用 enchant),如果是,则将其写入新文件。
这是我现在的代码:
with open('big_file_of_words', 'r') as in_file:
with open('output_file', 'w') as out_file:
words = in_file.read().split(' ')
for word in word:
if d.check(word) == True:
out_file.write("%s " % word)
我看了lazy method for reading big file in python ,这建议使用 yield
分块读取,但我担心使用预定大小的 block 会在中间拆分单词。基本上,我希望 block 尽可能接近指定的大小,同时只在空格上分割。有什么建议吗?
最佳答案
将一个 block 的最后一个单词与下一个 block 的第一个单词组合:
def read_words(filename):
last = ""
with open(filename) as inp:
while True:
buf = inp.read(10240)
if not buf:
break
words = (last+buf).split()
last = words.pop()
for word in words:
yield word
yield last
with open('output.txt') as output:
for word in read_words('input.txt'):
if check(word):
output.write("%s " % word)
关于python - 在 Python 中逐字读取一个非常大的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25372335/
我正在尝试返回数组“seats”,它本质上应该从文本文件返回数据 - 15x30 网格“#”。我已经尝试了很多东西,但我感到很沮丧,因为我对 java 的经验很少。我的代码可以编译,但在调用该方法时无
对于任何输入的字符串,我们需要按任意顺序的单词匹配来查找 super 字符串。即输入字符串中的所有单词必须以任何顺序出现在输出字符串中。例如给定数据集:“字符串搜索”“Java 字符串搜索”“手动 C
我有一个文本文件,其中包含一些我想放入二维数组中的内容。该文本文件由等长的句子组成。如何将每个单词放入数组? 文本文件的例子是- This is stackoverflow I am user 这个文
我正在编写一个实用程序,它接受一个 .resx 文件并创建一个包含 .resx 文件中所有名称/值对属性的 javascript 对象。这一切都很好,直到 .resx 中的值之一是 该经销商接受电子订
我输入了大量的数学表达式和方程式,我想为每个表达式和方程式打印出 latex 表示形式。到目前为止,我已经尝试过 Sage 和 sympy,但棘手的部分是不对表达式中的术语重新排序。 所以,如果我的输
我正在尝试通过实现异步任务在 android (java) 中流式传输 chatgpt api 的响应(逐字),但我收到错误。我正在将 java 的 HTTPurlconnection 库与输入和输出
我正在尝试通过实现异步任务在 android (java) 中流式传输 chatgpt api 的响应(逐字),但我收到错误。我正在将 java 的 HTTPurlconnection 库与输入和输出
我是一名优秀的程序员,十分优秀!