How to convert string to lowercase in Python

Is there a way to convert a string from uppercase, or even part uppercase to lowercase? Eg Kilometers --> kilometers. s = "Kilometer" print(s.lower()) 官方文档是str.lower() 。 With Python 2, this doesn't work for non-English words in UTF-8. In this case decode('utf-8') can help: >>> s='Километр' >>> print s.lower() Километр >>> print s.decode('utf-8

如何在Python中将字符串转换为小写

有没有办法将字符串从大写,甚至是大写部分转换为小写? 例如公里 - >公里。 s = "Kilometer" print(s.lower()) 官方文档是str.lower() 。 对于Python 2,这对于UTF-8中的非英文单词不起作用。 在这种情况下, decode('utf-8')可以帮助: >>> s='Километр' >>> print s.lower() Километр >>> print s.decode('utf-8').lower() километр 如何在Python中将字符串转换为小写? 有没

displaying widget on top of widget

I'm making an application that shows a map of an area and I'm trying to draw nodes on top of it which can represent information. I made it all work, but did so simply by making one custom widget which I showed and printing everything again and again everytime information changed. Also I couldn't 'connect' the nodes to listeners, because they were just images in the original

在小部件顶部显示小部件

我正在制作一个显示区域地图的应用程序,我试图在其上绘制表示信息的节点。 我完成了所有的工作,但只是通过制作一个自定义小部件,然后在每次更改信息时一次又一次地打印所有内容。 此外,我无法将节点“连接”到侦听器,因为它们只是原始小部件中的图像。 这使我想改革我的图形用户界面,现在我正在努力让每个类都成为一个自定义小部件! 但是有一个问题,我的MapNodes不再显示。 我搜索了stackoverflow并找到了这个有用

Accessing field of Protobuf message of unknown type in Python

Let's say I have 2 Protobuf-Messages, A and B. Their overall structure is similar, but not identical. So we moved the shared stuff out into a separate message we called Common. This works beautifully. However, I'm now facing the following problem: A special case exists where I have to process a serialized message, but I don't know whether it's a message of type A or type B. I

在Python中访问未知类型的Protobuf消息的字段

假设我有2个Protobuf消息,A和B.它们的总体结构相似,但不完全相同。 所以我们把共享的东西放到了一个我们称为Common的单独消息中。 这工作很好。 然而,我现在面临以下问题:存在一个特殊情况,我必须处理序列化消息,但我不知道它是A类型还是B类型消息。我在C ++中有一个工作解决方案(显示下面),但我没有找到一种方法来在Python中做同样的事情。 例: // file: Common.proto // contains some kind of shared struct

How to print a class or objects of class using print()?

I am learning the ropes in Python. When I try to print an object of class Foobar using the print() function, I get an output like this: <__main__.Foobar instance at 0x7ff2a18c> Is there a way I can set the printing behaviour (or the string representation) of a class and its objects? For instance, when I call print() on a class object, I would like to print its data members in a certain

如何使用print()打印类或类的对象?

我正在学Python的绳索。 当我尝试使用print()函数print() Foobar类的对象时,我得到如下输出: <__main__.Foobar instance at 0x7ff2a18c> 有没有一种方法可以设置类和它的对象的打印行为(或字符串表示)? 例如,当我在类对象上调用print()时,我想以特定的格式打印其数据成员。 如何在Python中实现这一点? 如果您熟悉C ++类,则可以通过为类添加friend ostream& operator << (ostream&, const Fo

How can I remove (chomp) a trailing newline in Python?

什么是Perl的chomp函数的Python等价物,如果它是换行符,它将删除字符串的最后一个字符? Try the method rstrip() (see doc Python 2 and Python 3) >>> 'test stringn'.rstrip() 'test string' Python's rstrip() method strips all kinds of trailing whitespace by default, not just one newline as Perl does with chomp . >>> 'test string n rnnr nn'.rstrip() 'test string' To strip only ne

如何删除(chomp)Python中的尾随换行符?

什么是Perl的chomp函数的Python等价物,如果它是换行符,它将删除字符串的最后一个字符? 尝试方法rstrip() (请参阅文档Python 2和Python 3) >>> 'test stringn'.rstrip() 'test string' Python的rstrip()方法默认剥离各种尾随空白,而不仅仅是Perl对chomp所做的一个新行。 >>> 'test string n rnnr nn'.rstrip() 'test string' 仅剥离换行符: >>> 'test string n rnnr nn'.rstrip('n') 'test

How to flush output of Python print?

How do I force Python's print function to output to the screen? This is not a duplicate of Disable output buffering - the linked question is attempting unbuffered output, while this is more general. The top answers in that question are too powerful or involved for this one (they're not good answers for this), and this question can be found on Google by a relative newbie. import sys sy

