博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python中数据类型转换
阅读量:3932 次
发布时间:2019-05-23

本文共 1851 字,大约阅读时间需要 6 分钟。

python中数据类型转换

字符串与数字之间的转换

是由数字组成的字符串。

在这里插入图片描述

字符串与列表之间的转换

1、

函数split的功能:

将字符串以一定规则切转换成列表。

函数split的用法:

string.split(sep=None,maxsplit=-1)

参数:

sep:切割的规则符号,不填写,默认空格,如字符串无空格则不分割生成列表

maxsplit:根据切割符号切割的次数,默认-1无限制。

返回值:

返回一个列表。

2、

列表转换字符串之join功能

将列表以一定的规则转换成字符串。

join函数的用法:

‘sep’.join(iterable)

参数:

sep:生成字符串用来分割列表每一个元素的符号。(也可以是空)

iterable:非数字类型的列表或元组或集合

返回值:

返回一个字符串。

#coding :utf-8test=['a','b','c']str='|'.join(test)print(str)

输出:

a|b|c

字符串与bytes的转换

1、什么是比特类型

  • 二进制的数据流——bytes
  • 也可以看成是一种特殊的字符串。
  • 在字符串前+b标记。
#coding :utf-8by=b'7823yjiang'print(type(by))

输出:

<class ‘bytes’>

#内置函数dir()可以返回该数据类型所有适合的函数。#coding :utf-8by=b'7823yjiang'print(type(by))print(dir(by))

输出:

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'center', 'count', 'decode', 'endswith', 'expandtabs', 'find', 'fromhex', 'hex', 'index', 'isalnum', 'isalpha', 'isascii', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

2、字符串转bytes的函数-encode

功能:

将字符串转换从比特类型

用法:

string.encode(encodeing=‘utf-8’,errors=‘strict’)

参数:

encodeing转换成的编码格式,如:ascii,gbk,默认utf-8

error:出错时的处理方法,默认为strict,直接抛弃错误,也可以选择ignore忽略错误。

返回值:

返回一个bytes类型。

3、bytes转字符串的函数–decode

功能:

将比特类型转换成字符串。

用法:

bytes.decode(encodeing=‘utf-8’,errors=‘strict’)

元组,列表,集合间的转换:

在这里插入图片描述

转载地址:http://hytgn.baihongyu.com/

你可能感兴趣的文章
深度学习 | (2) 二分类、多分类与多标签分类的区别与损失函数
查看>>
LaTex论文排版 | (21) 图表caption居中显示
查看>>
深度学习 | (4) 分类问题的Label为啥是one-hot?
查看>>
LaTex论文排版 | (22) argmax、argmin下标使用方法及任意、存在符号
查看>>
深度学习 | (5) 2分类、多分类问题评价指标以及在sklearn中的使用
查看>>
机器阅读理解 | (2) 文本问答概述
查看>>
预训练语言模型 | (1) 概述
查看>>
预训练语言模型 | (2) transformer
查看>>
预训练语言模型 | (3) Bert
查看>>
预训练语言模型 | (4) AlBert
查看>>
预训练语言模型 | (5) StructBert和RoBerta
查看>>
GNN在文本分类上的应用 | (1) TextGCN
查看>>
图神经网络 | (7) 如何理解Graph Convolutional Network(GCN)
查看>>
GNN在文本分类上的应用 | (2) Text Level Graph Neural Network for Text Classification
查看>>
GNN在文本分类上的应用 | (3) TensorGCN
查看>>
SemEval2019Task3_ERC | (1) Affect Classification in Dialogue using Attentive BiLSTMs
查看>>
SemEval2019Task3_ERC | (2) Attentive Conversation Modeling for Emotion Detection and Classification
查看>>
SemEval2019Task3_ERC | (3) Using Deep Sentiment Analysis Models and Transfer Learning for ERC
查看>>
SemEval2019Task3_ERC | (4) Emotion detection in conversations through Tweets,CNN and LSTM DNN
查看>>
Python杂谈 | (15) 使用Pycharm执行带命令行参数的脚本
查看>>