# 比赛问卷不够？Python来凑！

AI悦创原创Python问卷调查办公自动化问卷调查数据PythonPython问卷调查办公自动化问卷调查数据Python大约 17 分钟...约 5151 字

💬聊天记录

Why？

## 3. 实现

### 3.1 序号的实现

``````# 序号
serial_number = list(range(1000))
print(serial_number)
``````

### 3.2 交答卷时间

1.0
``````# 提交答卷时间
from datetime import datetime, timedelta

def generate_random_dates(start_date, end_date, num_dates):
date_format = "%Y/%m/%d %H:%M:%S"
start_datetime = datetime.strptime(start_date, date_format)
end_datetime = datetime.strptime(end_date, date_format)
delta = end_datetime - start_datetime

if delta.total_seconds() <= 0:
raise ValueError("End date must be later than start date.")

random_dates = set()
while len(random_dates) < num_dates:
random_interval = random.randint(0, int(delta.total_seconds()))
random_datetime = start_datetime + timedelta(seconds=random_interval)

formatted_dates = [date.strftime(date_format) for date in random_dates]
return formatted_dates

start_date = "2023/07/13 00:00:00"
end_date = "2023/07/19 23:59:59"
num_dates = 100

random_dates = generate_random_dates(start_date, end_date, num_dates)
lst = []
for date in random_dates:
lst.append(date)

print(len(set(lst)) == len(lst))  # 证明是否有重复的日期，True 则全为真
``````

### 3.3 填写问卷所用时间

``````# 所用时间
import random

filling_time = random.randint(40, 80)
print(f"{filling_time}秒")
``````

### 3.4 来源

``````# 来源
source_origins = ["微信", "手机提交"]
random.shuffle(source_origins)
print(source_origins)
``````

### 3.5 来源详情

``````# 来源详情
source_detail = ["N/A", "直接访问"]
print(source_detail)
``````

``````source_origins = ["微信", "手机提交"]
random.shuffle(source_origins)
r = source_origins[0]

# 来源详情
source_detail = {"微信": "N/A", "手机提交": "直接访问"}
print(source_detail[r])
``````

### 3.6 IP 来自 IP

#### 3.6.1 寻找数据源

• 查询虽然是中国大陆真实 ip，但是没有对应的地理位置的区分，要是一个个 copy 查询的话简直就太酸爽了；
• 我们知道范围就能生成，但是需要 IP 和地区都要，所以弃之～

1. 点进去并输入

1. 右键查看网页源代码

#### 3.6.4 批量 IP 生成

1.0
``````def generate_ip_range(start_ip, end_ip):
start = list(map(int, start_ip.split('.')))
end = list(map(int, end_ip.split('.')))
ip_range = []

while start <= end:
ip = '.'.join(map(str, start))
ip_range.append(ip)
start[3] += 1

for i in range(3, 0, -1):
if start[i] == 256:
start[i] = 0
start[i - 1] += 1

return ip_range

ip_ranges = [
("183.128.0.0", "183.159.255.255", 3),
# ("36.96.0.0", "36.127.255.255", 3),
("115.192.0.0", "115.223.255.255", 3),
# # ... (将所有IP范围添加到此列表中)
("203.93.140.0", "203.93.140.255", 3)
]

ip_list = []

for start_ip, end_ip, count in ip_ranges:
ips = generate_ip_range(start_ip, end_ip)
ip_list.extend(ips[:count])

# 打印IP列表
for idnex, ip in enumerate(ip_list):
# print(ip)
# ip_search(ip)
print(f"{ip}({ip_search(ip)})")
# 223.104.164.115(浙江-嘉兴)
# 将IP列表保存到文件
# with open('ip_list.txt', 'w') as file:
#     for ip in ip_list:
#         file.write(ip + '\n')
``````

### 3.7 性别

``````# 您的性别是
genders = ["男", "女"]
random.shuffle(genders)
print(genders)
``````

### 3.7 年龄

1.0
``````# 您的年龄
# ages = ["22岁以下", "22-45岁", "45-60岁", "60岁以上"]
ages1 = ["22岁以下"] * 6
ages2 = ["22-45岁"] * 3
ages3 = ["45-60岁"] * 1
ages4 = ["60岁以上"] * 1
ages = ages1 + ages2 + ages3 + ages4
random.shuffle(ages)
print(ages)
``````

### 3.8 您是否看过梁弄镇明湖村的宣传视频？

1.0
``````# 您是否看过梁弄镇明湖村的宣传视频？
judgment_vide = ["是", "否"]
random.shuffle(judgment_vide)
print(judgment_vide)
``````

### 3.9 您会为采摘樱桃或杨梅，专程出远门到樱桃园和杨梅山上吗?

1.0
``````# 您会为采摘樱桃或杨梅，专程出远门到樱桃园和杨梅山上吗?
judgment_yanmei = ["会", "如果是周边地区才会考虑", "不会"]
random.shuffle(judgment_yanmei)
print(judgment_yanmei)
``````

### 3.10 在采摘杨梅和樱桃后，您是否愿意购买礼盒装的杨梅或樱桃作为伴手礼带回家中？

1.0
``````# 5、在采摘杨梅和樱桃后，您是否愿意购买礼盒装的杨梅或樱桃作为伴手礼带回家中？
``````

### 3.11 您在考虑杨梅或樱桃采摘的同时，会注重当地其他文创产品吗？

``````import random

