[Tutor] Retain UTF-8 Character in Python List
Hi, it's my first time in here. I hope you don't mind if I straight to the question. I do some work in python 2 and my job is to collect some query and then send it to java program via json. We're doing batch update in Apache Phoenix, that's why I collect those query beforehand. My question is: *Can we retain utf-8 character in list without changing its form into \xXX or \u00XX?* The reason is because that java program insert it directly "as is" without iterating the list. So, my query will be the same as we print the list directly. Example: c = 'sffs © fafd' l = list() l.append(c) print l ['sffs \xc2\xa9 fafd'] # this will be inserted, not ['sffs © fafd'] --- Best regards, Boy Sandy Gladies Arriezona ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] creat a program that reads frequency of words in file
Hello. i need serious help. i am a very very new python programmer. I have never done any code in my life. I am lost with these assignments for a class i am taking. I hope someone can assist. below is what i have so far which i know is incorrect. my question is how do i create a dictionary and save the words plus counts to it? i created an empty dictionary and i understand the program should read the entire file and create dictionary and store the data into it. but the only way i could get it to run at all was in the way you see below. i don’t think anything is actually being saved into the dictionary. i am so lost… “”" Word Frequency Write a program that reads the contents of a text file. The program should create a dictionary in which the keys are the individual words found in the file and the values are the number of times each word appears. for example, if the word 'the' appears 128 times, the dictionary would contain an element with 'the' as the key and 128 as the value. the program should either display the frequency of each word or create a second file containing a list of each words and its frequency. """ def main(): dict = {} count = 0 text = input('enter word: ') data = open("words.txt").readlines() for line in data: if text in line: count += 1 print("This word appears", count, "times in the file") main() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Retain UTF-8 Character in Python List
Boy Sandy Gladies Arriezona wrote: > Hi, it's my first time in here. Welcome! > I hope you don't mind if I straight to the > question. > I do some work in python 2 and my job is to collect some query and then > send it to java program via json. We're doing batch update in Apache > Phoenix, that's why I collect those query beforehand. > > My question is: > *Can we retain utf-8 character in list without changing its form into \xXX > or \u00XX?* The reason is because that java program insert it directly "as > is" without iterating the list. So, my query will be the same as we print > the list directly. > > Example: > c = 'sffs © fafd' > l = list() > > l.append(c) > > print l > ['sffs \xc2\xa9 fafd'] # this will be inserted, not ['sffs © fafd'] >>> import json >>> items = [u"sffs © fafd"] # unicode preferrable over str for non-ascii data >>> print json.dumps(items, ensure_ascii=False) ["sffs © fafd"] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creat a program that reads frequency of words in file
On 01/06/15 05:01, Stephanie Quiles wrote: Hello. i need serious help. i am a very very new python programmer. Welcome. Write a program that reads the contents of a text file. OK, Well you have done that bit OK. The program should create a dictionary in which the keys are the individual words found in the file But you are failing on this bit. As you said you are not storing anything in the dictionary. You need to split your lines into individual words, then store each word in the dictionary. I suggest you simplify the problem for now and try just doing that. Don't worry about counting them for now just save the words into the dictionary. You should end up with the dictionary containing one entry for each of the different words in the file. Some other comments below. def main(): dict = {} Don't use dict as a variable because thats a Python function for creating dictionaries. By using it as a name you hide the function. AS a general rule never name variables after their type, name them after their purpose. In this case it could be called 'words' or 'counts' or something similar. count = 0 text = input('enter word: ') You weren't asked to read a word from the user. All the data you need is in the file. data = open("words.txt").readlines() for line in data: You don't need the readlines() line you can just do for line in open("words.txt"): However, best practice says that this is even better: with open("words.txt") as data: for line in data: This ensures that the file is correctly closed at the end. if text in line: count += 1 print("This word appears", count, "times in the file") And this is, of course, completely off track. You need to split the line into its separate words and store each word into the dictionary. 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] Retain UTF-8 Character in Python List
On Mon, Jun 01, 2015 at 09:39:03AM +0700, Boy Sandy Gladies Arriezona wrote: > Hi, it's my first time in here. I hope you don't mind if I straight to the > question. > I do some work in python 2 and my job is to collect some query and then > send it to java program via json. We're doing batch update in Apache > Phoenix, that's why I collect those query beforehand. In Python 2, regular strings "" are actually ASCII byte strings, and cannot include Unicode characters. If you try, you'll get something platform dependent, which may be UTF-8, but could be something else. So for example: py> s = "a©b" # Not a Unicode string py> len(s) # Expecting 3. 4 py> for c in s: print c, repr(c) ... a 'a' � '\xc2' � '\xa9' b 'b' Not what you want! Instead, you have to use Unicode strings, u"". py> s = u"a©b" # Unicode string py> len(s) 3 py> for c in s: print c, repr(c) ... a u'a' © u'\xa9' b u'b' py> print s a©b Remember, the u is not part of the string, it is part of the delimiter: ASCII byte string uses delimiters " " or ' ' Unicode string uses delimiters u" " or u' ' > My question is: > *Can we retain utf-8 character in list without changing its form into \xXX > or \u00XX?* The reason is because that java program insert it directly "as > is" without iterating the list. So, my query will be the same as we print > the list directly. What do you mean, the Java program inserts it directly? Inserts it into what? > Example: > c = 'sffs © fafd' > l = list() > l.append(c) > print l > ['sffs \xc2\xa9 fafd'] # this will be inserted, not ['sffs © fafd'] Change the string 'sffs...' to a Unicode string u'sffs...' and your example will work. *However*, don't be fooled by Python's list display: py> mylist = [u'a©b'] py> print mylist [u'a\xa9b'] "Oh no!", you might think, "Python has messed up my string and converted the © into \xa9 which is exactly what I don't want!" But don't be fooled, that's just the list's default display. The string is actually still exactly what you want, it just displays anything which is not ASCII as an escape sequence. But if you print the string directly, you will see it as you intended: py> print mylist[0] # just the string inside the list a©b By the way, if you can use Python 3 instead of Python 2, you may find the Unicode handling is a bit simpler and less confusing. For example, in Python 3, the way lists are printed is a bit more sensible: py> mylist = ['a©b'] # No need for the u' delimiter in Python 3. py> print(mylist) ['a©b'] Python 2.7 will do the job, if you must, but it will be a bit harder and more confusing. Python 3.3 or higher is a better choice for Unicode. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Should error checking be duplicated for both functions if one function calls another one?
Suppose in a given state of a program, function 1 calls function 2. Function 1 includes checks for possible error conditions. If there are no issues, then function 2 should execute with no issues as well. The question is, should function 2 include the error checking done in function 1 if function 2 is only ever called by function 1? My inclination is to say yes, as in some future incarnation of the program function 2 might get called in new ways. What are your thoughts? -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Should error checking be duplicated for both functions if one function calls another one?
Hello, Not sure if I got it, but, in my opinion functions should do only one thing.So if function 2 finds an error, it should raise it. There should be another function (function 1 in your case?) taking care of possible raised errors. Best 2015-06-01 16:27 GMT+02:00 boB Stepp : > Suppose in a given state of a program, function 1 calls function 2. > Function 1 includes checks for possible error conditions. If there are > no issues, then function 2 should execute with no issues as well. The > question is, should function 2 include the error checking done in > function 1 if function 2 is only ever called by function 1? > > My inclination is to say yes, as in some future incarnation of the > program function 2 might get called in new ways. What are your > thoughts? > > -- > boB > ___ > 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] Should error checking be duplicated for both functions if one function calls another one?
On Mon, Jun 1, 2015 at 9:33 AM, David Palao wrote: > Hello, > Not sure if I got it, but, in my opinion functions should do only one > thing.So if function 2 finds an error, it should raise it. There > should be another function (function 1 in your case?) taking care of > possible raised errors. I guess my question was not clearly worded. The idea is that function 1 calls another function. Function 1 checks for possible errors that are relevant. Some or all of these checks are also relevant to the called function. Should the called function also include these relevant error checks? boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Should error checking be duplicated for both functions if one function calls another one?
boB Stepp wrote: > On Mon, Jun 1, 2015 at 9:33 AM, David Palao > wrote: >> Hello, >> Not sure if I got it, but, in my opinion functions should do only one >> thing.So if function 2 finds an error, it should raise it. There >> should be another function (function 1 in your case?) taking care of >> possible raised errors. > > I guess my question was not clearly worded. The idea is that function > 1 calls another function. Function 1 checks for possible errors that > are relevant. Some or all of these checks are also relevant to the > called function. Should the called function also include these > relevant error checks? I think more important than not repeating the checks is that you avoid duplicate code that does the same thing. If the checks are relatively cheap I don't see a problem with def check(a): """Verify that a ... Helper for f1() and f2(). """ if ...: raise ValueError def f1(a, b): check(a) c = ... f2(a, c) def f2(a, c): check(a) ... # actual work Alternatively you can make f2() private by convention: def f1(a, b): check(a) c = ... _f2(a, c) def _f2(a, c): """Frobnicate a with c. Should only be called by f1() which first verifies that `a` cannot explode. """ ... Should you need a standalone version of f2() later just implement it as def f2(a, c): check(a) return _f2(a, c) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Should error checking be duplicated for both functions if one function calls another one?
How many layers do you expect your program to have? (And if the answer is 'a whole lot' then maybe your design needs to be reconsidered.) Dealing with the exception at the lowest level that can deal with it is usually a good idea. Also dealing with the exception at the top level, so that when bad things happen (i.e. the low level that was supposed to catch it had a bug and didn't) your program doesn't terminate, but bravely tries to do the best it can -- this often a good idea. Copy and pasting the same wretched error handling code into every layer of your program is pretty much never a good idea. Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Should error checking be duplicated for both functions if one function calls another one?
On Mon, Jun 01, 2015 at 09:27:07AM -0500, boB Stepp wrote: > Suppose in a given state of a program, function 1 calls function 2. > Function 1 includes checks for possible error conditions. If there are > no issues, then function 2 should execute with no issues as well. The > question is, should function 2 include the error checking done in > function 1 if function 2 is only ever called by function 1? The answer is, "that depends". Suppose *both* functions are public functions, which anyone can use. Some people will call function 1 directly, some will call function 2 directly. In this case, both functions need to do their own error checking, because they cannot trust their arguments will be correct. For example: def greet(name): if name == '': raise ValueError('you must give a name') return "Hello " + name def long_greet(name): if name == '': raise ValueError('you must give a name') return "Greetings and salutations! " + greet(name) Of course, if the error checking is complicated, you should factor it out into a third function: def greet(name): check(name) ... def long_greet(name): check(name) ... There's another possibility. Suppose that only the first function is for public consumption, part of your library's public API. Since anyone might use the first function, including beginners, idiots and people who don't read the documentation, it needs to check the argument. But the second function is only used by *you*, as an internal detail. Of course you give the second function a name starting with an underscore, so that others will know not to use it. (Single underscore names are "private, don't touch".) In this case, the second function doesn't need to check it's arguments because it can trust that the first function will always do the right thing. def function(arg): if arg > 0: return _other_function(arg) else: raise ValueError def _other_function(arg): return ... After all, you would never make a mistake and pass the wrong value, would you? Hmmm... perhaps we should be a little more careful... (Note: in Python, this failure to check arguments is not as dangerous as it may be in some other languages. You typically won't crash the computer, or cause some horrible security vulnerability that lets criminals or the NSA take over your computer. You will probably just get an exception. So there are circumstances where you might choose to just completely ignore any error checking.) What we can do is an intermediate level of error checking between the full checking of arguments done by public functions, and the unconditional trust of the private function, by using assertions. Assertions are checks which can be turned off. (Although, in truth, most people never bother.) Our public function stays the same, and the private one becomes: def _other_function(arg): assert arg > 0 return ... If the assertion is ever false, arg is not larger than 0, Python will raise an AssertionError exception and stop the program. You will then be suitably embarrassed and fix the bug, before it is released to your customers and users. But, unlike the regular form of error checking, the assumption here is that assertions should always pass. Since they will always pass, they don't do anything, and can be turned off safely. You do that by running Python with the -O (for optimize) commandline switch, which disables asserts. This style of coding is often called "Design By Contract", and it is a powerful system for ensuring the safety of error checking during development and the speed of skipping unnecessary checks after deployment. You can read more about the use of assert here: http://import-that.dreamwidth.org/676.html -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Retain UTF-8 Character in Python List
On Mon, Jun 1, 2015 at 6:22 PM, Steven D'Aprano wrote: What do you mean, the Java program inserts it directly? Inserts it into what? I mean that java program use the list and directly send it into apache phoenix to do "batch upsert". I thought that because the list is not show me the way I set it, then it will send the wrong data into apache phoenix. I still haven't found the problem yet, I don't know which program is at fault. Anyway, your explanation is really good, thank you. On Mon, Jun 1, 2015 at 4:55 PM, Peter Otten <__pete...@web.de> wrote: import json items = [u"sffs © fafd"] # unicode preferrable over str for non-ascii data print json.dumps(items, ensure_ascii=False) ["sffs © fafd"] I haven't try that, using unicode strings before append it into the list. Awesome, I'll try it out later when I got in the office. Thank you very much. --- Best regards, Boy Sandy Gladies Arriezona ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Should error checking be duplicated for both functions if one function calls another one?
On 01/06/2015 15:37, boB Stepp wrote: On Mon, Jun 1, 2015 at 9:33 AM, David Palao wrote: Hello, Not sure if I got it, but, in my opinion functions should do only one thing.So if function 2 finds an error, it should raise it. There should be another function (function 1 in your case?) taking care of possible raised errors. I guess my question was not clearly worded. The idea is that function 1 calls another function. Function 1 checks for possible errors that are relevant. Some or all of these checks are also relevant to the called function. Should the called function also include these relevant error checks? boB No, the end result would be horrendous code bloat if that was applied across the board. Function 2 should do the checking and raise an error if there's a problem. Function 1 can catch that and proceed or not as it sees fit, as can any other piece of code calling function 2. It's the DRY principle, see http://c2.com/cgi/wiki?DontRepeatYourself -- 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] creat a program that reads frequency of words in file :p:
On 06/01/2015 05:56 PM, Alan Gauld wrote: if text in line: count += 1 print("This word appears", count, "times in the file") And this is, of course, completely off track. You need to split the line into its separate words and store each word into the dictionary. OP may want to research the setdefault and get methods for dictionaries. SDG, tom ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Trouble using bioread to convert files from .acq to .mat
Hello, I am a new Python user attempting to use bioread ( https://pypi.python.org/pypi/bioread/0.9.5) to convert files from aqknowledge to matlab. I am using a 64-bit PC, and I have downloaded Matlab, Python, numpy, scipy and bioread. Can someone walk me through the installation process for this package? I can't seem to get it to work. Thank you so much for your help!! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble using bioread to convert files from .acq to .mat
On 01/06/15 20:50, Ila Kumar wrote: Hello, I am a new Python user attempting to use bioread ( https://pypi.python.org/pypi/bioread/0.9.5) to convert files from aqknowledge to matlab. I am using a 64-bit PC, and I have downloaded Matlab, Python, numpy, scipy and bioread. Can someone walk me through the installation process for this package? I can't seem to get it to work. This list is really for questions about core Python and its standard library. None of the packages you mention fall into that category so you will likely get better support on their dedicated fora. The SciPy packages in particular have an active community. That having been said, if you are struggling with installation, try getting one of the SciPy bundles such as Anaconda or Canopy which install all (or most) of the modules you mention as default. However, if you do have questions about core python feel free to ask those here. -- 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] Trouble using bioread to convert files from .acq to .mat
On 01/06/2015 23:50, Alan Gauld wrote: On 01/06/15 20:50, Ila Kumar wrote: Hello, I am a new Python user attempting to use bioread ( https://pypi.python.org/pypi/bioread/0.9.5) to convert files from aqknowledge to matlab. I am using a 64-bit PC, and I have downloaded Matlab, Python, numpy, scipy and bioread. Can someone walk me through the installation process for this package? I can't seem to get it to work. This list is really for questions about core Python and its standard library. None of the packages you mention fall into that category so you will likely get better support on their dedicated fora. The SciPy packages in particular have an active community. That having been said, if you are struggling with installation, try getting one of the SciPy bundles such as Anaconda or Canopy which install all (or most) of the modules you mention as default. However, if you do have questions about core python feel free to ask those here. If the OP comes back please provide your OS and Python versions and a specific problem. For all we know "I can't seem to get it to work" might mean that everything is going along beautifully but you just can't see the output because your monitor has failed :) -- 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
[Tutor] Parsing/Crawling test College Class Site.
Hi. I'm creating a test py app to do a quick crawl of a couple of pages of a psoft class schedule site. Before I start asking questions/pasting/posting code... I wanted to know if this is the kind of thing that can/should be here.. The real issues I'm facing aren't so much pythonic as much as probably dealing with getting the cookies/post attributes correct. There's ongoing jscript on the site, but I'm hopeful/confident :) that if the cookies/post is correct, then the target page can be fetched.. If this isn't the right list, let me know! And if it is, I'll start posting.. Thanks -bd ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Parsing/Crawling test College Class Site.
On 02/06/15 00:06, bruce wrote: Hi. I'm creating a test py app to do a quick crawl of a couple of pages of a psoft class schedule site. Before I start asking questions/pasting/posting code... I wanted to know if this is the kind of thing that can/should be here.. Probably. we are targeted at beginners to Python and focus on core language and standard library. If you are using the standard library modules to build your app then certainly., If you are using a third party module then we may/may not be able to help depending on who, if anyone, within the group is familiar with it. In that case you may be better on the forum. The real issues I'm facing aren't so much pythonic as much as probably dealing with getting the cookies/post attributes correct. There's ongoing jscript on the site, but I'm hopeful/confident :) that if the cookies/post is correct, then the target page can be fetched.. Post sample code, any errors you get and as specific a description of the issue as you can. Include OS and Python versions. Use plain text not HTML to preserve code formatting. If it turns out to be way off topic we'll tell you (politely) where you should go for help. -- 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] creat a program that reads frequency of words in file
I've CCd the list. Please use reply all when responding to the list. Also please use plain text as HTML/RTF doesn't work on all systems and code in particular often gets mangled. On 01/06/15 23:59, Stephanie Quiles wrote: Hello again, here is the final code… I think :) please see below. Is this is the easiest way to go about it? I appreciate your assistance! defmain(): words = {} count =0 Do you need count? What is its purpose? withopen('words.txt')asdata: forlineindata: text = line.split() forwordintext: ifwordnot inwords: words[word] =1 else: words[word] +=1 Look into the setdefault() method of dictionaries. It can replace the if/else above. count +=1 print(words) Aside from the two comments above, good job! -- 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] Parsing/Crawling test College Class Site.
On 02/06/2015 00:06, bruce wrote: Hi. I'm creating a test py app to do a quick crawl of a couple of pages of a psoft class schedule site. Before I start asking questions/pasting/posting code... I wanted to know if this is the kind of thing that can/should be here.. The real issues I'm facing aren't so much pythonic as much as probably dealing with getting the cookies/post attributes correct. There's ongoing jscript on the site, but I'm hopeful/confident :) that if the cookies/post is correct, then the target page can be fetched.. If this isn't the right list, let me know! And if it is, I'll start posting.. Thanks -bd You'll almost certainly need the main list at https://mail.python.org/mailman/listinfo/python-list alternatively available as gmane.comp.python.general However just to get you going take a look at these. https://pypi.python.org/pypi/requests https://pypi.python.org/pypi/beautifulsoup4 -- 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