Python的不和谐机器人Java脚本代码

2024-04-26 22:03:29 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我的java脚本机器人有一行代码,我正试图用python重新创建它!如果有人能告诉我我需要做什么的话。甚至是如何用python编写它,我会非常感激!在

const fs = require("fs");
let points = JSON.parse(fs.readFileSync("./points.json", "utf8"));

client.on("message", message => {
  if (message.author.bot) return; // always ignore bots!

  // if the points don"t exist, init to 0;
  if (!points[message.author.id]) points[message.author.id] = {
    points: 0,
    level: 0
  };
  points[message.author.id].points++;

  // And then, we save the edited file.
  fs.writeFile("./points.json", JSON.stringify(points), (err) => {
    if (err) console.error(err)
  });
});

如果可能的话,我想继续使用.json!但如果他们有更好的方法,请告诉我!<;3个


Tags: the代码脚本idjsonmessageif机器人
1条回答
网友
1楼 · 发布于 2024-04-26 22:03:29

对于python2,可以使用open打开文件。使用rw标志,具体取决于您要读还是写。例如:

with open('input.txt', 'r') as f:
    for line in f.readlines():
        print(line)
^{pr2}$

对于json,使用内置库https://docs.python.org/2/library/json.html

这个片段展示了如何将json对象写入文件

import json

# store data in a dictionary
obj = {'name': 'joe'}

# write object to an output file
with open('output.txt', 'w') as f:
    json.dump(obj, f)

相关问题 更多 >