笔记 | 文件&数据读写操作

1.文件操作

  • os.path.exists() # 判断是否存在
  • os.path.join(path, file) # 形成文件路径
  • os.path.isdir(path) # 判断是否是目录
  • os.path.isfile(path) # 判断是否是文件
  • os.path.getsize(path) # 判断文件大小
  • os.rename(oldName,newName) # 文件重命名
  • shutil.copy(“oldFile”,“newFile”) # 文件复制,newFile不存在自动新建
  • shutil.move(“oldFile”,“newFile”) # 移动文件

2.原始unicode转义,输出显示汉字 decode(‘raw_unicode_escape’)

#("\u963f\u5f97\u52d2")
res = requests.post(url=url, headers=headers, data=json.dumps(data))
s = res.content
s = res.content.decode('raw_unicode_escape')
s_json = json.loads(s)
print(s_json)

3.判断是否是目录

if os.path.isdir(path):
	print(path)

4.批量读取文件:

# root:目录的路径,dirs:目录下的所有文件夹,files:目录下所有文件
for root, dirs, files in os.walk(path): # path为待读取的文件
	print(root, dirs, files)

5.批量读取文件 - 按文件名称顺序读取文件:

files=os.listdir(path)
files.sort() #对读取的路径进行排序

6.批量读取文件 - 按文件名大小读文件:

filenames=os.listdir(dir)
filenames.sort(key=lambda x:int(x[:-4])) # 将文件名提取(排除后缀,eg:1.txt)后进行排序

7.文件重命名

old_name=path + oldfile   #设置旧文件名(路径+文件名)
new_name=path + newfile	#设置新文件名
#文件重命名
os.rename(old_name,new_name)   

8.读写数据编码 encoding 报错:

# "UnicodeDecodeError: 'utf-8' codec can't decode byte"
with open(file_path, 'a+', encoding='utf8', errors='ignore') as f: # 加errors参数或尝试其他编码方式

9.xlsx文件数据读取:

from openpyxl import load_workbook

def read_xlsx():

    workbook = load_workbook(u'./百度数据集2/schema.xlsx')    #找到需要xlsx文件的位置
    booksheet = workbook.active                 #获取当前活跃的sheet,默认是第一个sheet

    #如果想获取别的sheet页采取下面这种方式,先获取所有sheet页名,在通过指定那一页。
    # sheets = workbook.get_sheet_names()  # 从名称获取sheet
    # booksheet = workbook.get_sheet_by_name(sheets[0])

    #获取sheet页的行数据
    rows = booksheet.rows
    #获取sheet页的列数据
    columns = booksheet.columns

    i = 0
    # 迭代所有的行
    for row in rows:
        print(row)
        i = i + 1
        line = [col.value for col in row]
        cell_data_1 = booksheet.cell(row=i, column=1).value               #获取第i行1 列的数据
        cell_data_2 = booksheet.cell(row=i, column=2).value               #获取第i行 2 列的数据
        cell_data_3 = booksheet.cell(row=i, column=3).value                   #获取第i行 3 列的数据
        cell_data_4 = booksheet.cell(row=i, column=4).value
        cell_data_4 = booksheet.cell(row=i, column=4).value#获取第i行 4 列的数据
        print (cell_data_1, cell_data_2, cell_data_3, cell_data_4)

10.csv文件数据读取:

f = open(input_path, 'r', encoding='utf8')
data = csv.reader(f)
for cont in data:
   	print(cont )
f_names.close()

11.list数据写入.csv文件:

f_write = open(output_path, 'a+', encoding='utf8', newline='')
csv_writer = csv.writer(f_write, dialect='excel') # dialect兼容excel
csv_writer.writerow(cont_list)	 # 按行写入	
f_write.close()

12.json数据转dict字典:

cont_dict = json.loads(cont_json)

13.dict字典转json数据:

cont_json = json.dumps(cont_dict)

14.dict写入.json文件:

f_write = open(output_path, 'a+', encoding='utf8')
#json.dumps序列化时候对中文默认使用的ascii编码,想要输出真正的中文需要指定ensure_ascii=False
json.dump(key_value_dict, f_write, ensure_ascii=False)
f_write.write('\n')
f_write.close()

15.lsit()列表/数组中重复元素的下标

target_index = [i for i,v in enumerate(l) if v==find]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值