菜鸟自学机器学习2.Python-文件数据处理

1.简单导入txt文件并将其读取出来

f=open("F:\study2.txt","r")    #打开一个文件“r”为对文件采取读取,赋值给f

p=f.read()    #将f读取并赋值给p
print(p)        

f.close()        #将打开的文件关闭

运行结果:

1 你试试

2 hhsd

3 hello world

2.简单写入数据到txt文件中

f=open("F:\study3.txt","w")    #“w”表示对文件采取写入操作
f.write("1 eeee") 
f.write("\n")
f.write("2 aawww")
f.write("\n")
f.write("3 eeee")

f.close()

写入后结果:

1 eeee
2 aawww

3 eeee

3.读取csv格式文件,并采取分割操作

所采用的csv数据集内容如下:

0,tcp,http,SF,215,45076,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,normal.
0,tcp,http,SF,162,4528,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,1,0,0,1,1,1,0,1,0,0,0,0,0,normal.
0,tcp,http,SF,236,1228,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,2,2,1,0,0.5,0,0,0,0,0,normal.
0,tcp,http,SF,233,2032,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,1,0,0,3,3,1,0,0.33,0,0,0,0,0,normal.
0,tcp,http,SF,239,486,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,3,3,0,0,0,0,1,0,0,4,4,1,0,0.25,0,0,0,0,0,normal.
0,tcp,http,SF,238,1282,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,4,4,0,0,0,0,1,0,0,5,5,1,0,0.2,0,0,0,0,0,normal.
0,tcp,http,SF,235,1337,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,5,5,0,0,0,0,1,0,0,6,6,1,0,0.17,0,0,0,0,0,normal.
0,tcp,http,SF,234,1364,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,6,6,0,0,0,0,1,0,0,7,7,1,0,0.14,0,0,0,0,0,normal.
0,tcp,http,SF,239,1295,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,7,7,0,0,0,0,1,0,0,8,8,1,0,0.12,0,0,0,0,0,normal.
0,tcp,http,SF,181,5450,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,8,8,0,0,0,0,1,0,0,9,9,1,0,0.11,0,0,0,0,0,normal.
0,tcp,http,SF,184,124,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,10,10,1,0,0.1,0,0,0,0,0,normal.
0,tcp,http,SF,185,9020,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,1,0,0,11,11,1,0,0.09,0,0,0,0,0,normal.
0,tcp,http,SF,239,1295,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,12,12,1,0,0.08,0,0,0,0,0,normal.
0,tcp,http,SF,181,5450,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,1,0,0,13,13,1,0,0.08,0,0,0,0,0,normal.
0,tcp,http,SF,236,1228,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,3,3,0,0,0,0,1,0,0,14,14,1,0,0.07,0,0,0,0,0,normal.
0,tcp,http,SF,233,2032,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,4,4,0,0,0,0,1,0,0,15,15,1,0,0.07,0,0,0,0,0,normal.
0,tcp,http,SF,238,1282,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,5,5,0,0,0,0,1,0,0,16,16,1,0,0.06,0,0,0,0,0,normal.
0,tcp,http,SF,235,1337,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,6,6,0,0,0,0,1,0,0,17,17,1,0,0.06,0,0,0,0,0,normal.
0,tcp,http,SF,234,1364,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,7,7,0,0,0,0,1,0,0,18,18,1,0,0.06,0,0,0,0,0,normal.

0,tcp,http,SF,239,486,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,8,8,0,0,0,0,1,0,0,19,19,1,0,0.05,0,0,0,0,0,normal.


代码如下:

kddcpu_data=[]    #为分割后的数据创建一个空列表
f=open("F:\study.csv","r")
data=f.read()
rows=data.split('\n')    #以\n(一个换行符)进行分割,rows被赋值后以列表的形式储存数据
print(rows)
print("----------------------------------------------------------------------")
for row in rows:           
    kddcpu_data.append(row.split(','))  #对row以“,”进行分割并加入到kddcpu_data中
print(kddcpu_data)    

输出结果:

['0,tcp,http,SF,215,45076,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,normal.', '0,tcp,http,SF,162,4528,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,1,0,0,1,1,1,0,1,0,0,0,0,0,normal.', '0,tcp,http,SF,236,1228,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,2,2,1,0,0.5,0,0,0,0,0,normal.', '0,tcp,http,SF,233,2032,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,1,0,0,3,3,1,0,0.33,0,0,0,0,0,normal.', '0,tcp,http,SF,239,486,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,3,3,0,0,0,0,1,0,0,4,4,1,0,0.25,0,0,0,0,0,normal.', '0,tcp,http,SF,238,1282,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,4,4,0,0,0,0,1,0,0,5,5,1,0,0.2,0,0,0,0,0,normal.', '0,tcp,http,SF,235,1337,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,5,5,0,0,0,0,1,0,0,6,6,1,0,0.17,0,0,0,0,0,normal.', '0,tcp,http,SF,234,1364,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,6,6,0,0,0,0,1,0,0,7,7,1,0,0.14,0,0,0,0,0,normal.', '0,tcp,http,SF,239,1295,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,7,7,0,0,0,0,1,0,0,8,8,1,0,0.12,0,0,0,0,0,normal.', '0,tcp,http,SF,181,5450,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,8,8,0,0,0,0,1,0,0,9,9,1,0,0.11,0,0,0,0,0,normal.', '0,tcp,http,SF,184,124,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,10,10,1,0,0.1,0,0,0,0,0,normal.', '0,tcp,http,SF,185,9020,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,1,0,0,11,11,1,0,0.09,0,0,0,0,0,normal.', '0,tcp,http,SF,239,1295,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,12,12,1,0,0.08,0,0,0,0,0,normal.', '0,tcp,http,SF,181,5450,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,1,0,0,13,13,1,0,0.08,0,0,0,0,0,normal.', '0,tcp,http,SF,236,1228,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,3,3,0,0,0,0,1,0,0,14,14,1,0,0.07,0,0,0,0,0,normal.', '0,tcp,http,SF,233,2032,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,4,4,0,0,0,0,1,0,0,15,15,1,0,0.07,0,0,0,0,0,normal.', '0,tcp,http,SF,238,1282,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,5,5,0,0,0,0,1,0,0,16,16,1,0,0.06,0,0,0,0,0,normal.', '0,tcp,http,SF,235,1337,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,6,6,0,0,0,0,1,0,0,17,17,1,0,0.06,0,0,0,0,0,normal.', '0,tcp,http,SF,234,1364,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,7,7,0,0,0,0,1,0,0,18,18,1,0,0.06,0,0,0,0,0,normal.', '0,tcp,http,SF,239,486,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,8,8,0,0,0,0,1,0,0,19,19,1,0,0.05,0,0,0,0,0,normal.', '']     !!!此处多了个小尾巴

----------------------------------------------------------------------
[['0', 'tcp', 'http', 'SF', '215', '45076', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '162', '4528', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '2', '2', '0', '0', '0', '0', '1', '0', '0', '1', '1', '1', '0', '1', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '236', '1228', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '1', '0', '0', '2', '2', '1', '0', '0.5', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '233', '2032', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '2', '2', '0', '0', '0', '0', '1', '0', '0', '3', '3', '1', '0', '0.33', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '239', '486', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '3', '3', '0', '0', '0', '0', '1', '0', '0', '4', '4', '1', '0', '0.25', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '238', '1282', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '4', '4', '0', '0', '0', '0', '1', '0', '0', '5', '5', '1', '0', '0.2', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '235', '1337', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '5', '5', '0', '0', '0', '0', '1', '0', '0', '6', '6', '1', '0', '0.17', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '234', '1364', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '6', '6', '0', '0', '0', '0', '1', '0', '0', '7', '7', '1', '0', '0.14', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '239', '1295', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '7', '7', '0', '0', '0', '0', '1', '0', '0', '8', '8', '1', '0', '0.12', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '181', '5450', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '8', '8', '0', '0', '0', '0', '1', '0', '0', '9', '9', '1', '0', '0.11', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '184', '124', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '1', '0', '0', '10', '10', '1', '0', '0.1', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '185', '9020', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '2', '2', '0', '0', '0', '0', '1', '0', '0', '11', '11', '1', '0', '0.09', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '239', '1295', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '1', '0', '0', '12', '12', '1', '0', '0.08', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '181', '5450', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '2', '2', '0', '0', '0', '0', '1', '0', '0', '13', '13', '1', '0', '0.08', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '236', '1228', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '3', '3', '0', '0', '0', '0', '1', '0', '0', '14', '14', '1', '0', '0.07', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '233', '2032', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '4', '4', '0', '0', '0', '0', '1', '0', '0', '15', '15', '1', '0', '0.07', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '238', '1282', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '5', '5', '0', '0', '0', '0', '1', '0', '0', '16', '16', '1', '0', '0.06', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '235', '1337', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '6', '6', '0', '0', '0', '0', '1', '0', '0', '17', '17', '1', '0', '0.06', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '234', '1364', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '7', '7', '0', '0', '0', '0', '1', '0', '0', '18', '18', '1', '0', '0.06', '0', '0', '0', '0', '0', 'normal.'], ['0', 'tcp', 'http', 'SF', '239', '486', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '8', '8', '0', '0', '0', '0', '1', '0', '0', '19', '19', '1', '0', '0.05', '0', '0', '0', '0', '0', 'normal.'], ['']]                   !!!此处多了个小尾巴

3.取出分割好的数据中最后一列数据

cpu99=[]
kddcpu_data=[]
f=open("F:\study.csv","r")
data=f.read()
rows=data.split('\n')
for row in rows:
    kddcpu_data.append(row.split(','))
#小心有一个小尾巴    
kddcpu_data = kddcpu_data[:-1]        #因为有一个小尾巴,所以区0至倒数第二个元素进行处理
for rowss in kddcpu_data:
    cpu99.append(rowss[41])

print(cpu99)

运行结果:

['normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.', 'normal.']



<--文章写得有点烂大家见谅-->


猜你喜欢

转载自blog.csdn.net/winterredrain/article/details/80664048
今日推荐