1.函数
split()
list()
list1=[ ]
list1.append(list(s.split('\t',1))
实验1
s='\twho is he\the\tme'
l1=[]
l2=[]
s1,s2=s.split('\t',1)#无strip()
l1.append(s1)
l1.append(s2) ###l1
#print(s)
print(l1)
print(l2)
['', 'who is he\the\tme']
[]
实验2
s='\twho is he \t he me\t'
l1=[]
l2=[]
l3=[]
s1,s2=s.strip().split('\t',1)
l1.append(s1)
l2.append(s2)
l3.append(list(s2)) #list()
#print(s)
print(l1)
print(l2)
print(l3)
['who is he ']
[' he me']
[[' ', 'h', 'e', ' ', 'm', 'e']]
实验3:什么形式数据揭秘
def open_file(filename, mode='r'):
return open(filename, mode, encoding='utf-8', errors='ignore')
def read_file(filename):
contents, labels = [], []
with open_file(filename) as f:
for line in f:
try:
label, content = line.strip().split('\t',1)
if content: #如果content不为空
contents.append(list(content)) #list()
labels.append(label)
except:
pass
return contents, labels,print(contents[0]),print(labels[0]),print(labels)
read_file('D:/month7.txt')
['11 ']
[' 3888']
[[' ', ' ', '3', '8', '8', '8']]
['咨', '询', '什', '么', '时', '间', '联', '系', '还', '未', '确', '认', '联', '系']
一日游问题
['一日游问题', '退款申请', '一日游问题', '退款申请', '订单确认']