小编Ant*_*ves的帖子

为什么我的函数用一个值代替另一个值?

我在.txt文件中有一个Vehicle数据库.每条线都有不同的车辆.每辆车都有一个x值和一个y值(均默认为2.5)每辆车的x和y应该> = 0且<= 5(并且最多有一个十进制数字).

我希望我的refresh()函数随机地为每条线(每个车辆)重新分配新的x和y坐标.

该函数有点工作,但在文本文件中,尽管每行具有不同的值,但每行的x和y值都相同.

我有类似的东西:

第1行:x = 0.8 | y = 0.8

第2行:x = 4.1 | y = 4.1

我想要的是:

第1行:x = 0.3 | y = 3.1

第2行:x = 2.4 | y = 1.7

def refresh():
    new_positions = []
    with open("Vehicles.txt", "r") as file:
        lines = file.readlines()
        for line in lines:
            old_line = line.split(" | ")
            x_to_change = old_line[14]
            y_to_change = old_line[15]
            pre_random_x = random.uniform(0, 5)
            pre_random_y = random.uniform(0, 5)
            random_x = str(round(pre_random_x, 1))
            random_y = str(round(pre_random_y, 1)) …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

3
推荐指数
1
解决办法
40
查看次数

How do I turn my JSON into something easier to read?

My Python script connects to an API and gets some JSON.
I've been trying out prettyprint, parse, loads, dumps but I haven't figured them out yet...

Right now, when i do print(request.json()) I get this:

{'info': {'status': 'OK', 'time': {'seconds': 0.050006151199341, 'human': '50 milliseconds'}},
 'datalist': {'total': 1, 'count': 1, 'offset': 0, 'limit': 3, 'next': 1, 'hidden': 0, 'loaded': True, 'list': [
     {'id': 27862209, 'name': 'Fate/Grand Order', 'package': 'com.xiaomeng.fategrandorder',
      'uname': 'komoe-game-fate-go', 'size': 49527668,
      'icon': 'http://pool.img.xxxxx.com/msi8/9b58a48638b480c17135a10810374bd6_icon.png',
      'graphic': 'http://pool.img.xxxxx.com/msi8/3a240b50ac37a9824b9ac99f1daab8c8_fgraphic_705x345.jpg',
      'added': '2017-05-20 10:54:53', 'modified': …
Run Code Online (Sandbox Code Playgroud)

python json pretty-print python-3.x python-requests

2
推荐指数
1
解决办法
43
查看次数

标签 统计

python ×2

python-3.x ×2

json ×1

pretty-print ×1

python-requests ×1