from datetime import datetime users={} for i in range(4): users_id=random.randint(0,10) users_score=random.randint(-1000,1000) users[users_id]=users_score with open('updates,csv','a')as f: csv_re=csv.writer(f) csv_re.writer
时间: 2024-03-14 08:45:03 浏览: 81
It seems like your code is incomplete and there is an error in the last line. It should be `csv_re.writerow([users_id, users_score])` instead of `csv_re.writer`. Also, you need to import the `random` module to use the `randint` function. Here's the corrected code:
```python
import csv
import random
from datetime import datetime
users = {}
for i in range(4):
users_id = random.randint(0, 10)
users_score = random.randint(-1000, 1000)
users[users_id] = users_score
with open('updates.csv', 'a') as f:
csv_re = csv.writer(f)
for user_id, user_score in users.items():
csv_re.writerow([user_id, user_score, datetime.now()])
```
This code generates 4 random user IDs and scores, adds them to a dictionary, and writes them to a CSV file along with the current timestamp. Each row in the CSV file will have three columns: `user_id`, `user_score`, and `timestamp`.
阅读全文