ฉันจะแปลงสตริงที่ต้องการ123,456.908ลอย123456.908ใน Python ได้อย่างไร
คำตอบ:
เพียงแค่ลบ,ด้วยreplace():
float("123,456.908".replace(',',''))
python cd_size = float("737,280,000".replace(',','')) (ฉันใช้ int จริง)
setlocaleตั้งแต่แรก
... หรือแทนที่จะถือว่าเครื่องหมายจุลภาคเป็นขยะเพื่อกรองออกเราสามารถถือว่าสตริงโดยรวมเป็นการจัดรูปแบบของโฟลที่แปลเป็นภาษาท้องถิ่นและใช้บริการแปล:
from locale import atof, setlocale, LC_NUMERIC
setlocale(LC_NUMERIC, '') # set to your default locale; for me this is
# 'English_Canada.1252'. Or you could explicitly specify a locale in which floats
# are formatted the way that you describe, if that's not how your locale works :)
atof('123,456') # 123456.0
# To demonstrate, let's explicitly try a locale in which the comma is a
# decimal point:
setlocale(LC_NUMERIC, 'French_Canada.1252')
atof('123,456') # 123.456
Extension modules should never call setlocale()
atof วิธีการสตริงในตัว สิ่งที่ฉันใช้ที่นี่คือฟังก์ชันatofจากlocaleโมดูลไลบรารีมาตรฐานซึ่งไม่ได้เลิกใช้งานเลย
locale.atof
หากคุณไม่ทราบว่าสถานที่เกิดเหตุและคุณต้องการที่จะแยกชนิดของหมายเลขใด ๆ ใช้นี้parseNumber(text)ฟังก์ชั่น ไม่สมบูรณ์แบบ แต่คำนึงถึงกรณีส่วนใหญ่:
>>> parseNumber("a 125,00 €")
125
>>> parseNumber("100.000,000")
100000
>>> parseNumber("100 000,000")
100000
>>> parseNumber("100,000,000")
100000000
>>> parseNumber("100 000 000")
100000000
>>> parseNumber("100.001 001")
100.001
>>> parseNumber("$.3")
0.3
>>> parseNumber(".003")
0.003
>>> parseNumber(".003 55")
0.003
>>> parseNumber("3 005")
3005
>>> parseNumber("1.190,00 €")
1190
>>> parseNumber("1190,00 €")
1190
>>> parseNumber("1,190.00 €")
1190
>>> parseNumber("$1190.00")
1190
>>> parseNumber("$1 190.99")
1190.99
>>> parseNumber("1 000 000.3")
1000000.3
>>> parseNumber("1 0002,1.2")
10002.1
>>> parseNumber("")
>>> parseNumber(None)
>>> parseNumber(1)
1
>>> parseNumber(1.1)
1.1
>>> parseNumber("rrr1,.2o")
1
>>> parseNumber("rrr ,.o")
>>> parseNumber("rrr1rrr")
1
หากคุณมีลูกน้ำเป็นตัวคั่นทศนิยมและจุดเป็นตัวคั่นหลักพันคุณสามารถทำได้:
s = s.replace('.','').replace(',','.')
number = float(s)
หวังว่ามันจะช่วยได้
อะไรประมาณนี้
my_string = "123,456.908"
commas_removed = my_string.replace(',', '') # remove comma separation
my_float = float(commas_removed) # turn from string to float.
ในระยะสั้น:
my_float = float(my_string.replace(',', ''))
นี่เป็นวิธีง่ายๆที่ฉันเขียนขึ้นเพื่อคุณ :)
>>> number = '123,456,789.908'.replace(',', '') # '123456789.908'
>>> float(number)
123456789.908
reเป็นค้อนขนาดใหญ่สำหรับงานดังกล่าว
float(number)เพราะสัมผัสที่บรรยาย +1 ;-)
เพียงแค่แทนที่,ด้วยการแทนที่ ()
f = float("123,456.908".replace(',',''))
print(type(f)
type () จะแสดงให้คุณเห็นว่าได้แปลงเป็น float แล้ว
ทางออกที่ดีกว่าสำหรับรูปแบบสกุลเงินต่างๆ :
def text_currency_to_float(text):
t = text
dot_pos = t.rfind('.')
comma_pos = t.rfind(',')
if comma_pos > dot_pos:
t = t.replace(".", "")
t = t.replace(",", ".")
else:
t = t.replace(",", "")
return(float(t))
localeโมดูล - ทุกสิ่งทุกอย่างเป็นเพียงการแฮ็กที่น่ารังเกียจที่จะทำให้คุณมีปัญหาในอนาคต