parent
5cc6e84450
commit
ed68f48ffa
@ -0,0 +1,72 @@
|
||||
import random
|
||||
import os
|
||||
import pandas as pd
|
||||
|
||||
|
||||
def generate_data(level, num_samples):
|
||||
if level not in [2, 3, 4]:
|
||||
return None
|
||||
data_list = []
|
||||
for _ in range(num_samples):
|
||||
# 构建指定等级的数据
|
||||
data = str(level)
|
||||
for _ in range(321):
|
||||
data += str(random.randint(0, 9))
|
||||
data_list.append(data)
|
||||
return data_list
|
||||
|
||||
def save_data_to_txt(data, filename):
|
||||
with open(filename, 'a') as f:
|
||||
for item in data:
|
||||
f.write("%s\n" % item)
|
||||
print(f"Data saved to {filename}")
|
||||
|
||||
def multiply_lists(list1, list2):
|
||||
if len(list1) != len(list2):
|
||||
return None
|
||||
result = []
|
||||
result.append(str(list2[0]))
|
||||
for i in range(1, len(list1)):
|
||||
result.append(str(list1[i] * list2[i]))
|
||||
return "".join(result)
|
||||
|
||||
def save_data_to_txt_1(data, filename):
|
||||
try:
|
||||
with open(filename, 'a') as f:
|
||||
f.write(data + "\n")
|
||||
print(f"数据已保存到 {filename}")
|
||||
except Exception as e:
|
||||
print(f"保存数据时发生错误:{e}")
|
||||
|
||||
output_filename = "combined_data.txt"
|
||||
|
||||
level_2_data = generate_data(2, 750)
|
||||
save_data_to_txt(level_2_data, output_filename)
|
||||
|
||||
level_3_data = generate_data(3, 750)
|
||||
save_data_to_txt(level_3_data, output_filename)
|
||||
|
||||
level_4_data = generate_data(4, 500)
|
||||
save_data_to_txt(level_4_data, output_filename)
|
||||
|
||||
df = pd.read_excel('data/LG.xlsx',header=None)
|
||||
rows_as_lists = df.values.tolist()
|
||||
level2 = rows_as_lists.pop()
|
||||
level3 = rows_as_lists.pop()
|
||||
level4 = rows_as_lists.pop()
|
||||
|
||||
output_filename_1 = "python_teamwork/data/testdata/testdata.txt"
|
||||
|
||||
with open('combined_data.txt', 'r', encoding='utf-8') as f:
|
||||
data_str_list = [line.strip() for line in f]
|
||||
for i in data_str_list:
|
||||
data = list(i)
|
||||
if int(data[0]) == int(level2[0]):
|
||||
result = multiply_lists(data, level2)
|
||||
save_data_to_txt_1(result, output_filename_1)
|
||||
if int(data[0]) == int(level3[0]):
|
||||
result = multiply_lists(data, level3)
|
||||
save_data_to_txt_1(result, output_filename_1)
|
||||
if int(data[0]) == int(level4[0]):
|
||||
result = multiply_lists(data, level4)
|
||||
save_data_to_txt_1(result, output_filename_1)
|
Loading…
Reference in new issue