如何刷新Python打印输出?

我如何强制Python的打印功能输出到屏幕? 这不是重复的禁用输出缓冲 - 链接的问题是尝试无缓冲输出,而这是更一般的。 这个问题的最佳答案太强大了,或者涉及到这个问题(他们不是很好的答案),这个问题可以在Google上找到一个相对的新手。 import sys sys.stdout.flush() 默认打印输出到sys.stdout 。 参考文献: http://docs.python.org/reference/simple_stmts.html#the-print-statement http://docs.python.org/

Which is the best python module for command execution

This question already has an answer here: Calling an external command in Python 50 answers os.system always runs /bin/sh , which parses the command string. This can be a security risk if you have whitespace, $ etc. in the command arguments, or the user has a shell config file. To avoid all such risks, use subprocess with a list or tuple of strings as the command ( shell=False ) instead. T

哪个是执行命令的最佳python模块

这个问题在这里已经有了答案: 在Python中调用外部命令50个答案 os.system总是运行/bin/sh ,它解析命令字符串。 如果在命令参数中有空格, $等,或者用户有一个shell配置文件,这可能会带来安全风险。 为避免所有这些风险,可以使用带有列表或字符串元组的subprocess作为命令( shell=False )。 要在命令中模拟os.chdir ,请在subprocess os.chdir中使用cwd=参数。

how to run my own external command in python script

This question already has an answer here: Calling an external command in Python 50 answers Run a linux system command as a superuser, using a python script 5 answers 你可以使用subprocess.Popen来达到这个目的: import shlex import subprocess proc = subprocess.Popen(shlex.split('sudo insteon on 23')) proc.communicate()

如何在python脚本中运行我自己的外部命令

这个问题在这里已经有了答案: 在Python中调用外部命令50个答案 以超级用户身份运行linux系统命令,使用python脚本5个答案 你可以使用subprocess.Popen来达到这个目的: import shlex import subprocess proc = subprocess.Popen(shlex.split('sudo insteon on 23')) proc.communicate()

Running shell commands within python interpreter

This question already has an answer here: Calling an external command in Python 50 answers In plain python, you need to use something along the lines of this: from subprocess import check_output check_output("ls", shell=True) In IPython, you can run either of those commands or a general shell command by starting off with ! . For example ! echo "Hello, world!" > /tmp/Hello.txt If you&

在python解释器中运行shell命令

这个问题在这里已经有了答案: 在Python中调用外部命令50个答案 在普通的python中,你需要使用以下内容: from subprocess import check_output check_output("ls", shell=True) 在IPython中,你可以运行这些命令或者一个通用的shell命令,从头开始! 。 例如 ! echo "Hello, world!" > /tmp/Hello.txt 如果您以交互方式使用python,那么您几乎肯定会对IPython感到高兴。 如果您打算在调用命令(ls,pwd,...)时交

Putting nested bash command in Python

This question already has an answer here: Calling an external command in Python 50 answers $(..) , aka command expansion, is performed by the shell. Since you're now using Python instead of a shell, you don't get that feature for free. The simple fix is to invoke a shell and give it your command: tcpd = subprocess.Popen(["bash", "-c", "timeout 2 tcpdump -i eth1 -s 96 -w /usr/src/p

将嵌套的bash命令放入Python中

这个问题在这里已经有了答案: 在Python中调用外部命令50个答案 $(..) ,也就是命令扩展,由shell执行。 由于您现在使用Python而不是shell,因此您无法免费获得该功能。 简单的解决方法是调用一个shell并为其提供命令: tcpd = subprocess.Popen(["bash", "-c", "timeout 2 tcpdump -i eth1 -s 96 -w /usr/src/pcapFiles/dump$(date +%y%m%d-%H%M%S).pcap"], stdout=subprocess.PIPE) output, err = tcpd.communicate()