写一段python代码,要求在导入一个csv文件,可以将其中的数据以四个时间段(6:00~7:30,7:30~15:30,15:00~17:00,17:00~6:00)区分,统计四个方向(东南西北)的区别下所出现的次数,
时间: 2024-09-06 17:04:57 浏览: 41
```python
import csv
from collections import defaultdict
def count_directions(file_path):
time_periods = [("6:00", "7:30"), ("7:30", "15:30"), ("15:00", "17:00"), ("17:00", "24:00"), ("00:00", "6:00")]
directions = ["东", "南", "西", "北"]
counts = defaultdict(lambda: defaultdict(int))
with open(file_path, newline='', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
next(reader) # 跳过表头
for row in reader:
time, direction = row[0], row[1]
for i, (start, end) in enumerate(time_periods):
if start <= time < end:
counts[i][direction] += 1
break
return counts
file_path = "your_csv_file.csv"
result = count_directions(file_path)
for period, direction_counts in result.items():
print(f"时间段 {period + 1}:")
for direction, count in direction_counts.items():
print(f"{direction}: {count}次")
```
阅读全文