我试图在Python 2.6.1中使用逗号作为千位分隔符打印一个整数.例如,我想将数字显示1234567为1,234,567.我该怎么做呢?我在Google上看过很多例子,但我正在寻找最简单实用的方法.
它不需要特定于语言环境来决定句点和逗号.我希望尽可能简单的事情.
我正在创建一个读取文件的程序,如果文件的第一行不是空白,则会读取接下来的四行.在这些行上执行计算,然后读取下一行.如果该行不为空,则继续.但是,我收到此错误:
ValueError: invalid literal for int() with base 10: ''.
它正在读取第一行但不能将其转换为整数.
我该怎么做才能解决这个问题?
代码:
file_to_read = raw_input("Enter file name of tests (empty string to end program):")
try:
infile = open(file_to_read, 'r')
while file_to_read != " ":
file_to_write = raw_input("Enter output file name (.csv will be appended to it):")
file_to_write = file_to_write + ".csv"
outfile = open(file_to_write, "w")
readings = (infile.readline())
print readings
while readings != 0:
global count
readings = int(readings)
minimum = (infile.readline())
maximum = (infile.readline())
Run Code Online (Sandbox Code Playgroud) 我正在尝试以区域设置感知的方式对字符串列表进行排序.我已经将Babel库用于其他与i18n相关的任务,但它不支持排序.Python的locale模块提供了一个strcoll函数,但需要将进程的语言环境设置为我想要使用的语言环境.有点痛,但我可以忍受它.
问题是我似乎无法实际设置区域设置.该文件的locale模块给出了这样的例子:
import locale
locale.setlocale(locale.LC_ALL, 'de_DE')
Run Code Online (Sandbox Code Playgroud)
当我运行时,我得到了这个:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\Lib\locale.py", line 494, in setlocale
locale.Error: unsupported locale setting
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我有一个字符串,表示一个使用逗号分隔数千的数字.如何将其转换为python中的数字?
>>> int("1,000,000")
Run Code Online (Sandbox Code Playgroud)
生成一个ValueError.
在我尝试转换它之前,我可以用空字符串替换逗号,但不知何故感觉不对.有没有更好的办法?
我需要一个字符串转换格式"1.234.345,00"的float值1234345.00.
一种方法是重复使用str.replace:
x = "1.234.345,00"
res = float(x.replace('.', '').replace(',', '.'))
print(res, type(res))
1234345.0 <class 'float'>
Run Code Online (Sandbox Code Playgroud)
但是,这似乎是手动的和非一般化的.这个备受好评的答案建议使用该locale库.但我的默认语言环境与我的输入字符串没有相同的约定.然后我发现了一种方法来提取本地约定中使用的字符作为字典:
import locale
print(locale.localeconv())
{'int_curr_symbol': '', 'currency_symbol': '', 'mon_decimal_point': '',
..., 'decimal_point': '.', 'thousands_sep': '', 'grouping': []}
Run Code Online (Sandbox Code Playgroud)
有没有办法更新此字典,另存为自定义区域设置,然后能够调用此自定义区域设置.就像是:
mylocale = locale.create_new_locale() # "blank" conventions or copied from default
mylocale.localeconv()['thousands_sep'] = '.'
mylocale.localeconv()['decimal_point'] = ','
setlocale(LC_NUMERIC, mylocale)
atof('123.456,78') # 123456.78
Run Code Online (Sandbox Code Playgroud)
如果无法做到这一点,我们如何获得所有可用语言环境及其约定的列表?似乎反模式从约定中"推断"了正确的配置(更不用说低效/手动),所以我希望有一个通用的解决方案,比如上面的伪代码.
编辑:这是我尝试找到所有语言环境的地方thousands_sep == '.'和decimal_point == ','.事实上,更一般地说,通过组合这些参数对区域设置进行分组:
import locale
from …Run Code Online (Sandbox Code Playgroud) 对于 python 中的某些机器控制,我将结果写入一个文本文件,其他人可以将其复制到 Excel 中(这是在这种情况下最方便的方法)。但是,在荷兰,Excel 有一个逗号作为小数点分隔符,因此我希望文本文件中的结果“位置”为 123,456,但是当我使用这样的 f-string 方法时:
resultfile.write(f"Position\t{position:.5}")
Run Code Online (Sandbox Code Playgroud)
这显然会导致点小数分隔符。
如何在不遍历整个文件的情况下将其更改为逗号并用逗号替换点?
在 中Python 3.5,我想使用locale.atof以下代码将德语数字字符串转换为浮点数:
import locale
from locale import atof
locale.setlocale(locale.LC_ALL, 'de_DE')
number = atof('17.907,08')
Run Code Online (Sandbox Code Playgroud)
然而,这提出了一个ValueError:
ValueError: could not convert string to float: '17.907.08'
Run Code Online (Sandbox Code Playgroud)
atof()为了这个而生的吗? 如何复制DataFrameto_cliboard 并将其粘贴到 excel 中,逗号为十进制?
在 R 中,这很简单。
write.table(obj, 'clipboard', dec = ',')
Run Code Online (Sandbox Code Playgroud)
但我无法在pandasto_clipboard中弄清楚。
我没有成功尝试改变:
import locale
locale.setlocale(locale.LC_ALL, '')
Run Code Online (Sandbox Code Playgroud)
Spanish_Argentina.1252
或者 df.to_clipboard(float_format = '%,%')
提前致谢
在处理我们的一些数据时,我必须执行非常基本的列条件组合。填充空值后,尝试在分配新变量的列中添加。其中一列最终成为对象,这并非史无前例。然而,我发现看似有效的值不会转换为浮点数(例如 4,789.67)。经过多次搜索,似乎我所看到的每个解决方案都指向存在不规则字符(它没有描述我的情况)。因此,我尝试在 IPython 中进行实验以重现错误,并且我成功了。但是,我不明白为什么会出现此错误:
测试
z='4,534.07' #initial assignment
print z
print type(z) #checked type
print repr(z) #tried to reveal hidden characters
print repr(z.replace("'","")) #tried to remove excess quotes
print z[1:-1] #tried again to remove excess quotes
print float(z) #failed conversion attempt
Run Code Online (Sandbox Code Playgroud)
输出
4,534.07
<type 'str'>
'4,534.07'
'4,534.07'
,534.0
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-70-8a3c46ebe6ab> in <module>()
6 print z[1:-1]
7 print z
----> 8 print float(z)
ValueError: invalid literal for float(): 4,534.07
Run Code Online (Sandbox Code Playgroud)
我看到的基本转换问题的解决方案总是建议以下将“x”转换为 float -->> float(x)。如果有人能解释我遗漏了什么,我将不胜感激。(我以前没有发生过这种情况。)
我一直在使用Enthought平台:
发行说明 …
python ×9
comma ×1
decimal ×1
f-string ×1
format ×1
locale ×1
localization ×1
pandas ×1
python-3.5 ×1
python-3.x ×1
string ×1
windows ×1