How to print number with commas as thousands separators?

I am trying to print an integer in Python 2.6.1 with commas as thousands separators. For example, I want to show the number 1234567 as 1,234,567 . How would I go about doing this? I have seen many examples on Google, but I am looking for the simplest practical way. It does not need to be locale-specific to decide between periods and commas. I would prefer something as simple as reasonably p

如何使用逗号作为数千个分隔符来打印数字?

我想用逗号作为数千个分隔符在Python 2.6.1中打印一个整数。 例如,我想显示1234567号码为1,234,567 。 我会如何去做这件事? 我在Google上看到了很多例子,但我正在寻找最简单的实用方法。 它不需要特定于语言环境来决定句点和逗号。 我宁愿尽可能简单的事情。 对于Python≥2.7: "{:,}".format(value) 按格式规范迷你语言, ','选项表示使用千位分隔符的逗号。 对于可识别语言环境的分隔符,请改为使用

Comment out a Python code block

This question already has an answer here: Why doesn't Python have multiline comments? 16 answers Python does not have such a mechanism. Prepend a # to each line to block comment. For more information see PEP 8. Most Python IDEs support a mechanism to do the block-commenting-with-pound-signs automatically for you. For example, in IDLE on my machine, it's Alt+3 and Alt+4. Don'

注释掉一个Python代码块

这个问题在这里已经有了答案: 为什么Python没有多行注释? 16个答案 Python没有这样的机制。 在每行前加一个#来阻止评论。 有关更多信息,请参阅PEP 8.大多数Python IDE支持一种自动为您执行块注释 - 磅 - 符号的机制。 例如,在我的机器上的IDLE中,它是Alt + 3和Alt + 4。 不要使用三重引号; 正如您发现的那样,这是针对文档字符串而不是阻止评论的,尽管它具有类似的效果。 如果你只是临时评论事情,这可以作为

better way than using if

Possible Duplicate: Putting a simple if-then statement on one line I am working on a python expression and I want that expression to be compressed than using the if else statement. s = [1, 2, 3, 4] if len(s)>5: print s.index(5) else: print 'cant print' Is there a better way than using as if else statement? You can do: s = [1, 2, 3, 4] print 'y' if len(s) > 5 else 'n' Howev

比使用if更好的方法

可能重复: 将简单的if-then语句放在一行上 我正在处理一个python表达式,并且我希望该表达式比使用if else语句进行压缩。 s = [1, 2, 3, 4] if len(s)>5: print s.index(5) else: print 'cant print' 有没有比使用if语句更好的方法? 你可以做: s = [1, 2, 3, 4] print 'y' if len(s) > 5 else 'n' 不过,我认为这不会让代码更具可读性(一目了然)。 还要注意的是if和else不创建一个循环,他们只是

Putting an if

I have read the links below, but it doesn't address my question. Does Python have a ternary conditional operator? (the question is about condensing if-else statement to one line) Is there an easier way of writing an if-elif-else statement so it fits on one line? For example, if expression1: statement1 elif expression2: statement2 else: statement3 [UPDATE] if i>100: x

放一个if

我已阅读下面的链接,但没有解决我的问题。 Python是否有三元条件运算符? (这个问题是关于将if-else语句压缩到一行) 是否有更简单的方法来编写if-elif-else语句,以便它适合于一行? 例如, if expression1: statement1 elif expression2: statement2 else: statement3 [UPDATE] if i>100: x=2 elif i<100: x=1 else: x=0 我只是觉得,如果上面的例子可以用下面的方式编写,它可能看起

how to write if and else in a single line in python

Possible Duplicate: Ternary conditional operator in Python I am working on python and trying to save data in to database Suppose i had below code conn = mdb.connect(user='root', passwd='redhat', db='Data', host='localhost', charset="utf8") ex_li = ['stri_one','stri_two','stri_three','stri_four'] if ex_li[0] != '': campaignID = ex_li[0] else: campaignID = '' if ex_li[1] != '':

如何写python中的if和else

可能重复: Python中的三元条件运算符 我正在研究python并试图将数据保存到数据库中假设我有下面的代码 conn = mdb.connect(user='root', passwd='redhat', db='Data', host='localhost', charset="utf8") ex_li = ['stri_one','stri_two','stri_three','stri_four'] if ex_li[0] != '': campaignID = ex_li[0] else: campaignID = '' if ex_li[1] != '': keywordID = ex_li[1] else: keywordID = '' ...

