Python xác định các chức năng chuyển đổi loại để chuyển đổi trực tiếp một loại dữ liệu sang loại dữ liệu khác. Bài viết này nhằm mục đích cung cấp thông tin về việc chuyển đổi chuỗi thành float. Trong Python, chúng ta có thể sử dụng float [] để chuyển đổi chuỗi thành float. và chúng ta có thể sử dụng int [] để chuyển đổi chuỗi thành số nguyên.float[] to convert String to float. and we can use int[] to convert String to an integer.
Chương trình Python để phân tích một chuỗi vào một chiếc phao
Hàm này được sử dụng để chuyển đổi bất kỳ loại dữ liệu nào thành số điểm nổi.
Cú pháp của phao:
float[x]
Phương thức chỉ chấp nhận một tham số và đó cũng là tùy chọn để sử dụng. Nếu không có đối số được truyền thì phương thức trả về 0,0.
Ví dụ 1: phân tích một chuỗi vào một chiếc phao
Trong ví dụ này, chúng tôi đang chuyển đổi chuỗi thành float.
Python3
3.141 3.1417
3.141 3.1418
3.141 3.1419
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'0
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'1
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'0
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'4
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'5
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'6
3.141 3.1418
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'8
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'9
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'0
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'6
int[]3
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'0
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'4
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'6
int[]9
Output:
3.141 3.141
Ví dụ 2: Một chuỗi float số vào phao
Trong ví dụ này, chúng tôi cố gắng chuyển đổi một chuỗi thành một số nguyên và sau đó là một float. Trong đầu ra, chúng ta có thể thấy rằng chúng ta có thể convent một số float chuỗi vào một số nguyên, do đó, chúng ta sẽ gặp lỗi giá trị.
Python3
555670
3.141 3.1418
555672
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'6
3.141 3.1418
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'8
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
555677
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'5
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'0
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'4
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'6
int[]9
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'0
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
157
158
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'6
int[]3
Output:
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'
Chương trình Python để phân tích một chuỗi vào int
Hàm này được sử dụng để chuyển đổi bất kỳ loại dữ liệu nào thành số nguyên.
Cú pháp của int
int[]
Ví dụ 1: phân tích một chuỗi thành một số nguyên
Trong ví dụ này, chúng tôi đang chuyển đổi chuỗi thành số nguyên.
Python3
555670
3.141 3.1418
>>> float["545.2222"]
545.2222
3ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'6
3.141 3.1418
555677
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'1
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'0
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'4
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'6
int[]9
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'0
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
157
158
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'6__
Output:
55567
Ví dụ 2: Một chuỗi nổi số vào một số nguyên
Trong ví dụ này, chúng tôi cố gắng chuyển đổi chuỗi float thành float và sau đó thành số nguyên.
Python3
>>> int["0b11111", 2]
31
>>> int["11111", 2]
31
>>> int['0o37', 8]
31
>>> int['37', 8]
31
>>> int['0x1f', 16]
31
>>> int['1f', 16]
31
03.141 3.1418
>>> int["0b11111", 2]
31
>>> int["11111", 2]
31
>>> int['0o37', 8]
31
>>> int['37', 8]
31
>>> int['0x1f', 16]
31
>>> int['1f', 16]
31
2>>> int["0b11111", 2]
31
>>> int["11111", 2]
31
>>> int['0o37', 8]
31
>>> int['37', 8]
31
>>> int['0x1f', 16]
31
>>> int['1f', 16]
31
33.141 3.1418
555677
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'8
>>> int["0b11111", 2]
31
>>> int["11111", 2]
31
>>> int['0o37', 8]
31
>>> int['37', 8]
31
>>> int['0x1f', 16]
31
>>> int['1f', 16]
31
8ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'0
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'3
ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'4
>>> int["0b11111", 0]
31
>>> int['0o37', 0]
31
>>> int['0x1f', 0]
31
2ValueError Traceback [most recent call last] in [] 1 string = '55.567' 2 ----> 3 Float = float[int[string]] 4 5 print[type[Float]] ValueError: invalid literal for int[] with base 10: '55.567'0
>>> int["0b11111", 0]
31
>>> int['0o37', 0]
31
>>> int['0x1f', 0]
31
4Output:
15
Trong Python, làm thế nào tôi có thể phân tích một chuỗi số như "545.2222" với giá trị float tương ứng của nó, 542.2222? Hoặc phân tích chuỗi "31" cho một số nguyên, 31? Tôi chỉ muốn biết làm thế nào để phân tích một chuỗi float vào một float và [riêng biệt] một chuỗi int vào int. I just want to know how to parse a float string to a float, and [separately] an int string to an int.
Thật tốt khi bạn yêu cầu làm những điều này một cách riêng biệt. Nếu bạn đang trộn chúng, bạn có thể sẽ tự đặt ra các vấn đề sau này. Câu trả lời đơn giản là:
5 để nổi:>>> int["0b11111", 0]
31
>>> int['0o37', 0]
31
>>> int['0x1f', 0]
31
>>> float["545.2222"]
545.2222
6 cho một số nguyên:>>> int["0b11111", 0]
31
>>> int['0o37', 0]
31
>>> int['0x1f', 0]
31
>>> int["31"]
31
Các chuyển đổi khác, ints đến và từ các chuỗi và chữ:
Chuyển đổi từ các cơ sở khác nhau và bạn nên biết cơ sở trước [10 là mặc định]. Lưu ý Bạn có thể đặt tiền tố chúng với những gì Python mong đợi cho các chữ của nó [xem bên dưới] hoặc xóa tiền tố:
>>> int["0b11111", 2]
31
>>> int["11111", 2]
31
>>> int['0o37', 8]
31
>>> int['37', 8]
31
>>> int['0x1f', 16]
31
>>> int['1f', 16]
31
Nếu bạn không biết trước cơ sở, nhưng bạn biết họ sẽ có tiền tố chính xác, Python có thể suy ra điều này cho bạn nếu bạn vượt qua
>>> int["0b11111", 0]
31
>>> int['0o37', 0]
31
>>> int['0x1f', 0]
31
7 làm cơ sở:>>> int["0b11111", 0]
31
>>> int['0o37', 0]
31
>>> int['0x1f', 0]
31
Không phân giải [tức là số nguyên] theo các cơ sở khác
Tuy nhiên, nếu động lực của bạn là có mã của riêng bạn thể hiện rõ ràng các giá trị cụ thể được mã hóa cứng, bạn có thể không cần chuyển đổi từ các cơ sở - bạn có thể để Python tự động làm điều đó với cú pháp chính xác.
Bạn có thể sử dụng tiền tố Apropos để chuyển đổi tự động cho các số nguyên với các chữ cái sau. Chúng có giá trị cho Python 2 và 3:
Nhị phân, tiền tố
>>> int["0b11111", 0]
31
>>> int['0o37', 0]
31
>>> int['0x1f', 0]
31
83.141 3.1410
Octal, tiền tố
>>> int["0b11111", 0]
31
>>> int['0o37', 0]
31
>>> int['0x1f', 0]
31
93.141 3.1411
Thập lục phân, tiền tố
3.141 3.14100
3.141 3.1412
Điều này có thể hữu ích khi mô tả các cờ nhị phân, quyền tệp trong mã hoặc giá trị hex cho màu - ví dụ: lưu ý không có báo giá:
3.141 3.1413
Làm cho Python 2 Octals mơ hồ tương thích với Python 3
Nếu bạn thấy một số nguyên bắt đầu bằng 0, trong Python 2, thì đây là cú pháp octal [không dùng nữa].
3.141 3.1414
Nó là xấu bởi vì nó trông giống như giá trị phải là
3.141 3.14101. Vì vậy, trong Python 3, bây giờ nó làm tăng một
3.141 3.14102:
3.141 3.1415
Chuyển đổi Python 2 của bạn thành Octals hoạt động trong cả 2 và 3 với tiền tố
>>> int["0b11111", 0]
31
>>> int['0o37', 0]
31
>>> int['0x1f', 0]
31
9:3.141 3.1411