[英]How to remove a certain amount of characters from a string
在python中,我正在制作一个虚拟助手。 我正在研究一种方法来获取您设备的当前位置并显示该位置的天气。 获取位置工作正常。 我将该数据存储在一个变量中并打印它,但我使用的模块显示了一堆我不想要的额外信息。 我怎样才能修剪掉多余的字符,让它只是城市? 输出是
Python: The current weather for <[OK] Ipinfo - Geocode [mycity, mystate, US]> is .
我的代码是
# GeoCoder
import geocoder
g = geocoder.ip('me')
print(g)
# import modules necessary to request data from weather api
import requests
import json
print("""
Welcome to Python Assistant.
Check the included documentation for a list of requests.
You can start by typing in a request.
""")
while True:
text = input("$PyASSIST > ")
# HELP
if(text.upper() == "HELP"):
print("Python: Check the documentation for help.")
# HELLO
elif(text.upper() == "HELLO") or (text.upper() == "HI"):
print("Python: Hi! How are you today?")
elif(text.upper() == "TEST") or (text.upper() == "TEST2"):
print("Python: The current weather for " + str(g) + " is " + ".")
else:
print("Python: I don't know how to respond to that.")
编辑:几乎不小心把自己搞砸了,那会很糟糕
可以试试这个吗?
myString = 'Python: The current weather for <[OK] Ipinfo - Geocode [mycity, mystate, US]> is .'
myString = myString.split('[')[2].split(',')[0]
输出:
mycity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.