1 Star 1 Fork 0

heyuanqing007/AlgorithmsLibraryHYQ

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ComToolsHyq.py 1.87 KB
一键复制 编辑 原始数据 按行查看 历史
heyuanqing007 提交于 2022-10-27 07:52 . update ComToolsHyq.py.
# @Time: 2022/10/20
# @Author: HeYuanqing
import os
import csv
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# 读取csv文件
def read_csv(file_path):
data_list = []
with open(file_path) as f:
f_csv = csv.reader(f)
# 获取数据头
headers = next(f_csv)
data_list.append(headers)
# 循环获取每一行
for row in f_csv:
data_list.append(row)
print("csv文件:" + file_path + " 读取成功!")
return data_list
# 写csv文件
def write_csv(dir_name, file_name, file_data):
if not os.path.isdir(dir_name):
os.makedirs(dir_name)
file_path = dir_name + "\\" + file_name
height = len(file_data)
if height < 0:
print("写csv文件失败!")
return
width = len(file_data[0])
if width < 0:
print("写csv文件失败!")
return
with open(file_path, 'w', newline='', encoding='utf-8') as file_csv:
writer_csv = csv.writer(file_csv)
# 按行写入
writer_csv.writerows(file_data)
file_csv.close()
print("csv文件:" + file_path + " 写入成功!")
# 读取json文件
def read_json(file_path):
import json
f = open(file_path, 'r')
data = json.load(f)
f.close()
return data
# 获取文件夹下的文件名
def get_filename(rootpath):
files = []
if os.path.exists(rootpath):
for file_name in os.listdir(rootpath):
files.append(file_name)
else:
print("路径:" + rootpath + " 不存在!")
return files
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/heyuanqing007/algorithms-library-hyq.git
git@gitee.com:heyuanqing007/algorithms-library-hyq.git
heyuanqing007
algorithms-library-hyq
AlgorithmsLibraryHYQ
master

搜索帮助