Reputation: 21
If I have a file with multiple lines like:
name1
12345
67890
name2
23456
78901
name3
34567
89012
How would I go about concatenating each line in the form:
"name1 1234567890"
"name2 2345678901"
"name3 3456789012"
Specifically, what is the best way to concatenate strings from a file into a single line until an empty line is encountered?
Upvotes: 2
Views: 2277
Reputation: 304137
Here's a way that doesn't read the entire file into memory at once. I also don't assume that there are only 3 lines in each group.
>>> 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
Upvotes: 0
Reputation: 98881
It can be done with just 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])
Output:
"name1 1234567890"
"name2 2345678901"
"name3 3456789012"
Upvotes: 0
Reputation: 9257
Assuming your input file is called file_input
and your output file is called file_output
, you can do something like this:
# 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")
Output:
"name1 1234567890"
"name2 2345678901"
"name3 3456789012"
Upvotes: 0
Reputation: 22885
You can split first with \n\n
and then split \n
to get each item.
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']
Upvotes: 1