基于Python中的值差异将列文本文件拆分为较小的文件

基于Python中的值差异将列文本文件拆分为较小的文件,python,list,file,text,split,Python,List,File,Text,Split,我试图将一个包含3列的文本文件拆分为许多更小的单个文本文件,这是基于第一列中存在的值跳转。下面是要拆分的文件的一小部分的示例: 2457062.30520078 1.00579146 1 2457062.30588184 1.00607543 1 2457062.30656300 1.00605515 1 2457062.71112193 1.00288150 1 2457062.71180299 1.00322454 1 2457062.71248415 1.00430136 1 在第3行和第

我试图将一个包含3列的文本文件拆分为许多更小的单个文本文件,这是基于第一列中存在的值跳转。下面是要拆分的文件的一小部分的示例:

2457062.30520078 1.00579146 1

2457062.30588184 1.00607543 1

2457062.30656300 1.00605515 1

2457062.71112193 1.00288150 1

2457062.71180299 1.00322454 1

2457062.71248415 1.00430136 1


在第3行和第4行之间有一个比平常更大的跳跃。这将是分割数据和分离单独创建的文本文件的点,分别创建前三行和后三行的文本文件。跳跃在第一列中始终超过0.1的变化。我们的目标是让像本例这样的任何跳转都成为分隔文件的分割点。非常感谢您的帮助,请假定您的文件是test.txt

f=open('test.txt').read().split('\n')
for i in f:
    frst_colmn,second_colmn,thrid_colmn = i.split('')

当您读取文件时,您到底想做什么?

您可以在读取文件时检测到跳转

def reader(infile):
    number = float('-infinity')
    for line in infile:
        prev, number = number, float(line.split(' ', 1)[0])
        jump = number - prev >= 0.1
        yield jump, line

for jump, line in reader(infile):
    # jump is True if one must open a new output file
    ...

只要满足您的条件,我将循环浏览主文件并继续写行。这完全符合while循环的定义。这样做的主要复杂性在于,您需要同时打开两个文件(主文件和当前正在写入的文件),但这对Python来说不是问题

MAINTEXT = "big_file.txt"
SFILE_TEMPL = 'small_file_{:03.0g}.txt'
# Delimiter is a space in the example you gave, but 
#  might be tab (\t) or comma or anything.
DELIMITER = ' ' 

LIM = .1

# i will count how many files we have created.
i = 0

# Open the main file
with open(MAINTEXT) as mainfile:
    # Read the first line and set up some things
    line = mainfile.readline()
    # Note that we want the first element ([0]) before
    #  the delimiter (.split(DELIMITER)) of the row (line)
    #  as a number (float)
    v_cur = float(line.split(DELIMITER)[0])
    v_prev = v_cur

    # This will stop the loop once we reach end of file (EOF)
    #  as readline() will then return an empty string.
    while line:
        # Open the second file for writing (mode='w').
        with open(SFILE_TEMPL.format(i), mode='w') as subfile:
            # As long as your values are in the limit, keep 
            #  writing lines to the current file.
            while line and abs(v_prev - v_cur)<LIM:
                subfile.write(line)
                line = mainfile.readline()
                v_prev = v_cur
                v_cur = float(line.split(DELIMITER)[0])
        # Increment the file counter
        i += 1
        # Make sure we don't get stuck after one file
        #  (If we don't replace v_prev here, the while loop
        #  will never execute after the first time.)
        v_prev = v_cur
MAINTEXT=“big_file.txt”
SFILE_temp='small_文件{:03.0g}.txt'
#Delimiter是您给出的示例中的一个空格,但是
#可能是制表符(\t)或逗号或任何内容。
分隔符=“”
LIM=.1
#我会数一数我们已经创建了多少个文件。
i=0
#打开主文件
打开(MAINTEXT)作为主文件:
#阅读第一行并设置一些内容
line=mainfile.readline()
#请注意,我们希望第一个元素([0])位于
#行(行)的分隔符(.split(delimiter))
#作为数字(浮动)
v_cur=float(line.split(分隔符)[0])
v_prev=v_cur
#一旦到达文件末尾(EOF),这将停止循环
#as readline()将返回一个空字符串。
while line:
#打开第二个文件进行写入(mode='w')。
以open(文件模板格式(i),mode='w')作为子文件:
#只要您的值在限制范围内,请保持
#将行写入当前文件。

虽然line和abs(v_prev-v_cur)可以更具体一点吗?目标是根据第一列中较大的值跳跃,将一个较大的3列文本文件分隔为单独的文本文件。问题是这种跳跃似乎是随机的。感谢您的帮助,直到创建了第20个文件,直到第33行的v_cur出现值错误,无法将字符串转换为float为止,这一切都很好。知道是什么导致的吗?我想它实际上可能是文件的结尾,因为我刚刚意识到,在检查下一行是否为空之前,会发生到浮点的转换。最干净的方法可能是移动
readline()
和v_prev/v_cur赋值。或者,在v_prev和v_cur分配周围包括一行
if:
else:break
try
。在这种情况下,您还可以从上面的while语句中删除“
行和
”。