-1

I have a text file. I want to read the file line by line, split with separator and make tuples every n separator. The example is :

"A-B-C-D 

E-F-G

Thank You!

For Helping

Stranger-seeker "

Output of above lines will look like:

(A,B,C,D)

(E,F,G\n\nThank You!\n\nFor Helping\n\nStranger,seeker)

I will be very grateful if you could help me with the solution in Python.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
tryCatch
  • 23
  • 1
  • 6

3 Answers3

0

There is a recipe for grouper in itertools. You can use it to group a list into a fixed length chuncks

>>> from itertools import zip_longest
>>> def grouper(iterable, n, fillvalue=None):
...     "Collect data into fixed-length chunks or blocks"
...     # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
...     args = [iter(iterable)] * n
...     return zip_longest(*args, fillvalue=fillvalue)
... 
>>> data = open('tmp.txt').read().split('-')
>>> list(grouper(data, 4))
[('A', 'B', 'C', 'D\nE'), ('F', 'G\nThank You!\nFor Helping\nStranger', 'seeker\n', None)]
Sunitha
  • 11,777
  • 2
  • 20
  • 23
  • thank you for trying. After 4 fields in the tuple, the new line suggests another record. I am also trying to read the file each line at a time. 'D\nE" is part of second record. – tryCatch Sep 09 '18 at 04:11
  • `list(grouper(data, 3))` would group the elements in chunks or three. So you would get `[('A', 'B', 'C'), ('D\nE', 'F', 'G\nThank You!\nFor Helping\nStranger'), ('seeker\n', None, None)]` – Sunitha Sep 09 '18 at 04:14
0

You can split the input string and then use list comprehension on a rolling slice of the list:

s = '''A-B-C-D

E-F-G

Thank You!

For Helping

Stranger-seeker'''
l = s.split('-')
print([l[i:i+3] for i in range(0, len(l), 3)])

This outputs:

[['A', 'B', 'C'], ['D\n\nE', 'F', 'G\n\nThank You!\n\nFor Helping\n\nStranger'], ['seeker']]
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

My solution reading the whole file at once is. Please help me improve it.

with open(filename) as fp:

 data = fp.read()
 my_values = data.split('-')
 my_data = []
 count = 0
 rem_values = []
 delimiter_count =  len(my_values) - 1
 for x in range(len(my_values)):
     count += 1
     if count == 4:
         split_val = my_values[x].split("\n")
         if len(split_val) == 2:
             count = 1
             my_data.append(split_val[0] )
             rem_values.append(my_data ) 
             my_data = [ split_val[1] ]
        else:
             count = 1
             my_data.append(split_val[0] )
             rem_values.append(my_data ) 
    else:
        my_data.append( my_values[x] )

print(rem_values)

tryCatch
  • 23
  • 1
  • 6