Merge 2 lines with delimiter - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Merge 2 lines with delimiter (/thread-18051.html) |
Merge 2 lines with delimiter - Buddhism - May-03-2019 So I have code that prints out a into a file like this: Quote:xxx.xxx.xx.xx I need to merge the 'x' and 'y' with a ':' delimiter So end file will look like: Quote:xxx.xxx.xx.xx:yyyy RE: Merge 2 lines with delimiter - nilamo - May-03-2019 What have you tried? RE: Merge 2 lines with delimiter - nilamo - May-03-2019 >>> text = '''xxx.xxx.xx.xx ... yyyy ... ... ... ... ... ... xxx.xxx.xx.xx ... yyyy ... ... ... ... ... ... xxx.xxx.xx.xx ... yyyy''' >>> lines = text.split('\n') >>> lines ['xxx.xxx.xx.xx', 'yyyy', '', '', '', '', '', 'xxx.xxx.xx.xx', 'yyyy', '', '', '', '', '', 'xxx.xxx.xx.xx', 'yyyy'] >>> lines = [line for line in lines if line] # remove blanks >>> lines ['xxx.xxx.xx.xx', 'yyyy', 'xxx.xxx.xx.xx', 'yyyy', 'xxx.xxx.xx.xx', 'yyyy'] >>> pairs = [(lines[i-1], lines[i]) for i in range(1, len(lines), 2)] >>> pairs [('xxx.xxx.xx.xx', 'yyyy'), ('xxx.xxx.xx.xx', 'yyyy'), ('xxx.xxx.xx.xx', 'yyyy')] >>> '\n'.join(':'.join(pair) for pair in pairs) 'xxx.xxx.xx.xx:yyyy\nxxx.xxx.xx.xx:yyyy\nxxx.xxx.xx.xx:yyyy' >>> print('\n'.join(':'.join(pair) for pair in pairs)) xxx.xxx.xx.xx:yyyy xxx.xxx.xx.xx:yyyy xxx.xxx.xx.xx:yyyy RE: Merge 2 lines with delimiter - Buddhism - May-03-2019 (May-03-2019, 08:28 PM)nilamo Wrote:>>> text = '''xxx.xxx.xx.xx ... yyyy ... ... ... ... ... ... xxx.xxx.xx.xx ... yyyy ... ... ... ... ... ... xxx.xxx.xx.xx ... yyyy''' >>> lines = text.split('\n') >>> lines ['xxx.xxx.xx.xx', 'yyyy', '', '', '', '', '', 'xxx.xxx.xx.xx', 'yyyy', '', '', '', '', '', 'xxx.xxx.xx.xx', 'yyyy'] >>> lines = [line for line in lines if line] # remove blanks >>> lines ['xxx.xxx.xx.xx', 'yyyy', 'xxx.xxx.xx.xx', 'yyyy', 'xxx.xxx.xx.xx', 'yyyy'] >>> pairs = [(lines[i-1], lines[i]) for i in range(1, len(lines), 2)] >>> pairs [('xxx.xxx.xx.xx', 'yyyy'), ('xxx.xxx.xx.xx', 'yyyy'), ('xxx.xxx.xx.xx', 'yyyy')] >>> '\n'.join(':'.join(pair) for pair in pairs) 'xxx.xxx.xx.xx:yyyy\nxxx.xxx.xx.xx:yyyy\nxxx.xxx.xx.xx:yyyy' >>> print('\n'.join(':'.join(pair) for pair in pairs)) xxx.xxx.xx.xx:yyyy xxx.xxx.xx.xx:yyyy xxx.xxx.xx.xx:yyyy This looks very good, but I is from a text file which includes the text. otherwise I could just do Quote:x = "xxx.xxx.xx.xx" Or am I wrong? RE: Merge 2 lines with delimiter - snippsat - May-04-2019 (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 RE: Merge 2 lines with delimiter - Buddhism - May-04-2019 (May-04-2019, 12:23 AM)snippsat Wrote:(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. Am I doing something wrong? You are seeing the last of the code. Those last lines are whats running. Everything else is def The file that the program is working with the whole time is ScrapedProxys.txt KEEP IN MIND: list_concat(lst) is above this image to run the def But this is the error RE: Merge 2 lines with delimiter - snippsat - May-04-2019 Try to use code tags and not images with code,look at BBCode. In my example i am running the last part in interactive shell has >>> .Now you have put all in one function and that will not work as i have written it. Here a example running as a script,see that need print() when running as a script.def line_concat(lst): temp = zip(lst[::2], lst[::-2]) return [':'.join(i) for i in temp] def file_read(file_in): with open(file_in) as f: lst = [line.strip() for line in f if not line.isspace()] return lst if __name__ == '__main__': file_in = 'xy.txt' lst = file_read(file_in) print(lst) # Test file read for line in line_concat(lst): print(line)
xy.txt that i use to test with is:
RE: Merge 2 lines with delimiter - Buddhism - May-04-2019 (May-04-2019, 07:45 AM)snippsat Wrote: Try to use code tags and not images with code,look at BBCode. Here is what I've got. The file, when I open it, is unchanged from before I added the code. Thank you for the help so far, this stuff can just get confusing Heres the code now: Quote:def line_concat(lst): temp = zip(lst[::2], lst[::-2]) return [':'.join(i) for i in temp] def file_read(file_in): with open(file_in) as f: lst = [line.strip() for line in f if not line.isspace()] return lst if __name__ == '__main__': with open("ScrapedProxys.txt","w+") as t: t.close() main() file_in = "ScrapedProxys.txt" lst = file_read(file_in) for line in line_concat(lst): f.write(line) Clear() input("Press Enter to Exit") The Quote:with open("ScrapedProxys.txt","w+") as t:is to create the file on start RE: Merge 2 lines with delimiter - snippsat - May-04-2019 You use close() that's not needed when use with open and there is no write() .Here how it look if write result to file. def line_concat(lst): temp = zip(lst[::2], lst[::-2]) return [':'.join(i) for i in temp] def file_read(file_in): with open(file_in) as f: lst = [line.strip() for line in f if not line.isspace()] return lst if __name__ == '__main__': file_in = 'xy.txt' lst = file_read(file_in) #print(lst) # Test file read with open('xy_new.txt', 'w') as f_out: for line in line_concat(lst): f_out.write(f'{line}\n') RE: Merge 2 lines with delimiter - Buddhism - May-05-2019 (May-04-2019, 08:21 PM)snippsat Wrote: You use Thank you so much! After some error on my side of files showing up blank, I got it with your help! Cheers! |