Python Forum
Merge 2 lines with delimiter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Merge 2 lines with delimiter
#8
(May-04-2019, 07:45 AM)snippsat Wrote: 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) 
Output:
['xxx.xxx.xx.xx', 'yyyy', 'xxx.xxx.xx.xx', 'yyyy'] xxx.xxx.xx.xx:yyyy xxx.xxx.xx.xx:yyyy
xy.txt that i use to test with is:
Output:
xxx.xxx.xx.xx yyyy xxx.xxx.xx.xx yyyy

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
Reply


Messages In This Thread
Merge 2 lines with delimiter - by Buddhism - May-03-2019, 07:54 PM
RE: Merge 2 lines with delimiter - by nilamo - May-03-2019, 08:18 PM
RE: Merge 2 lines with delimiter - by nilamo - May-03-2019, 08:28 PM
RE: Merge 2 lines with delimiter - by Buddhism - May-03-2019, 08:56 PM
RE: Merge 2 lines with delimiter - by snippsat - May-04-2019, 12:23 AM
RE: Merge 2 lines with delimiter - by Buddhism - May-04-2019, 05:21 AM
RE: Merge 2 lines with delimiter - by snippsat - May-04-2019, 07:45 AM
RE: Merge 2 lines with delimiter - by Buddhism - May-04-2019, 07:58 PM
RE: Merge 2 lines with delimiter - by snippsat - May-04-2019, 08:21 PM
RE: Merge 2 lines with delimiter - by Buddhism - May-05-2019, 02:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,552 Nov-09-2023, 10:56 AM
Last Post: mg24
  Context-sensitive delimiter ZZTurn 9 1,561 May-16-2023, 07:31 AM
Last Post: Gribouillis
  Read csv file with inconsistent delimiter gracenz 2 1,227 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Delimiter issue with a CSV file jehoshua 1 1,338 Apr-19-2022, 01:28 AM
Last Post: jehoshua
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 2,892 Dec-15-2020, 05:21 PM
Last Post: snippsat
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 6,000 Aug-10-2020, 11:01 PM
Last Post: medatib531
  copy content of text file with three delimiter into excel sheet vinaykumar 0 2,383 Jul-12-2020, 01:27 PM
Last Post: vinaykumar
  How to print string multiple times separated by delimiter Mekala 1 1,942 May-23-2020, 09:21 AM
Last Post: Yoriz
  How to split string after delimiter SriRajesh 1 2,140 Sep-11-2019, 11:25 AM
Last Post: metulburr
  How to split at specified delimiter SriRajesh 5 3,128 Dec-27-2018, 03:43 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020