Python - 打开和更改大文本文件
Python - Opening and changing large text files
我有一个 ~600MB Roblox 类型的 .mesh 文件,在任何文本编辑器中读起来都像文本文件。我有以下代码:
mesh = open("file.mesh", "r").read()
mesh = mesh.replace("[", "{").replace("]", "}").replace("}{", "},{")
mesh = "{"+mesh+"}"
f = open("p2t.txt", "w")
f.write(mesh)
它returns:
Traceback (most recent call last):
File "C:\TheDirectoryToMyFile\p2t2.py", line 2, in <module>
mesh = mesh.replace("[", "{").replace("]", "}").replace("}{", "},{")
MemoryError
这是我的文件示例:
[-0.00599, 0.001466, 0.006][0.16903, 0.84515, 0.50709][0.00000, 0.00000, 0][-0.00598, 0.001472, 0.00599][0.09943, 0.79220, 0.60211][0.00000, 0.00000, 0]
我能做什么?
编辑:
我不确定将此标记为重复的其他线程中的 head、follow 和 tail 命令是什么。我尝试使用它,但无法正常工作。该文件也是一大行,没有拆分成行。
你可以一行一行地做:
mesh = open("file.mesh", "r")
with open("p2t.txt", "w") as f:
for line in mesh:
line= line.replace("[", "{").replace("]", "}").replace("}{", "},{")
line = "{"+line +"}"
f.write(line)
import os
f = open('p2f.txt','w')
with open("file.mesh") as mesh:
while True:
c = mesh.read(1)
if not c:
f.seek(-1,os.SEEK_END)
f.truncate()
break
elif c == '[':
f.write('{')
elif c == ']':
f.write('},')
else:
f.write(c)
p2f.txt
:
{-0.00599, 0.001466, 0.006},{0.16903, 0.84515, 0.50709},{0.00000, 0.00000, 0},{-0.00598, 0.001472, 0.00599},{0.09943, 0.79220, 0.60211},{0.00000, 0.00000, 0}
您需要每次迭代读取一口,对其进行分析,然后写入另一个文件或 sys.stdout
。试试这个代码:
mesh = open("file.mesh", "r")
mesh_out = open("file-1.mesh", "w")
c = mesh.read(1)
if c:
mesh_out.write("{")
else:
exit(0)
while True:
c = mesh.read(1)
if c == "":
break
if c == "[":
mesh_out.write(",{")
elif c == "]":
mesh_out.write("}")
else:
mesh_out.write©
更新:
它运行起来真的很慢(感谢 jamylak)。所以我改变了它:
import sys
import re
def process_char(c, stream, is_first=False):
if c == '':
return False
if c == '[':
stream.write('{' if is_first else ',{')
return True
if c == ']':
stream.write('}')
return True
def process_file(fname):
with open(fname, "r") as mesh:
c = mesh.read(1)
if c == '':
return
sys.stdout.write('{')
while True:
c = mesh.read(8192)
if c == '':
return
c = re.sub(r'\[', ',{', c)
c = re.sub(r'\]', '}', c)
sys.stdout.write(c)
if __name__ == '__main__':
process_file(sys.argv[1])
所以现在它在 1.4G 文件上运行约 15 秒。给运行吧:
$ python mesh.py file.mesh > file-1.mesh
def read(afilename):
with open("afilename", "r") as file
lines = file.readlines()
lines.replace("[", "{")
#place reset of code here in
BLOCK_SIZE = 1 << 15
with open(input_file, 'rb') as fin, open(output_file, 'wb') as fout:
for block in iter(lambda: fin.read(BLOCK_SIZE), b''):
# do your replace
fout.write(block)
我有一个 ~600MB Roblox 类型的 .mesh 文件,在任何文本编辑器中读起来都像文本文件。我有以下代码:
mesh = open("file.mesh", "r").read()
mesh = mesh.replace("[", "{").replace("]", "}").replace("}{", "},{")
mesh = "{"+mesh+"}"
f = open("p2t.txt", "w")
f.write(mesh)
它returns:
Traceback (most recent call last):
File "C:\TheDirectoryToMyFile\p2t2.py", line 2, in <module>
mesh = mesh.replace("[", "{").replace("]", "}").replace("}{", "},{")
MemoryError
这是我的文件示例:
[-0.00599, 0.001466, 0.006][0.16903, 0.84515, 0.50709][0.00000, 0.00000, 0][-0.00598, 0.001472, 0.00599][0.09943, 0.79220, 0.60211][0.00000, 0.00000, 0]
我能做什么?
编辑:
我不确定将此标记为重复的其他线程中的 head、follow 和 tail 命令是什么。我尝试使用它,但无法正常工作。该文件也是一大行,没有拆分成行。
你可以一行一行地做:
mesh = open("file.mesh", "r")
with open("p2t.txt", "w") as f:
for line in mesh:
line= line.replace("[", "{").replace("]", "}").replace("}{", "},{")
line = "{"+line +"}"
f.write(line)
import os
f = open('p2f.txt','w')
with open("file.mesh") as mesh:
while True:
c = mesh.read(1)
if not c:
f.seek(-1,os.SEEK_END)
f.truncate()
break
elif c == '[':
f.write('{')
elif c == ']':
f.write('},')
else:
f.write(c)
p2f.txt
:
{-0.00599, 0.001466, 0.006},{0.16903, 0.84515, 0.50709},{0.00000, 0.00000, 0},{-0.00598, 0.001472, 0.00599},{0.09943, 0.79220, 0.60211},{0.00000, 0.00000, 0}
您需要每次迭代读取一口,对其进行分析,然后写入另一个文件或 sys.stdout
。试试这个代码:
mesh = open("file.mesh", "r")
mesh_out = open("file-1.mesh", "w")
c = mesh.read(1)
if c:
mesh_out.write("{")
else:
exit(0)
while True:
c = mesh.read(1)
if c == "":
break
if c == "[":
mesh_out.write(",{")
elif c == "]":
mesh_out.write("}")
else:
mesh_out.write©
更新:
它运行起来真的很慢(感谢 jamylak)。所以我改变了它:
import sys
import re
def process_char(c, stream, is_first=False):
if c == '':
return False
if c == '[':
stream.write('{' if is_first else ',{')
return True
if c == ']':
stream.write('}')
return True
def process_file(fname):
with open(fname, "r") as mesh:
c = mesh.read(1)
if c == '':
return
sys.stdout.write('{')
while True:
c = mesh.read(8192)
if c == '':
return
c = re.sub(r'\[', ',{', c)
c = re.sub(r'\]', '}', c)
sys.stdout.write(c)
if __name__ == '__main__':
process_file(sys.argv[1])
所以现在它在 1.4G 文件上运行约 15 秒。给运行吧:
$ python mesh.py file.mesh > file-1.mesh
def read(afilename):
with open("afilename", "r") as file
lines = file.readlines()
lines.replace("[", "{")
#place reset of code here in
BLOCK_SIZE = 1 << 15
with open(input_file, 'rb') as fin, open(output_file, 'wb') as fout:
for block in iter(lambda: fin.read(BLOCK_SIZE), b''):
# do your replace
fout.write(block)