Thứ nhất, try
/
>>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
0 không phải là chức năng, mà là các câu lệnh.Để chuyển đổi một chuỗi [hoặc bất kỳ loại nào khác có thể được chuyển đổi] thành một số nguyên trong Python, chỉ cần gọi hàm tích hợp
>>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
1. >>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
1 sẽ >>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
3 A >>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
4 nếu nó thất bại và bạn nên bắt điều này cụ thể:Trong Python 2.x:
>>> for value in '12345', 67890, 3.14, 42L, 0b010101, 0xFE, 'Not convertible':
... try:
... print '%s as an int is %d' % [str[value], int[value]]
... except ValueError as ex:
... print '"%s" cannot be converted to an int: %s' % [value, ex]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
Trong Python 3.x
Cú pháp đã thay đổi một chút:
>>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
Trong hướng dẫn này, chúng tôi sẽ học cách chuyển đổi chuỗi Python sang int và int thành chuỗi trong Python. Trong hướng dẫn trước đây của chúng tôi, chúng tôi đã tìm hiểu về chức năng Danh sách Python.
Chuỗi python đến int
Nếu bạn đọc các hướng dẫn trước đây của chúng tôi, bạn có thể nhận thấy rằng vào một lúc nào đó chúng tôi đã sử dụng chuyển đổi này. Trên thực tế, điều này là cần thiết trong nhiều trường hợp. Ví dụ: bạn đang đọc một số dữ liệu từ một tệp, sau đó nó sẽ ở định dạng chuỗi và bạn sẽ phải chuyển đổi chuỗi thành int. Bây giờ, chúng tôi sẽ đi thẳng vào mã. Nếu bạn muốn chuyển đổi một số được biểu diễn trong chuỗi thành int, bạn phải sử dụng hàm
>>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
1 để làm như vậy. Xem ví dụ sau:num = '123' # string data
# print the type
print['Type of num is :', type[num]]
# convert using int[]
num = int[num]
# print the type again
print['Now, type of num is :', type[num]]
Đầu ra của mã sau sẽ
Type of num is :
Now, type of num is :
Python chuỗi vào intChuyển đổi chuỗi thành int từ cơ sở khác nhau
Nếu chuỗi bạn muốn chuyển đổi thành INT thuộc về số cơ sở số khác nhau mà cơ sở 10, bạn có thể chỉ định cơ sở để chuyển đổi. Nhưng hãy nhớ rằng số nguyên đầu ra luôn ở gốc 10. Một điều khác bạn cần nhớ là cơ sở đã cho phải ở trong khoảng từ 2 đến 36. Xem ví dụ sau để hiểu chuyển đổi chuỗi sang INT với đối số cơ sở.
num = '123'
# print the original string
print['The original string :', num]
# considering '123' be in base 10, convert it to base 10
print['Base 10 to base 10:', int[num]]
# considering '123' be in base 8, convert it to base 10
print['Base 8 to base 10 :', int[num, base=8]]
# considering '123' be in base 6, convert it to base 10
print['Base 6 to base 10 :', int[num, base=6]]
Đầu ra của mã sau sẽ
Type of num is :
Now, type of num is :
Python chuỗi vào intChuyển đổi chuỗi thành int từ cơ sở khác nhau
Nếu chuỗi bạn muốn chuyển đổi thành INT thuộc về số cơ sở số khác nhau mà cơ sở 10, bạn có thể chỉ định cơ sở để chuyển đổi. Nhưng hãy nhớ rằng số nguyên đầu ra luôn ở gốc 10. Một điều khác bạn cần nhớ là cơ sở đã cho phải ở trong khoảng từ 2 đến 36. Xem ví dụ sau để hiểu chuyển đổi chuỗi sang INT với đối số cơ sở.base=16 in the int[] function. It will raise a
>>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
4 exception if there is any digit that does not belong to the decimal number system. The following example will illustrate this exception while converting a string to int."""
Scenario 1: The interpreter will not raise any exception but you get wrong data
"""
num = '12' # this is a hexadecimal value
# the variable is considered as decimal value during conversion
print['The value is :', int[num]]
# the variable is considered as hexadecimal value during conversion
print['Actual value is :', int[num, base=16]]
"""
Scenario 2: The interpreter will raise ValueError exception
"""
num = '1e' # this is a hexadecimal value
# the variable is considered as hexadecimal value during conversion
print['Actual value of \'1e\' is :', int[num, base=16]]
# the variable is considered as decimal value during conversion
print['The value is :', int[num]] # this will raise exception
Python chuyển đổi chuỗi thành int với cơ sở
ValueError Khi chuyển đổi chuỗi thành intTrong khi chuyển đổi từ chuỗi sang int, bạn có thể nhận được ngoại lệ >>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
4. Ngoại lệ này xảy ra nếu chuỗi bạn muốn chuyển đổi không đại diện cho bất kỳ số nào. Giả sử, bạn muốn chuyển đổi một số thập lục phân thành một số nguyên. Nhưng bạn đã không vượt qua Base Agunce = 16 trong hàm int []. Nó sẽ tăng một ngoại lệ >>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
4 nếu có bất kỳ chữ số nào không thuộc hệ thống số thập phân. Ví dụ sau đây sẽ minh họa ngoại lệ này trong khi chuyển đổi một chuỗi thành int.
>>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
>>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
Đầu ra của mã trên sẽ là:
hexadecimalValue = 0x1eff
print['Type of hexadecimalValue :', type[hexadecimalValue]]
hexadecimalValue = str[hexadecimalValue]
print['Type of hexadecimalValue now :', type[hexadecimalValue]]
The value is : 12
Actual value is : 18
Actual value of '1e' is : 30
Traceback [most recent call last]:
File "/home/imtiaz/Desktop/str2int_exception.py", line 22, in
print['The value is :', int[num]] # this will raise exception
ValueError: invalid literal for int[] with base 10: '1e'
Chuỗi Python để int valueErrorPython int để chuỗiChuyển đổi INT thành chuỗi không cần nỗ lực hoặc kiểm tra. Bạn chỉ cần sử dụng hàm
>>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
... try:
... print['%s as an int is %d' % [str[value], int[value]]]
... except ValueError as ex:
... print['"%s" cannot be converted to an int: %s' % [value, ex]]
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int[] with base 10: 'Not convertible'
8 để thực hiện chuyển đổi. Xem ví dụ sau.