从由换行符

时间:2017-04-24 03:14:04

标签: python python-3.x

如果我有一个包含多行代码的文件:

name1  
12345  
67890

name2  
23456  
78901

name3  
34567  
89012

我如何在表格中连接每一行:

"name1 1234567890"

"name2 2345678901"

"name3 3456789012"

具体来说,在遇到空行之前,将文件中的字符串连接成一行的最佳方法是什么?

4 个答案:

答案 0 :(得分:1)

您可以先使用\n\n拆分,然后拆分\n以获取每个项目。

data = open('file_name').read()

output = ["%s %s%s" % tuple(item.split('\n')) for item in data.split('\n\n')]
  

[' name1 1234567890',' name2 2345678901',' name3 3456789012']

答案 1 :(得分:0)

假设您的输入文件名为df %>% rename_if(grepl("mean", names(.)), funs(sprintf("%s_expr", .))) 且输出文件名为file_input,您可以执行以下操作:

file_output

输出:

# Read your input file, removing \n and spliting by spaces
with open("input_file", 'r') as f:
    data = f.read().rstrip().split()

# Opening a new file in append mode 'a' 
with open("output_file", 'a') as f:
    # Processing your data by iterating through the data list with steps
    n = ['"{0} {1}"'.format(k, "".join(v)) for k,*v in zip(data[::3], data[1::3], data[2::3])]
    for k in n:
        f.write(k+"\n")

答案 2 :(得分:0)

这是一种不会立即将整个文件读入内存的方法。我也不假设每组只有3行。

>>> from itertools import groupby
>>> with open("fin") as fin:
...     groups = groupby(fin, str.isspace)
...     for name, *rest in (map(str.strip, v) for  g,v in groups if not g):
...         print(name, " ", *rest, sep="")
... 
name1 1234567890
name2 2345678901
name3 3456789012

答案 3 :(得分:0)

只需split即可完成。

for x in open('the_file', 'r').read().split('\n\n'): # split the file on empty lines
    m = x.split() # split on line break
    print '"{} {}{}"\n'.format(m[0], m[1], m[2])

<强>输出

"name1 1234567890"

"name2 2345678901"

"name3 3456789012"