Python 如何将单个文件中的特定列重复替换为另一个文件';s列?
我是python编码新手,我有两个文本文件需要处理。有人知道如何将一个文件的特定列替换为另一个文件列吗?那么比如说,, 我想取“test1.txt”的最后四列Python 如何将单个文件中的特定列重复替换为另一个文件';s列?,python,file,text,multiple-columns,edit,Python,File,Text,Multiple Columns,Edit,我是python编码新手,我有两个文本文件需要处理。有人知道如何将一个文件的特定列替换为另一个文件列吗?那么比如说,, 我想取“test1.txt”的最后四列 1 N1 -3.8340 -1.0640 2.8770 n3 1 UNL -0.696600 2 N2 -2.7490 -1.5690 2.2220 n3 1 UNL -0.278400 3 C1
1 N1 -3.8340 -1.0640 2.8770 n3 1 UNL -0.696600
2 N2 -2.7490 -1.5690 2.2220 n3 1 UNL -0.278400
3 C1 -2.3750 -0.9950 1.1200 c3 1 UNL 0.169400
4 C2 -1.2280 -1.5720 0.2740 c3 1 UNL 0.671800
并仅将第一个文本的最后四列替换为“test2.txt”——请注意,当它重复它时,从第三列到最后一列的整数将增加
1 N1 31.2480 39.4030 91.8950 N.3 1 UNL 0.000000
2 N2 32.0980 38.3940 91.5460 N.2 1 UNL 0.000000
3 C1 33.0530 38.6590 90.7070 C.2 1 UNL 0.000000
4 C2 33.9820 37.5500 90.1880 C.2 1 UNL 0.000000
5 N1 55.1040 41.1430 27.6790 N.3 2 UNL 0.000000
6 N2 53.9860 41.7250 27.1570 N.2 2 UNL 0.000000
7 C1 53.7640 41.5940 25.8850 C.2 2 UNL 0.000000
8 C2 52.5820 42.3090 25.2080 C.2 2 UNL 0.000000
最终的结果是
1 N1 31.2480 39.4030 91.8950 n3 1 UNL -0.696600
2 N2 32.0980 38.3940 91.5460 n3 1 UNL -0.278400
3 C1 33.0530 38.6590 90.7070 c3 1 UNL 0.169400
4 C2 33.9820 37.5500 90.1880 c3 1 UNL 0.671800
5 N1 55.1040 41.1430 27.6790 n3 2 UNL -0.696600
6 N2 53.9860 41.7250 27.1570 n3 2 UNL -0.278400
7 C1 53.7640 41.5940 25.8850 c3 2 UNL 0.169400
8 C2 52.5820 42.3090 25.2080 c3 2 UNL 0.671800
像这样。。。
这在python编码中是可能的吗?
这两个文件保存在两个不同的文件名中。我真的不知道这些值
来自所需结果第一列下4行。我忽略了这些,并假设这些值不应该被替换
下面的my_cycle()
函数不是为任何iterable设计的通用函数,它仅用于帮助我们使用text1.txt
。尽管可以出于其他目的对其进行修改。我尝试使用一个修改版的itertools.cycle()
在每个循环后更新一个特定值,在本例中,是test1.txt
右侧的第三列。要更好地理解itertools.cycle()
,请完成。Python文档总是很有用的
def update_targeted_column(element):
list_elem = element.split()
new_element = ' '.join(list_elem[:-3] + [str(int(list_elem[-3]) + 1)] + list_elem[-2:])
return '\n ' + new_element
def my_cycle(iterable):
"""After each iteration of all rows, my_cycle() should increment the 3rd right-most column by 1"""
saved = []
for element in iterable:
yield element
saved.append(update_targeted_column(element))
while saved:
for element in saved:
yield element
saved.append(update_targeted_column(element))
# Combining the two files into a third one
with open('f1.txt', 'r') as file_01:
with open('f2.txt', 'r') as file_02:
with open('f3.txt', 'a+') as file_03:
cycled = my_cycle(file_01.readlines())
for line_01, line_02 in zip(cycled, file_02.readlines()):
last = ' '.join(line_01.split()[-4:]) # Separating the 4 right-most columns from lines of file_01
first = ' '.join(line_02.split()[:5]) # Separating the 5 left-most columns from lines of file_02
print(first + ' ' + last) # Joining the separated columns to get expected result
# file_03.write(first + ' ' + last + '\n')
输出(在第三个文件中):
数据只是一个中间有空格的txt文件?总是相同数量的空格吗?例如,如果您可以将数据保存为
csv
文件,这将使事情变得更简单。我不清楚您想要什么。是否要将最后一列替换为第一个表中的值?是否还要替换第6列(N.3->n3)中的值?欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南。在这里申请。StackOverflow不是一种设计、编码、研究或教程服务。特别是,该应用程序还不太常见,无法提供标准格式。您必须自己编写重复代码。请注意,您所更改的只是最后一列;在test2.txt
中,从第三列到最后一列已正确递增。将重复值存储在列表中。用一个简单的计数器来测量这些线。每行的最后一列都被替换为计数器%4所指示的值。
1 N1 31.2480 39.4030 91.8950 n3 1 UNL -0.696600
2 N2 32.0980 38.3940 91.5460 n3 1 UNL -0.278400
3 C1 33.0530 38.6590 90.7070 c3 1 UNL 0.169400
4 C2 33.9820 37.5500 90.1880 c3 1 UNL 0.671800
5 N1 55.1040 41.1430 27.6790 n3 2 UNL -0.696600
6 N2 53.9860 41.7250 27.1570 n3 2 UNL -0.278400
7 C1 53.7640 41.5940 25.8850 c3 2 UNL 0.169400
8 C2 52.5820 42.3090 25.2080 c3 2 UNL 0.671800