import pandas as pd
def process_doc(txt_path1, txt_path2):
file1 = pd.read_csv(txt_path1, sep='\s+', header=None)
file2 = pd.read_csv(txt_path2, sep='\s+', header=None)
for index1, row1 in file1.iterrows():
for index2, row2 in file2.iterrows():
if row1[0] == row2[11]:
file2.at[index2, 'total_length'] = row1.iloc[1]
file2.to_csv("processed.txt", sep='\t', index=False, header=False)
return file2
def read_csv_file(filename,index_out,index):
df = pd.read_csv(filename, sep='\s+', header=None)
data = df.iloc[index_out:index + 1, :2].values.tolist()
return data
def total_length_with_overlap(segments):
total = 0
segments.sort()
prev_end = float('-inf')
for seg in segments:
start, end = seg
if start <= prev_end:
total += max(0, end - prev_end)
else:
total += end - start
prev_end = max(prev_end, end)
return total
def calculate(filename,index_out,index):
data = read_csv_file(filename,index_out,index)
total_all = total_length_with_overlap(data)
return total_all
def count(data):
total_sum = 0
index_out = 0
total_sum_first = 0
tag_line = 11
length_line = 6
for index, row in data.iterrows():
"""if index == 0:
total_sum = calculate(filename,index_out,index) #data.iloc[index, length_line]
data.at[index, 'total_sum'] = total_sum
pass
elif 0 < index < len(data) - 1 and row[tag_line] == data.iloc[index - 1, tag_line]:
pass
total_sum = total_sum + data.iloc[index, length_line] # 累加第七列内容
data.at[index, 'total_sum'] = total_sum"""
if 0 <= index < len(data) - 1 and not row[tag_line] == data.iloc[index + 1, tag_line]:
data.at[index, 'total_sum'] = calculate(filename, index_out, index)
print("{}:{}:{}".format(data.iloc[index, 11], data.iloc[index, 12],
int(data.iloc[index, 15]) / int(data.iloc[index, 14])))
index_out = index + 1
elif index == len(data) - 1:
data.at[index, 'total_sum'] = calculate(filename, index_out, index)
print("{}:{}:{}".format(data.iloc[index, 11], data.iloc[index, 12],
int(data.iloc[index, 15]) / int(data.iloc[index, 14])))
data.to_csv("finally.txt", sep='\t', index=False, header=False)
if __name__=='__main__':
filename = "processed.txt"
data = process_doc("test.txt", "t.txt")
count(data)