def generate_wenchuang_data(n):
willing = ["会"] * int(n * 0.8) # 70%的数据为"会"
not_willing = ["不会"] * (n - len(willing)) # 剩下的数据为"不会"

wenchuang_data = willing + not_willing
random.shuffle(wenchuang_data) # 将数据随机排序
return wenchuang_data

wenchuang_data = generate_wenchuang_data(100)
print(wenchuang_data)
``````

### 3.12 如果在四明山上建设帐篷民宿，您愿意花钱留在山中帐篷民宿中过夜吗？

``````import random

def generate_minsu_data(n):
willing = ["愿意"] * int(n * 0.8)
not_willing = ["不愿意"] * (n - len(willing)) # 剩下的数据为"不愿意"

minsu_data = willing + not_willing
random.shuffle(minsu_data) # 将数据随机排序
return minsu_data

minsu_data = generate_minsu_data(100)
print(minsu_data)
``````

### 3.13 如果您要当乡镇旅游，以下哪个项目较吸引您呢？

``````import random

def generate_tourist_activities():
activities = ["水果采摘", "山腰处帐篷民宿", "网红打卡点", "山地越野车", "青团等当地特色小吃", "徒步远足",
"情景剧本杀"]
# 随机选择的活动数量
num_activities = random.randint(1, len(activities))
# 随机选择活动
selected_activities = random.sample(activities, num_activities)
# 将活动组合成一个字符串
itinerary = '┋'.join(selected_activities)
return itinerary

# 生成随机旅游活动组合
print(generate_tourist_activities())
``````

## 4. 汇总代码

``````import random
import time
import os
import openai
from submit_time import generate_random_dates
from age import generate_age_data
from data import generate_judgment_data_video, generate_trip_decision_data_yanmei, generate_gift_box_data, \
generate_wenchuang_data, generate_minsu_data, generate_tourist_activities
import xlwt
from ip_engin import ip_search, ip_info

openai.api_key = os.getenv("KEY")
total_num = 100
# 序号
# serial_number = list(range(1, total_num + 1))
# print(serial_number)

# 提交时间
submit_dates = generate_random_dates(start_date="2023/07/13 05:00:00", end_date="2023/07/19 23:59:59",
num_dates=total_num)

def openai_ip(ip):
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "你会把收到的信息，翻译成中文"},
{"role": "user", "content": f"{ip}"}
]
)
return completion.choices[0].message["content"]

# 填写问卷所用的时间
def filling_time():
return f"{random.randint(40, 80)}秒"

# 来源
def source_origins():
source_origins = ["微信", "手机提交"]
random.shuffle(source_origins)
return random.choice(source_origins)

# 来源详情
source_detail = {"微信": "N/A", "手机提交": "直接访问"}
# r = source_origins()
# print(source_detail[r])

# ip 地址
ip_list = open("ip_test/ip_list.txt", "r")

# print(ip_lst)

# 你的性别
def gender():
genders = ["男", "女"]
random.shuffle(genders)
return random.choice(genders)

ages = generate_age_data(total_num)
# print(ages)

# video
videos = generate_judgment_data_video(total_num)
# print(videos)

# 樱桃、杨梅
judgment_yanmei = generate_trip_decision_data_yanmei(total_num)
# print(judgment_yanmei)

wenchuang_data = generate_wenchuang_data(total_num)
# print(wenchuang_data)
minsu_data = generate_minsu_data(total_num)
# print(minsu_data)

# 生成随机旅游活动组合
# print(generate_tourist_activities())

# save excel
"序号",
"提交答卷时间",
"所用时间",
"来源",
"来源详情",
"来自IP",
"1、您的性别是",
"2、您的年龄",
"3、您是否看过梁弄镇明湖村的宣传视频？",
"4、您会为采摘樱桃或杨梅，专程出远门到樱桃园和杨梅山上吗？",
"5、在采摘杨梅和樱桃后，您是否愿意购买礼盒装的杨梅或樱桃作为伴手礼带回家中？",
"6、您在考虑杨梅或樱桃采摘的同时，会注重当地其他文创产品吗？",
"7、如果在四明山上建设帐篷民宿，您愿意花钱留在山中帐篷民宿中过夜吗？",
"8、如果您要当乡镇旅游，以下哪个项目较吸引您呢？",
]
wb = xlwt.Workbook()

for row in range(1, total_num):
sheet.write(row, 0, row)
sheet.write(row, 1, submit_dates[row])
sheet.write(row, 2, filling_time())
# 来源
source_origin = source_origins()
sheet.write(row, 3, source_origin)
sheet.write(row, 4, source_detail[source_origin])
# ip = random.choice(ip_lst)
time.sleep(6)
ip_d = ip_info(random.choice(ip_lst).strip())
ip = openai_ip(ip_d)
print(ip)
sheet.write(row, 5, ip)
sheet.write(row, 6, gender())
sheet.write(row, 7, ages[row])
sheet.write(row, 8, videos[row])
sheet.write(row, 9, judgment_yanmei[row])
sheet.write(row, 11, wenchuang_data[row])
sheet.write(row, 12, minsu_data[row])
sheet.write(row, 13, generate_tourist_activities())
wb.save("潜在旅客视角谈宁波余姚梁弄镇明湖村旅游规划调查问卷.xls")
``````

AI悦创·编程一对一

AI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Linux、Web 全栈」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

C++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

• 0
• 0
• 0
• 0
• 0
• 0

• 按正序
• 按倒序
• 按热度