小编Pre*_*ter的帖子

按键值排序JSON数据

我目前从discogs API(mp3标签数据)获取JSON数据,并希望按键的值对结果进行排序.在这种情况下,我试图获取Guns n Roses歌曲的数据,输出有1988年作为第一个,而数据实际上有1987年的记录.我如何排序这些数据,以便我可以通过以下方式获取排序数据一年(奥尔德斯特到最新).下面的代码按键或值排序,但这不是我想要的.请帮忙.

import json
import urllib2
request = urllib2.Request('http://api.discogs.com/database/search?sort=year&sort_order=asc&artist=%22Guns+N%27+Roses%22&track=%22Sweet+Child+O%27+Mine%22&format_exact=Album&type=master')
request.add_header('User-Agent','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
request.add_header('Content-Type','application/json')
response = urllib2.urlopen(request)
json_raw= response.readlines()
json_object = json.loads(json_raw[0])



for row in json_object['results']:
    try:
        from operator import itemgetter
        for k, v in sorted(row.items(), key=itemgetter(0)):
            print k, v
    except KeyError: 
        pass
Run Code Online (Sandbox Code Playgroud)

python json list discogs-api

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

从Python中的给定字符串中删除所有形式的URL

我是python的新手,想知道是否有更好的解决方案来匹配可能在给定字符串中找到的所有形式的URL.在谷歌搜索,似乎有很多解决方案提取域,用链接等替换它,但没有一个从字符串中删除/删除它们.我在下面提到了一些例子供参考.谢谢!

str = 'this is some text that will have one form or the other url embeded, most will have valid URLs while there are cases where they can be bad. for eg, http://www.google.com and http://www.google.co.uk and www.domain.co.uk and etc.'

URLless_string = re.sub(r'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|

(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))', '', thestring)

print '==' + URLless_string + '=='
Run Code Online (Sandbox Code Playgroud)

错误日志:

C:\Python27>python test.py
  File "test.py", line 7
SyntaxError: Non-ASCII character '\xab' in file test.py on line 7, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Run Code Online (Sandbox Code Playgroud)

python regex

6
推荐指数
2
解决办法
7141
查看次数

Python找到最接近的匹配句子

我正在尝试从专辑中获取曲目列表(歌曲),对于给定的曲目,我希望得到所有匹配相似的曲目.我已经提到了下面的例子,关于如何在python中继续这个的任何想法?看起来像difflib.get_close_matches只适用于单个单词而不是句子.

例如:(找到包含字符串'环游世界'的任何内容

tracks = ['Around The World (La La La La La) (Radio Version)', 'Around The World (La La La La La) (Alternative Radio Version)', 'Around The World (La La La La La) (Acoustic Mix)', 'Around The World (La La La La La) (Rucegsegger#Wittwer Club Mix)', 'World In Motion','My Heart Beats Like A Drum (Dam Dam Dam)','Thinking Of You','Why Oh Why','Mistake No. 2','With You','Love Is Blind','Lonesome Suite','Let Me Come & Let Me Go']
Run Code Online (Sandbox Code Playgroud)

输出:

 Around The World (La La La …
Run Code Online (Sandbox Code Playgroud)

python

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

Python在一个句子中清理单词

我正在尝试编写一个接受字符串(句子)的函数,然后清除它并返回所有字母,数字和一个超级.但是代码似乎有误.请知道我在这里做错了什么.

例如:Blake D'Souza是一个!d!0t
应该返回:Blake D'Souza是一个d0t

蟒蛇:

def remove_unw2anted(str):
    str = ''.join([c for c in str if c in 'ABCDEFGHIJKLNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890\''])
    return str

def clean_sentence(s):
    lst = [word for word in s.split()]
    #print lst
    for items in lst:
        cleaned = remove_unw2anted(items)
    return cleaned

s = 'Blake D\'souza is an !d!0t'
print clean_sentence(s)
Run Code Online (Sandbox Code Playgroud)

python string python-2.7

0
推荐指数
1
解决办法
1810
查看次数

标签 统计

python ×4

discogs-api ×1

json ×1

list ×1

python-2.7 ×1

regex ×1

string ×1