python ? (conditional/ternary) operator for assignments

This question already has an answer here: Does Python have a ternary conditional operator? 22 answers Python has such an operator: variable = something if condition else something_else Alternatively, although not recommended (see @karadoc's comment): variable = (condition and something) or something_else In older Python code, you may see the trick: condition and something or somethi

python? (条件/三元)操作员

这个问题在这里已经有了答案: Python是否有三元条件运算符? 22个答案 Python有这样一个运算符: variable = something if condition else something_else 或者,尽管不推荐(请参阅@ karadoc的评论): variable = (condition and something) or something_else 在较老的Python代码中,您可能会看到这样的技巧: condition and something or something_else 然而,这已被大大优越的... if ... else ...构造所取代:

How to delete temp folder data using python script

我如何创建Python脚本来删除Windows环境系统中临时文件夹中的数据。 你可以这样去import os os.remove("C:WindowsPrefetch*.*") os.remove("C:WindowsTemp*.*") os.remove("C:UsersYour_User_NameAppDataRoamingMicrosoftWindowsRecent Items*.*")

如何使用python脚本删除临时文件夹数据

我如何创建Python脚本来删除Windows环境系统中临时文件夹中的数据。 你可以这样去import os os.remove("C:WindowsPrefetch*.*") os.remove("C:WindowsTemp*.*") os.remove("C:UsersYour_User_NameAppDataRoamingMicrosoftWindowsRecent Items*.*")

How to remove a lot of folders at once using Python?

I have seen a lot of questions (Delete Folder Contents in Python, How to delete a file or folder?, How do I remove/delete a folder that is not empty with Python?) asking how to delete a folder (empty or not) but I haven't seen any asking about how to delete a large number of folders at once. I tried using shutils and writing something as shutils.rmtree('.../run*') (all the folders t

如何使用Python一次删除大量文件夹?

我看到很多问题(删除Python中的文件夹内容,如何删除文件或文件夹?,如何删除/删除Python中没有的文件夹?)询问如何删除文件夹(是否为空)但我还没有看到任何关于如何一次删除大量文件夹的问题。 我尝试使用shutils并将其写成shutils.rmtree('.../run*') (我想要删除的所有文件夹都称为run0000,run0001等等),但这不起作用,因为*未被理解。 最后,我最终导入了subprocess.Popen('rm -r ./run*/', sh

Error on deleting file in a python code

I have a little code that allows me to print images arriving in a folder. But I'd like them to be deleted just after having been printed - I want to try this on a RaspberryPI and I will not have enough space to store all images. If someone can help me it would be very much appreciated. Here is my code: monRep = "/Users/XX/Desktop/XXX/" import os, mimetypes, random while True: fpath

在Python代码中删除文件时出错

我有一个小小的代码,允许我打印到文件夹中的图像。 但是,我希望在打印后立即删除它们 - 我想在RaspberryPI上尝试此操作,并且我没有足够的空间来存储所有图像。 如果有人能帮助我,将非常感激。 这是我的代码: monRep = "/Users/XX/Desktop/XXX/" import os, mimetypes, random while True: fpaths = [] for fname in os.listdir(monRep): fpath = os.path.join(monRep, fname) if os.path.isf

strange output when using flags in python

I'm currently writing a script in python that takes a number of flags. This is my first attempt at such a program, and I am getting an output from the bash script that I don't quite understand. For example when I run the script in the bash shell: $ python my_script.py -f <input_file.txt> -k <test_string> -c <user_input> I get this output before my script's output

在Python中使用标志时奇怪的输出

我目前正在用Python编写一个脚本,它需要一些标志。 这是我第一次尝试这样的程序,并且我从bash脚本中得到了一个我不太明白的输出。 例如,当我在bash shell中运行脚本时: $ python my_script.py -f <input_file.txt> -k <test_string> -c <user_input> 我在脚本输出之前得到这个输出: usage: rm [-f | -i] [-dPRrvW] file ... unlink file 我似乎无法摆脱这一点,这对于产出的美观感到沮丧。