获取gitlab上项目列表过程及脚本_xiaodaiwang的博客-CSDN博客使用Python及shell,获取gitlab上项目列表过程及脚本https://blog.csdn.net/xiaodaiwang/article/details/131781316?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22131781316%22%2C%22source%22%3A%22xiaodaiwang%22%7D前面获取了项目列表,还是从得到的data.json文件出发,提取最近更新时间。
1、因为提取时总报错,先将data.json保存为utf-8格式,保存为data_utf8.json,然后再去处理。
change.py内容
# -*- coding: utf-8 -*-
import json
import codecs
# 读取原始文件内容
with codecs.open('data.json', 'r', encoding='utf-8-sig') as file:
data = json.load(file)
# 将数据保存为UTF-8格式
with codecs.open('data_utf8.json', 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
2、改变格式后,提取项目名称及最近更新时间last_activity_at内容
Last-date.py内容:
# -*- coding: utf-8 -*-
import json
with open('data_utf8.json', 'r') as file:
data = json.load(file)
for item in data:
name = item.get('name')
last_activity_at = item.get('last_activity_at')
print("Name: {}, Last Activity: {}".format(name.encode('utf-8'), last_activity_at.encode('utf-8')))
3、运行顺序:
python change.py 得到修改了文件格式的数据:data_utf8.json
python last-date.py >name-last-date.txt 得到了包含项目名称和最新更时间的文本。
因为我想保存到Excel中,涉及到群组的内容,所以需要将项目名称和更新时间拆分。利用chaifen.py 拆分到names.txt 和 last_activities.txt 文件中。
chaifen.py内容:
# -*- coding: utf-8 -*-
with open('name-last-date.txt', 'r') as file:
lines = file.readlines()
# 分割逗号前后的内容
names = []
last_activities = []
for line in lines:
name, last_activity = line.strip().split(',')
names.append(name)
last_activities.append(last_activity)
# 将结果写入文本文件
with open('names.txt', 'w') as names_file:
for name in names:
names_file.write("{}\n".format(name))
with open('last_activities.txt', 'w') as activities_file:
for activity in last_activities:
activities_file.write("{}\n".format(activity))
print("数据已成功分开并写入到 names.txt 和 last_activities.txt 文件中")
vim names.txt
vim last_activities.txt
日期的内容太长了,利用 tiquriqi.py提取日期的前几位。
tiquriqi.py内容:
# -*- coding: utf-8 -*-
with open("last_activities.txt", "r") as file:
content = file.read()
# 使用循环查找所有日期部分并提取
dates = []
start_index = 0
while True:
# 查找下一个日期部分的起始位置和结束位置
start_index = content.find("Last Activity: ", start_index)
if start_index == -1: # 找不到日期部分时退出循环
break
start_index += len("Last Activity: ")
end_index = content.find("T", start_index)
# 提取日期部分
date_str = content[start_index:end_index]
dates.append(date_str)
start_index = end_index
# 打印输出所有日期值
for date in dates:
print(date)
python tiquriqi.py >riqi.txt
vim riqi.txt
然后粘贴到我的Excel中。
4、合并脚本:
#!/bin/bash
cd /home/test3
python change.py
python last-date.py >name-last-date.txt
python chaifen.py
python tiquriqi.py > riqi.txt
不知道为什么脚本里生成的name-last-date.txt不生效,需要单独执行一下这一句,权限,路径都没问题,奇怪。