[Tutor] Formatting text file with python
Hello friends, I want to format the log (text) file my email's server. The text file (named s1.txt) contains the following information, text file has about 3000 lines. "hMailServer SpamProtection rejected RCPT (Sender: valeria0...@mikelsonconstruction.com, IP:187.62.63.218, Reason: Rejected by Spamhaus.)" "hMailServer SpamProtection rejected RCPT (Sender: veronika07...@etb.net.co, IP:190.25.189.74, Reason: Rejected by SpamCop.)" "hMailServer SpamProtection rejected RCPT (Sender: sofia...@pembroketownhouse.ie, IP:103.247.48.95, Reason: Rejected by Spamhaus.)" I want to select ip addresses in text file and I want to delete duplicated records. Finally I want to write this data into a new file. What do you suggest me? That codes returned me about 500 records and gives error ; Traceback (most recent call last): File "C:/Depo/Python/prj1/analiz.py", line 17, in prnt(deger) File "C:/Depo/Python/prj1/analiz.py", line 7, in prnt iplist = list_line[1] IndexError: list index out of range) --- My code is below; def prnt(L1): L1 = L1[:-1] list_line = L1.split(",") list0 = list_line[0] iplist = list_line[1] list2 = list_line[2] print(iplist) with open("s1.txt","r",) as file: for deger in file: prnt(deger) #with open("iplistesi.txt","w") as file2: #file2.write(i) Best Regards. Huseyin ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Suggest some Android app
Hi, please suggest me some good android apps through which i can execute my python scripts and practice python. I want to download videos using youtube-dl python script and want to save them to external drive like otg pendrive. I tried termux, which is not able to store the files in external storage. I want to directly download videos to external otg pendrive. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Formatting text file with python
On 06/02/18 21:07, Hüseyin Ertuğrul wrote: > "hMailServer SpamProtection rejected RCPT (Sender: > valeria0...@mikelsonconstruction.com, IP:187.62.63.218, Reason: Rejected by > Spamhaus.)" > "hMailServer SpamProtection rejected RCPT (Sender: veronika07...@etb.net.co, > IP:190.25.189.74, Reason: Rejected by SpamCop.)" > "hMailServer SpamProtection rejected RCPT (Sender: > sofia...@pembroketownhouse.ie, IP:103.247.48.95, Reason: Rejected by > Spamhaus.)" > > Traceback (most recent call last): > File "C:/Depo/Python/prj1/analiz.py", line 17, in > prnt(deger) > File "C:/Depo/Python/prj1/analiz.py", line 7, in prnt > iplist = list_line[1] > IndexError: list index out of range) So you have a line with no commas in it. Have you checked the line that causes the error - presumably right after the last one that printed correctly. > --- > My code is below; > def prnt(L1): > > L1 = L1[:-1] > > list_line = L1.split(",") You are splitting by comma but I notice the IP addresses all start with IP: so it might be easier/more reliable to split by 'IP:' in case there are other messages with commas but no IP addresses in them? But even so you still need to either check the result length or use a try/except IndexError to deal with malformed lines. -- 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] can someone explain the reason for error
Hi, I am a beginner level programmer and in one assignment the question given is:to remove ',' from a list after getting a comma separated input from console. I gave the below (most print statements are for reference except the last print statement). but i get the attached error. can someone explain why this error nd how to rectify? inputdata = input ('Enter comma separated data \n') type(inputdata) inputlist = list(inputdata) print(inputlist) a = len(inputdata) print(a) print ('') for b in range(0,a): print(a) a = a - 1 print(inputlist) inputlist.remove(',') print(inputlist) Thanks, Vinod ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] can someone explain the reason for error
On 07/02/18 11:58, vinod bhaskaran wrote: > Hi, I am a beginner level programmer and in one assignment the question > given is:to remove ',' from a list after getting a comma separated input > from console. First up I'll say you are doing an awful lot of work that's not needed. As a result your solution is much more complex than is necessary. Remember that a string is just like a list of letters so you don't need to convert it to a list. Just use it as is. Also strings have lots of useful methods that you can use - hint: try reading about them in the Python documentation, you might find easier ways to do things. > I gave the below (most print statements are for reference except the last > print statement). but i get the attached error. can someone explain why > this error nd how to rectify? > > inputdata = input ('Enter comma separated data \n') > type(inputdata) This line does nothing useful. > inputlist = list(inputdata) > print(inputlist) > a = len(inputdata) > print(a) Note that you converted to a list but you are taking the length of the original string. > print ('') > for b in range(0,a): > print(a) > a = a - 1 This is a terrible idea. Don't mess with the values to give to range in a loop. Bad things are likely to happen. Also note that Pythons for loops give you each item in the data, you don;t need to mess about with indexes. And if you do need indexes enumerate() is usually a better solution than using range. (range is great if you just want a list of numbers but its usually the wrong way to process a collection of any kind) However in this case you are not using a or b, just reducing the size of a in each iteration, while increasing the size of b such that they will meet in the middle. I'm pretty sure that's not what you want? > print(inputlist) > inputlist.remove(',') This removes the commas starting at the front of the list. So you remove 1 comma each time round the loop. But you go round the loop half as many times as there are characters in the string. What happens if there are less commas than that? You don't show us the error, (if you used an attachment it will have been stripped by the server). But if it was a ValueError then I suspect you hit the above scenario. > print(inputlist) Let me suggest a simpler algorithm in pseudo code. (WE don;t do your homework for you so you will need to translate the pseudocode into real Python yourself) inputstr = input() for char in inputstr: if char not a comma print char There is an even shorter way if you use the built in string methods. -- 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] can someone explain the reason for error
On 07/02/18 11:58, vinod bhaskaran wrote: Hi, I am a beginner level programmer and in one assignment the question given is:to remove ',' from a list after getting a comma separated input from console. I gave the below (most print statements are for reference except the last print statement). but i get the attached error. can someone explain why this error nd how to rectify? inputdata = input ('Enter comma separated data \n') type(inputdata) inputlist = list(inputdata) print(inputlist) a = len(inputdata) print(a) print ('') for b in range(0,a): print(a) a = a - 1 print(inputlist) inputlist.remove(',') print(inputlist) Thanks, Vinod The attachment doesn't get through to this text only list so please cut and paste the entire traceback into a reply, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] can someone explain the reason for error
On 2018-02-07 03:58, vinod bhaskaran wrote: Hi, I am a beginner level programmer and in one assignment the question given is:to remove ',' from a list after getting a comma separated input from console. I gave the below (most print statements are for reference except the last print statement). but i get the attached error. can someone explain why this error nd how to rectify? inputdata = input ('Enter comma separated data \n') type(inputdata) inputlist = list(inputdata) print(inputlist) a = len(inputdata) print(a) print ('') for b in range(0,a): print(a) a = a - 1 print(inputlist) inputlist.remove(',') print(inputlist) The following might help you on your way: Python 3.6.3 (default, Oct 6 2017, 08:44:35) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. s = "a,b,c,d" l = list(s) l ['a', ',', 'b', ',', 'c', ',', 'd'] l2 = s.split() l2 ['a,b,c,d'] l3 = s.split(',') l3 ['a', 'b', 'c', 'd'] When 'list()' is handed a string, it returns a list of all characters in the string; in your case many of them are commas. The str method 'split' takes a string and splits it into an array: by default it uses white space as the delimiter (your string has no white space so it is returned as the only member of the list) but this (the delimiter) can be set to what you want. In your case you want to split on the comma character. For "extra bonus points" you might want to look at the csv (comma separated values) module- it might be helpful depending on your use case. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Java equivalent of Python-Tutor?
In my early days of using Python I benefited greatly from this Tutor list, thanks to both Alan and Steven as well as as many contributors. I still check in now and then and try to chime in to help now that I have a bit more experience under my belt. I'm doing a few projects in Java now and would love to find a similar resource that covers that language, and the Eclipse IDE. Some of my questions are too newbie for a forum like stackoverflow (and most of the responses there assume a non-newbie level of knowledge). Any suggestions? (I acknowledge that this is a bit off-topic, but I hope the blatantly obsequious sucking up at the beginning of my note makes up for it.) -- Terry Carroll carr...@tjc.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] help
greetings, this is my first time using python and i just cannot figure out what I am doing wrong im sure the answer is very simple but sadly i do not know what it is thanks for the help! ''' Author: Frank Dominguez CS 140: February 5, 2018 Python Lab 1 Determine the cost of landscaping a backyard ''' length = eval(input("Enter the length of the yard") width = eval(input("Enter the width of the yard") sod = eval(input("Enter the cost of sod" fence = eval(input("What is the cost of the fence") area = length*width perimeter = length*2+width*2 total_sod = area*sod total_fence = fence*perimeter landscaping = total_sod+total_fence print ("the total cost of landscaping is",landscaping) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Suggest some Android app
On 07/02/18 03:22, Dragan Mestrovik wrote: > please suggest me some good android apps through which i can execute my > python scripts and practice python. I don't know if its the best but the one I use is QPython3. Its free and includes an interactive interpreter. > I want to download videos using > youtube-dl python script and want to save them to external drive like otg I have no idea if it will help with that... -- 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Java equivalent of Python-Tutor?
On 07/02/18 20:06, Terry Carroll wrote: > I'm doing a few projects in Java now and would love to find a similar > resource that covers that language, I did a deep dive into Java for a new job a couple of years ago and found the official Oracle tutorials very good combined with YouTube videos for a quick overview. If you have a reasonable Python background you should be able to follow it easily enough. The biggest challenges are the static typing (which soon becomes frustratingly annoying after Python! - that's where the IDE pays dividends) > and the Eclipse IDE. I used Netbeans as it is the official Java IDE so I can't help with Java on Eclipse, although I do know a lot of it depends on which plugins you install (I used it for UML designs in a previous life!). > Any suggestions? I also used two books (Actually I read about 5 but these were the most useful): - Learning Java - O'Reilly and - Java A Beginners Guide Both are good but, if forced to choose just one, I'd now opt for the more advanced version of the latter: "Java The Complete Reference" by the same author, Herbert Schildt. There is a new 10th edition out which means the previous one is available at much cheaper prices... FWIW Java 8 is now a half decent language, something I'd never have said about Java up to v5 (which was the last time I looked at it). Finally, If you need to use the Enterprise extensions (JEE) you can use the online tutorials but I definitely recommend pre-viewing YouTube vids for that. They really help with the concepts. HTH, -- 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help
On 07/02/18 21:34, Frank Dominguez wrote: > this is my first time using python and i just cannot figure out what I am > doing wrong im sure the answer is very simple but sadly i do not know what Please always include full error messages in your mail - don't assume we will willingly run buggy code! Also post the code in the email, using plain text rather than attaching it. It makes replies easier and ensures it doesn't get lost in the archives. Here is yours with my comments: > length = eval(input("Enter the length of the yard") > width = eval(input("Enter the width of the yard") > sod = eval(input("Enter the cost of sod" You are missing a closing paren there. But you are also missing a closing paren on all the other lines. You close the input() but not the eval() > fence = eval(input("What is the cost of the fence") Never, ever, combine eval() with input() it is a recipe for disaster since a malicious user (or a mis-typing one) can wreak havoc by typing in malicious code that could, potentially trash your computer. It's a very bad habit, avoid at all costs. Read the input and convert it explicitly using int() or float() or whatever. Like so: fence = float(input("What is the cost of the fence")) It's much safer (and not much extra typing). > area = length*width > perimeter = length*2+width*2 > total_sod = area*sod > total_fence = fence*perimeter > landscaping = total_sod+total_fence > print ("the total cost of landscaping is",landscaping) This bit seems fine. -- 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help
I think you need to convert the input value to an integer with int(variable_name) Sent from my iPhone > On Feb 7, 2018, at 4:34 PM, Frank Dominguez wrote: > > greetings, > this is my first time using python and i just cannot figure out what I am > doing wrong im sure the answer is very simple but sadly i do not know what > it is > thanks for the help! > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Java equivalent of Python-Tutor?
On Wed, Feb 7, 2018 at 2:06 PM, Terry Carroll wrote: > > In my early days of using Python I benefited greatly from this Tutor list, > thanks to both Alan and Steven as well as as many contributors. I still check > in now and then and try to chime in to help now that I have a bit more > experience under my belt. > > I'm doing a few projects in Java now and would love to find a similar > resource that covers that language, and the Eclipse IDE. Some of my questions > are too newbie for a forum like stackoverflow (and most of the responses > there assume a non-newbie level of knowledge). > > Any suggestions? When I was dabbling with Java a few years ago, I found the Beginning Java Forum at JavaRanch helpful. It can be found at: https://coderanch.com/f/33/java -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor