N
N
netor4ite2020-04-30 18:40:08
Python
netor4ite, 2020-04-30 18:40:08

Problem with PYTHON strings?

I work with api.vk, the text from the last message is written to the variable, you need to get the balance:
H1WWx.jpg
Help, how to do this. It turns out that I need to get the numbers from the second line.

+code and exit
p1jVJ.jpg

import vk
import time
import random

token = "" #Сюда вводим свой токен.

session = vk.Session(access_token = token)
api = vk.API(session, v = "5.95")

a=api.messages.getHistory(peer_id=-173401327, offset=0, count=1)
bal = a['items'][0]['text']
print(bal)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
YariKartoshe4ka, 2020-04-30
@YariKartoshe4ka

# Срезом определите ограничение числа
bal = int(a['items'][0]['text'].replace(',' '')[:7])
print(bal)

I
Igor, 2020-04-30
@DMGarikk

bal.split(":") will help you
ps and then yourself

O
o5a, 2020-04-30
@o5a

You can sequentially cut through split () with different conditions. Or use regexp.

s = """
Балансы:

Баланс $: 228,951
Баланс котяток: 0.67984234"""

import re

m = re.search(r"Баланс \$: (.+)", s)
if m:
    print(m.group(1))
    print(m.group(1).replace(',',''))

We get:
228.951
228951

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question