[英]Concatenate text file lines with condition in python
I have a text file in this format:我有一个这种格式的文本文件:
0.jpg 12,13,14,15,16
0.jpg 13,14,15,16,17
1.jpg 1,2,3,4,5
1.jpg 2,3,4,5,6
I want to check if the image name is the same and then concatenate those lines into one line with the following format:我想检查图像名称是否相同,然后将这些行连接成一行,格式如下:
0.jpg 12,13,14,15,16 13,14,15,16,17
1.jpg 1,2,3,4,5 2,3,4,5,6
I have tried something like this but don't know how to do the actual comparison and also don't quite know what logic to apply since the first line_elements[0]
will be taken and compared with each other line's line_elements[0]
我试过这样的事情,但不知道如何进行实际比较,也不太清楚应用什么逻辑,因为将采用第一
line_elements[0]
并将其与其他行的line_elements[0]
进行比较
with open("file.txt", "r") as input: # Read all data lines.
data = input.readlines()
with open("out_file.txt", "w") as output: # Create output file.
for line in data: # Iterate over data lines.
line_elements = line.split() # Split line by spaces.
line_updated = [line_elements[0]] # Initialize fixed line (without undesired patterns) with image's name.
if line_elements[0] = (next line's line_elements[0])???:
for i in line_elements[1:]: # Iterate over groups of numbers in current line.
tmp = i.split(',') # Split current group by commas.
if len(tmp) == 5:
line_updated.append(','.join(tmp))
if len(line_updated) > 1: # If the fixed line is valid, write it to output file.
output.write(f"{' '.join(line_updated)}\n")
Could be something like:可能是这样的:
for i in range (len(data)):
if line_elements[0] in line[i] == line_elements[0] in line[i+1]:
line_updated = [line_elements[0]]
for i in line_elements[1:]: # Iterate over groups of numbers in current line.
tmp = i.split(',') # Split current group by commas.
if len(tmp) == 5:
line_updated.append(','.join(tmp))
if len(line_updated) > 1: # If the fixed line is valid, write it to output file.
output.write(f"{' '.join(line_updated)}\n")
Save the first field of the line in a variable.将行的第一个字段保存在变量中。 Then check if the first field of the current line is equal to the value.
然后检查当前行的第一个字段是否等于该值。 If it is, append to the value, otherwise write the saved line and start a new output line.
如果是,则将 append 写入该值,否则写入保存的行并开始新的 output 行。
current_name = None
with open("out_file.txt", "w") as output:
for line in data:
name, values = line.split()
if name == current_name:
current_values += ' ' + values
continue
if current_name:
output.write(f'{current_name} {current_values}\n')
current_name, current_values = name, values
# write the last block
if current_name:
output.write(f'{current_name} {current_values}\n')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.