1:统计list中相同的个数,并按大小排序。

original_list = ['a', 'b', 'b', 'a', 'd', 'd', 'b', 'z', 'c', 'b', 'r', 's', 'h', 'f', 'f', 's', 'b', 'b', 'y', 'b']
counter = {}
for i in original_list:
if original_list.count(i) > 1:
counter[i] = original_list.count(i) # 按字典的值,进行从大到排序
counter_list = list(zip(counter.values(), counter.keys()))
counter1_list = sorted(counter_list, reverse=True) # # 按字典的键,进行从大到排序
# counter1 = sorted(counter.items(), key=lambda x: x[0], reverse=True) for counter1 in counter1_list:
print(counter1)

2:读取excel中某一个sheet中的每一行数据。

import xlrd
excel_path = r'********'
data = xlrd.open_workbook(excel_path).sheet_by_name("sheet1")
n_rows = data.nrows # 行数
for i in range(0, n_rows):
# 每一行数据
row_values = data.row_values(i)
print(row_values)

3:存储数据到excel。

import pandas as pd
dict_content_list = [{'a': '', 'b': ''}, {'a': '', 'b': ''}]
data = pd.DataFrame(dict_content_list)
data.to_excel('./***.xls', encoding='utf8', index=False)

 4:统计list中相同元素的个数,并从大到小排序。

a_list = ['', '', '', '', '', '', '', '']
counter = {}
for i in a_list:
if a_list.count(i) > 0:
counter[i] = a.count(i) # 按字典的值,进行从大到排序
counter_list = list(zip(counter.values(), counter.keys()))
counter1_list = sorted(counter_list, reverse=True)
# # 按字典的键,进行从大到排序
# counter1 = sorted(counter.items(), key=lambda x: x[0], reverse=True) for counter1 in counter1_list:
print(counter1)

5:存储数据到CSV。

import csv
test_list = ['a', 'b', 'c']
with open(f'test.csv', "a+", encoding='utf-8', newline='') as f:
writer = csv.writer(f, dialect="excel")
writer.writerow(test_list)

6:读取excel数据存储到mongodb。

import pymongo
import xlrd
client = pymongo.MongoClient(host='192.168.0.193', port=27017)
db_storage = client.liuxue['liuxue']
excel_path = r'C:\Users\Administrator\Desktop\留学.xlsx'
data = xlrd.open_workbook(excel_path).sheets()[0]
# 读取excel第一行数据作为存入mongodb的字段名
rows_tag = data.row_values(0)
n_rows = data.nrows
for i in range(1, n_rows):
# 将字段名和excel数据存储为字典形式
result_dict = dict(zip(rows_tag, data.row_values(i)))
print(result_dict)
db_storage.insert_one(result_dict)

7:字典键获值按大小排序。

counter = {}
# 按字典的值,进行从大到排序
counter_list = list(zip(counter.values(), counter.keys()))
counter1_list = sorted(counter_list, reverse=True)
# 按字典的键,进行从大到排序
counter1 = sorted(counter.items(), key=lambda x: x[0], reverse=True)

