如何将大型文本文件的每两行合并到Python列表中?

时间:2019-05-21 09:32:09

标签: python python-3.x

我有一个.txt文件,该文件分为多行,但是我想将这两行中的每一行合并为列表的一行。我怎么做?

非常感谢!

我所拥有的是这样组织的:

[1 2 3 4

5 6]

[1 2 3 4

5 6 ]

而我需要的是:

[1 2 3 4 5 6]

[1 2 3 4 5 6]

5 个答案:

答案 0 :(得分:0)

您可以在此处做两件事:

1)如果文本文件是使用numpy的savetxt函数编写的,则可以简单地将numpy.loadtxt函数与适当的定界符一起使用。

2)读取字符串中的文件,并使用替换和拆分功能的组合。

file = open(filename,'r')
dataset = file.read()
dataset = dataset.replace('\n',' ').replace('] ',']\n').split('\n')
dataset = [x.replace('[','').replace(']','').split(' ') for x in dataset]

答案 1 :(得分:0)

data =[]
with open(r'<add file path here >','r') as file:
    x = file.readlines()

    for i in range(0,len(x),2):
        data.append(x[i:i+2])
new =[' '.join(i) for  i in data]
for i in range(len(new)):
    new[i]=new[i].replace('\n','')
new_file_name = r'' #give new file path here
with open(new_file_name,'w+') as file:
    for i in new:
        file.write(i+'\n')

答案 2 :(得分:0)

尝试

final_data = []
with open('file.txt') as a:
    fdata= a.readlines()
    for ln in range(0,len(fdata),2):
        final_data.append(" ".join([fdata[ln].strip('\n'), fdata[ln+1].strip('\n')]))

print (final_data)

答案 3 :(得分:0)

我觉得您可以使用正则表达式解决这种情况:

#! /usr/bin/env python2.7
import re

with open("textfilename.txt") as r:
     text_data = r.read()
independent_lists = re.findall(r"\[(.+?)\]",r ,re.DOTALL)
#now that we have got each independent_list we can next work on
#turning it into a list
final_list_of_objects = [each_string.replace("\n"," ").split() for each_string in independent_lists]
print final_list_of_objects

但是,如果您不希望它们成为列表对象,而只是希望结果在列表之间没有换行符,那么

#! /usr/bin/env python2.7
import re

with open("textfilename.txt") as r:
     text_data = r.read()
new_txt = ""
for each_char in text_data:
     if each_char == "[":
        bool_char = True
     elif each_char == "]":
        bool_char = False
     elif each_char == "\n" and bool_char:
        each_char = " "
     new_txt += each_char
new_txt = re.sub(r"\s+", " ", new_txt) # to remove multiple space lines between numbers

答案 4 :(得分:0)

with open('test.txt') as file:
    new_data =  (" ".join(line.strip() for line in file).replace('] ',']\n').split('\n')) # ['[1 2 3 4  5 6]', ' [1 2 3 4  5 6 ]']


with open('test.txt','w+') as file:
    for data in new_data:
        file.write(data+'\n')
  

line.rstrip()仅从行中删除尾随的换行符('\ n')。

     

您需要将所有已读取和剥离的行传递到''.join(),而不是   每行本身。 python中的字符串是序列,因此字符串   行中包含的内容在传递时被解释为单独的字符   属于''.join()。