如何使用python将大型txt文件按行拆分为小型txt文件
我有一个大的txt文件包含100万行,我想把它们分割成小的txt文件,每个文件包含10行,如何使用python呢? 我发现了一些相关问题,代码如下:如何使用python将大型txt文件按行拆分为小型txt文件,python,text,split,Python,Text,Split,我有一个大的txt文件包含100万行,我想把它们分割成小的txt文件,每个文件包含10行,如何使用python呢? 我发现了一些相关问题,代码如下: def split_file(filepath, lines=30): """Split a file based on a number of lines.""" path, filename = os.path.split(filepath) # filename.split('.') would not work
def split_file(filepath, lines=30):
"""Split a file based on a number of lines."""
path, filename = os.path.split(filepath)
# filename.split('.') would not work for filenames with more than one .
basename, ext = os.path.splitext(filename)
# open input file
with open(filepath, 'r') as f_in:
try:
# open the first output file
f_out = open(os.path.join(path, '{}_{}{}'.format(basename, 0, ext)), 'w')
# loop over all lines in the input file, and number them
for i, line in enumerate(f_in):
# every time the current line number can be divided by the
# wanted number of lines, close the output file and open a
# new one
if i % lines == 0:
f_out.close()
f_out = open(os.path.join(path, '{}_{}{}'.format(basename, i, ext)), 'w')
# write the line to the output file
f_out.write(line)
finally:
# close the last output file
f_out.close()
但是,它只在小的txt文件中起作用,但在我的目标文件中不起作用,并且没有错误信息,我不知道为什么。这应该可以工作。这有点迂回,但应该避免您的神秘错误,同时使人可读 首先,让我们定义两个有用的函数。第一种方法读取一个文件并使每一行成为一个列表元素,第二种方法将列表作为文件写入 注意,如果不存在具有该名称的文件,则第二个函数将创建一个新文件,如果存在,则覆盖该文件
def line_reader(target_file):
with open(target_file, 'r') as file:
store = file.readlines()
return store
def line_writer(file_name, store):
with open(file_name, 'w') as file:
file.writelines(store)
接下来,让我们定义一个函数,它将实际将文件分解为更小的文件
def breakdown(target, new_file_name, chunk_length = 10):
# First let's store a list representing the data from the original file
data = line_reader(target)
# part_no is solely for naming purposes
part_no = 0
# this list will be used to hold smaller chunks of lines
tmp_list = []
condition = True
while condition:
for i in range(chunk_length):
# just a basic check to make sure that there are still lines left to be replaced
if len(data) > 0:
tmp_list.append(data.pop(0))
else:
condition = False
tmp_list.append('\n')
break
part_no += 1
line_writer(str(new_file_name + ' ' + str(part_no)), tmp_list)
tmp_list = []
调用breakdown会将目标分割成更小的文件,文件长度为
chunk\u
行(默认为10行),最后是一个空行。最后一个文件将是原始文件中剩下的任何文件。看起来您希望我们为您编写一些代码。虽然许多用户愿意为陷入困境的程序员编写代码,但他们通常只在海报已经试图自己解决问题时才提供帮助。演示这项工作的一个好方法是包括您迄今为止编写的代码、示例输入(如果有)、预期输出和实际获得的输出(输出、回溯等)。你提供的细节越多,你可能得到的答案就越多。检查和。到目前为止,您尝试了什么?任务的哪一部分您遇到了问题?我已经更新了Thanks。当命令行实用程序已经存在来做同样的事情时,您为什么要在python中这样做?它太大了,命令行分割实用程序不能很好地解决问题。作品