当前位置: 首页 > 图灵资讯 > 行业资讯> 如何用python给csv里的数据排序

如何用python给csv里的数据排序

发布时间:2025-01-22 15:34:40

用python对csv中的数据进行排序的方法:1、使用argparse组件获取命令行参数和数据;2、使用re组件获取需要搜索的字符串所在行;3、使用pandas组件对文件进行数据排序、可以获取执行后的文件数据。

1、使用argparse组件获取命令行参数;使用re组件获取需要搜索的字符串所在行

2、使用pandas组件对文件进行排序。

3、命令执行数据获取和排序,并写入文件;

以下是完整代码:

#coding:utf-8
importre
importargparse
importpandasaspd
parser=argparse.ArgumentParser(description='manualtothisscript')
parser.add_argument('--ip',type=str,default=None)
parser.add_argument('--type',type=str,default=None)
args=parser.parse_args()
filterStr=args.ip+""+args.type
f1=file('perf.csv','r')
perfdata=f1.readlines()
f1.close()
results=[]
f2=open('filter.csv','w')
f2.writelines(perfdata[0])
foriinperfdata:
n=re.findall(filterStr,i)
ifn:
f2.writelines(i)
f2.close()
df=pd.read_csv('filter.csv')
df=df.sort_values('elapsed',ascending=False)
df.to_csv('filterOrder.csv',index=False)

推荐课程:数据挖掘基础(黑马程序员)

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09