Python根据坐标半径生成测试点数据
一、代码
# -*- coding:UTF-8 -*- import csv import random import math import datetime from faker import Faker # 定义语言 faker_data = Faker(locale='zh_CN') # 获取当前时间 current_time = datetime.datetime.now() # 格式化时间 formatted_time = current_time.strftime("%H%M%S") # 文件名 bs_name = "litdata_" + str(formatted_time) + ".csv" def gen_data(): # 指定坐标点和半径 center_x = 103.827703 center_y = 36.064076 radius = 0.3 # 控制随机生成的点数 num_points = 10 # 存储点的坐标 points = [] for _ in range(num_points): # 在指定半径内生成随机坐标 angle = random.uniform(0, 2 * math.pi) distance = random.uniform(0, radius) x = center_x + distance * math.cos(angle) y = center_y + distance * math.sin(angle) address = faker_data.address() company = faker_data.company() tag = "Lit" # 添加到列表中 points.append([x, y, address, company, tag]) # 导出为CSV文件 with open(bs_name, mode='w', newline='') as file: writer = csv.writer(file) try: writer.writerow(['xy', 'address', 'company', 'tag']) # 写入表头 for point in points: # writer.writerow([point[0], point[1], point[2], point[3], point[4]]) # 写入坐标以及其它数据 writer.writerow([point[0:2], point[2], point[3], point[4]]) # 写入坐标以及其它数据 print("导出为CSV文件成功") except: print("导出CSV文件失败") if __name__ == '__main__': gen_data()
二、执行结果