Python 常用的操作文件代码的更多相关文章

  1. Python常用时间操作总结【取得当前时间、时间函数、应用等】转载

    Python常用时间操作总结[取得当前时间.时间函数.应用等] 转载  2017-05-11   作者:清风乐逍遥    我要评论 这篇文章主要介绍了Python常用时间操作,包括取得当前时间.时间函 ...

  2. python学习笔记 操作文件和目录

    如果我们要操作文件.目录,可以在命令行下面输入操作系统提供的各种命令来完成.比如dir.cp等命令. 如果要在Python程序中执行这些目录和文件的操作怎么办?其实操作系统提供的命令只是简单地调用了操 ...

  3. python基础知识---操作文件

    一.打开文件  open()函数 open函数返回一个文件对象. 用法:open('文件名','模式') 打开文件的模式有: r,只读模式(默认). w,只写模式.[不可读:不存在则创建:存在则删除内 ...

  4. 利用Python 脚本生成 .h5 文件 代码

    利用Python 脚本生成 .h5 文件 import os, json, argparse from threading import Thread from Queue import Queue ...

  5. Python常用的数据文件存储的4种格式(txt/json/csv/excel)及操作Excel相关的第三方库(xlrd/xlwt/pandas/openpyxl)(2021最新版)

    序言:保存数据的方式各种各样,最简单的方式是直接保存为文本文件,如TXT.JSON.CSV等,除此之外Excel也是现在比较流行的存储格式,通过这篇文章你也将掌握通过一些第三方库(xlrd/xlwt/ ...

  6. python,os操作文件,文件路径(上一级目录)

    python获取文件上一级目录:取文件所在目录的上一级目录 os.path.abspath(os.path.join(os.path.dirname('settings.py'),os.path.pa ...

  7. python常用字符串操作

    #!/usr/bin/env python name='cunzhang' print(name.capitalize())#首字母大写 print(name.count('n'))#统计字符有几个 ...

  8. Python OS模块操作文件和目录

    #-*-coding:utf-8-*- import os import shutil ###############OS模块############## #获得当前python脚本的工作目录 os. ...

  9. Python常用目录操作(Python2)

    Python获取当前路径   Python查看指定路径下的文件和文件夹 Python修改当前工作目录(在读取文件等时需要) Python添加import路径(有时候为了import自己写的py文件,且 ...

随机推荐

  1. humlbe bundle如何绑定二次验证码_虚拟MFA_两步验证_谷歌身份验证器?

    一般点账户名——设置——安全设置中开通虚拟MFA两步验证 具体步骤见链接 humlbe bundle如何绑定二次验证码_虚拟MFA_两步验证_谷歌身份验证器? 二次验证码小程序于谷歌身份验证器APP的 ...

  2. 面试题四十三:在1~n整数中1出现的次数

    方法一:直观来看,遍历1到n,每个数去做%10的循环判断 int Number1_B_1toN( int n){ int sum=0; for(int i=1;i<=n;i++){ int k= ...

  3. seaborn分类数据可视化:散点图|箱型图|小提琴图|lv图|柱状图|折线图

    一.散点图stripplot( ) 与swarmplot() 1.分类散点图stripplot( ) 用法stripplot(x=None, y=None, hue=None, data=None, ...

  4. java JDBC自我总结

    preparedstatement和statement的区别 当不需要预编译时(不需要占位符)可以选用statement,存在不安全 当有占位符(?)时,需要选用preparedstatement s ...

  5. c++ string类型举例(递归预习的边界)

    #include <iostream> #include <string> using namespace std; int main ( ) { string str; // ...

  6. HTML <html> xmlns 属性

    实例 一个简单的 XHTML 文档,带有最少的必需标签: <html xmlns="http://www.w3.org/1999/xhtml"><head> ...

  7. PHP wordwrap() 函数

    实例 按照指定长度对字符串进行折行处理: <?php高佣联盟 www.cgewang.com$str = "An example of a long word is: Supercal ...

  8. Phantomjs实现后端生成图片文件

    目录 PhantomJS简介 了解rasterize.js 使用方法 今天,给大家分享一个Java后端利用Phantomjs实现生成图片的功能,同学们使用的时候,可以参考下! PhantomJS简介 ...

  9. [转]Nginx介绍-反向代理、负载均衡

    原文:https://www.cnblogs.com/wcwnina/p/8728391.html 作者:失恋的蔷薇 1. Nginx的产生 没有听过Nginx?那么一定听过它的"同行&qu ...

  10. QComboBox设置下拉item大小

    1.首先给ComboBox设置view: ui->comboBox->setView(new QListView()); 注意:这一句需要放在在所在界面的构造函数里,否则不生效. 2.给c ...