May-04-2019, 12:23 AM
(May-03-2019, 08:56 PM)Buddhism Wrote: Or am I wrong?It will not work like this when reading from a file,x and y variables will just be overwritten when read each line from file.
Therefor is it easiest to first make a list as @nilmao dos,then make changes on list.
Here an other approach.
def line_concat(lst): temp = zip(lst[::2], lst[::-2]) return [':'.join(i) for i in temp] with open('xy.txt') as f: lst = [line.strip() for line in f if not line.isspace()]Test.
>>> lst ['xxx.xxx.xx.xx', 'yyyy', 'xxx.xxx.xx.xx', 'yyyy'] >>> line_concat(lst) ['xxx.xxx.xx.xx:yyyy', 'xxx.xxx.xx.xx:yyyy'] >>> for line in line_concat(lst): ... print(line) ... xxx.xxx.xx.xx:yyyy xxx.xxx.xx.xx:yyyy