Posts: 6
Threads: 2
Joined: May 2019
May-03-2019, 07:54 PM
(This post was last modified: May-03-2019, 07:55 PM by Buddhism.)
So I have code that prints out a into a file like this:
Quote:xxx.xxx.xx.xx
yyyy
#5 blank line gap
xxx.xxx.xx.xx
yyyy
#5 blank line gap
#This repeats for a while
I need to merge the 'x' and 'y' with a ':' delimiter
So end file will look like:
Quote:xxx.xxx.xx.xx:yyyy
xxx.xxx.xx.xx:yyyy
xxx.xxx.xx.xx:yyyy
xxx.xxx.xx.xx:yyyy
#etc.
Posts: 3,458
Threads: 101
Joined: Sep 2016
Posts: 3,458
Threads: 101
Joined: Sep 2016
>>> 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
Posts: 6
Threads: 2
Joined: May 2019
May-03-2019, 08:56 PM
(This post was last modified: May-03-2019, 08:57 PM by Buddhism.)
(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"
y = "yyyy"
print (x + ":" + y)
print ("\n")
Or am I wrong?
Posts: 7,324
Threads: 123
Joined: Sep 2016
(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
Posts: 6
Threads: 2
Joined: May 2019
May-04-2019, 05:21 AM
(This post was last modified: May-04-2019, 05:21 AM by Buddhism.)
(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.
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
Am I doing something wrong?
![[Image: IGFJ11i.png]](https://i.imgur.com/IGFJ11i.png)
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
Posts: 7,324
Threads: 123
Joined: Sep 2016
May-04-2019, 07:45 AM
(This post was last modified: May-04-2019, 07:46 AM by snippsat.)
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
Posts: 6
Threads: 2
Joined: May 2019
May-04-2019, 07:58 PM
(This post was last modified: May-04-2019, 07:58 PM by Buddhism.)
(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
Posts: 7,324
Threads: 123
Joined: Sep 2016
May-04-2019, 08:21 PM
(This post was last modified: May-04-2019, 08:22 PM by snippsat.)
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')
Posts: 6
Threads: 2
Joined: May 2019
(May-04-2019, 08:21 PM)snippsat Wrote: 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')
Thank you so much!
After some error on my side of files showing up blank, I got it with your help!
Cheers!
|