我有一个文本文件,想要获取它的值,并希望将它们放在python中的数据框中。我知道我必须使用read_csv但不知道该怎么做。
文本文件如下所示:
持续时间,协议类型,服务,标志,src_bytes,dst_bytes,土地,wrong_fragment,急,热,num_failed_logins,LOGGED_IN,num_compromised,root_shell,su_attempted,num_root,num_file_creations,num_shells,num_access_files,num_outbound_cmds,is_host_login,is_guest_login,计数,srv_count ,serror_rate,srv_serror_rate,rerror_rate,srv_rerror_rate,same_srv_rate,diff_srv_rate,srv_diff_host_rate,dst_host_count,dst_host_srv_count,dst_host_same_srv_rate,dst_host_diff_srv_rate,dst_host_same_src_port_rate,dst_host_srv_diff_host_rate,dst_host_serror_rate,dst_host_srv_serror_rate,dst_host_rerror_rate,dst_host_srv_rerror_rate 0,ICMP,ecr_i,SF,1032,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,511,511,0.0,0.0,0.0, 0.0,1.0,0.0,0.0,255,255,1.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0
其中所有字符串都是我想要用于行的字符串,其余字符串是值。我在anaconda中使用pandas,文本文件名为kddcup.txt。如果有人可以帮助我,我将不胜感激。谢谢!
答案 0 :(得分:0)
试试这个。文件路径是文件所在的位置,相对路径。
import pandas as pd
data = pd.read_csv('path_to_file.csv', sep=',')