python-2.x

How to recognize name (from text file) in user input and then print name

我的梦境 提交于 2020-01-06 04:50:23
问题 My ideal goal is for the chat bot to recognize that you are talking about one of your siblings. So, when you mention your brother or sister (either by name or the keywords: my brother/sister) the chat bot will already know who they are from the data given in their text file. I have already figured out the keyword part, but when I mention them by name (For example: I can't stand James). The chat bot doesn't print what I want it to and when the user tells the chat bot their sibling's name it

How do I check if an input is a string or int in Python 2.x?

别来无恙 提交于 2020-01-06 02:41:05
问题 I am trying to check if an input is a word or a number. var = input("var: ") if isinstance(var, str): print "var = word" else: print "var = number" This is the code I came up with but sadly doesn't work; I'm new to python and programming in general so I don't know alot of commands, any suggestion would be appreciated ^^ 回答1: input() will take and evaluate your input before handing it over to you. That is, if the user enters exit() , your application will exit. This is undesirable from a

How to Get Index of Selected Option inTkinter Combobox

喜夏-厌秋 提交于 2020-01-05 07:11:12
问题 Following code lets me to print out the selected value from the Combobox, but I need to print(get) the indexnumber of selected item in the list. Can you please let me know how to do that? import Tkinter import tkMessageBox from Tkinter import * import ttk from ttk import * app = Tk() def OptionCallBack(*args): print variable.get() variable = StringVar(app) variable.set("Select From List") variable.trace('w', OptionCallBack) so = ttk.Combobox(app, textvariable=variable) so.config(values =(

python re.compile and split with ÆØÅ charcters

五迷三道 提交于 2020-01-04 05:21:10
问题 I am very new in Python. I do have a file with a list of words. They contain Danish letters (ÆØÅ) but the re.compile do not understand theses characters. The function split the words by each ÆØÅ. The text are downloade from Twitter and Facebook and do not always contain only letters. text = "Rød grød med fløde.... !! :)" pattern_split = re.compile(r"\W+") words = pattern_split.split(text.lower()) words = ['r', 'd', 'gr', 'd', 'med', 'fl', 'de'] The right result should be words = ['rød', 'grød

Swapping uppercase and lowercase in a string

寵の児 提交于 2020-01-03 14:01:23
问题 I would like to change the chars of a string from lowercase to uppercase. My code is below, the output I get with my code is a ; could you please tell me where I am wrong and explain why? Thanks in advance test = "AltERNating" def to_alternating_case(string): words = list(string) for word in words: if word.isupper() == True: return word.lower() else: return word.upper() print to_alternating_case(test) 回答1: If you want to invert the case of that string, try this: >>> 'AltERNating'.swapcase()

Url encode using python 2.7

孤人 提交于 2020-01-03 13:38:07
问题 >>> import httplib >>> x = httplib.HTTPConnection('localhost', 8080) >>> x.connect() >>> x.request('GET','/camera/store?fn=aaa&ts='+str.encode('2015-06-15T14:45:21.982600+00:00','ascii')+'&cam=ddd') >>> y=x.getresponse() >>> z=y.read() >>> z 'error: Invalid format: "2015-06-15T14:45:21.982600 00:00" is malformed at " 00:00"' And the system show me this error. As i want to encode this format to this: 2015-06-15T14%3A45%3A21.982600%2B00%3A00 来源: https://stackoverflow.com/questions/37541436/url

What versions of Python will work in Windows XP?

我是研究僧i 提交于 2020-01-03 09:29:26
问题 I would like the most advanced version of Python that still works on windows XP. I need Python 2 and Python 3. What versions of Python will work in Windows XP? 回答1: I found that python 2.7.9 and python 3.4.3 are the newest versions of python that work in windows xp. I found this out through trial and error. 回答2: I've found a desperate guy (Daniel Pistelli) who didn't give up at the windows error dialog: https://ntcore.com/?p=458 I like how he approaches the topic: If we try to start any new

How to map func_closure entries to variable names?

允我心安 提交于 2020-01-03 08:45:25
问题 I have a lambda object that is created in this function: def add_url_rule(self, rule, endpoint=None, view_func=None, **options): self.record(lambda s: s.add_url_rule(rule, endpoint, view_func, **options)) Using func_closure of the lambda function object I can access the closure scope of the lambda function: (<cell at 0x3eb89f0: str object at 0x2fb4378>, <cell at 0x3eb8a28: function object at 0x3cb3a28>, <cell at 0x3eb8a60: str object at 0x3ebd090>, <cell at 0x3eb8b08: dict object at 0x3016ec0

Compare string to float in Python [duplicate]

不羁的心 提交于 2020-01-03 03:48:26
问题 This question already has answers here : How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists? (2 answers) Closed 6 years ago . I tried this in python shell >>> a='apple' >>> b=11.1 >>> a>b True >>> a 'apple' >>> b>a False Can someone explain to me how a>b is True? When a is an string and b is float. 回答1: Order comparison between elements of different types was a "design bug" in Python 2.x that has been removed in Python 3

unpack_from requires a buffer of at least 4 bytes

孤街浪徒 提交于 2020-01-02 19:35:09
问题 I am receiving a packet from client, consisting of many fields. I read all fields successfully, but when it comes to the last field which is tag_end, python gives me an error: unpack_from requires a buffer of at least 4 bytes not found. this is the code: def set_bin(self, buf): """Reads a vector of bytes (probably received from network or read from file) and tries to construct the packet structure from it, by reading each packet member from the buffer. This is somehow like deserializing the