[Tutor] print issue
ose micah
osaosemwe at yahoo.com
Thu Oct 3 17:02:54 EDT 2019
Hello Alan,
I am here again. I checked the code it pans out, w will write over the previous code while a will append new lines... well it depends on the final usage.
I am trying to copy only subnets from a website to the screen
#!/usr/bin/env pythonimport requestsimport sysimport fileinput
ip_ranges = requests.get('http://d7uri8nf7uskq.cloudfront.net/tools/list-cloudfront-ips').json()cloudfront_ips = [item['ip_prefix'] for item in ip_ranges if item == "CLOUDFRONT_GLOBAL_IP_LIST"]ec2_ips = [item['ip_prefix'] for item in ip_ranges if item == "CLOUDFRONT_REGIONAL_EDGE_IP_LIST"]
cloudfront_ips_more_edge=[]
for ip in cloudfront_ips: if ip not in ec2_ips: cloudfront_ips_more_edge.append(ip)
for ip in ec2_ips: if ip not in cloudfront_ips: cloudfront_ips_more_edge.append(ip)
for ip in cloudfront_ips_more_edge: print(str(ip))
such that output would be like
52.94.22.0/2452.94.17.0/2452.95.154.0/2352.95.212.0/2254.239.0.240/2854.239.54.0/2352.119.224.0/21....
I dont know why I am not able to get the code working.
please can you help out. Thanks.
Ose M.
On Wednesday, October 2, 2019, 06:35:16 PM EDT, Alan Gauld <alan.gauld at btinternet.com> wrote:
On 02/10/2019 19:43, ose micah wrote:
>
> Hello Alan,
> Thank you A lot. You are a time saver. it works better with "a" as the
> function or a is to append. "w" overwrites the previous line.
>
Only if you have the open() inside the loop.
In my second post I moved the second open outside the for loop so the
writes all go into the same file in order.
The problem with append is that if you rerun the script without
first deleting the file you will wind up creating two copies
of the data inside. The second appended after the first!
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list