我有一个数据库,其中包含以28种语言出版的赞美诗中的赞美诗标题。当我输入标题时,我研究了每种语言中哪些unicode字符最正确(例如,汤加声门停止应该是U + 02BB,即使它看起来像撇号;同样,在罗马尼亚语中,U + 021A(ţ)比U + 0163(ţ)等更正确。)
现在我正在开展一个类似的项目,我想回去通过用一种语言收集所有标题并输出标题中使用的所有独特字符的列表来“分解”我的研究。
有没有办法用MySQL和/或Python做到这一点?我正在考虑在每个角色之间分割一个字符串,命令所有角色,并将它们组合在一起。我的网站是用Python编写的,但它都是非常基本的编码(我还不太先进)。
编辑:这就是我的代码最终结果,感谢这些回复,并且效果很好!
hymnstitleslist = lookup('''
SELECT HyName FROM Hymns
WHERE HymnbookID = "'''+hbid+'''"
''')
import string
from collections import Counter
some_text = ""
for x in range(0, len(hymnstitleslist)):
some_text = some_text+hymnstitleslist[x]['HyName']
letters = []
for i in some_text:
letters.append(i)
letter_count = Counter(letters)
for letter,count in letter_count.iteritems():
print "{}: {}".format(letter,count)
答案 0 :(得分:2)
我正在考虑在每个角色之间分割一个字符串, 命令所有角色,并将它们组合在一起。
这部分很容易完成:
import string
from collections import Counter
some_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque fringilla augue ac metus laoreet quis imperdiet velit congue. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Quisque tincidunt lorem ut justo fermentum adipiscing. Nullam ullamcorper eros in arcu tincidunt non scelerisque ligula molestie. Vestibulum imperdiet facilisis nisi, et sodales leo sodales at. In hac habitasse platea dictumst."
letters = []
for i in some_text:
# Each "i" is a letter or space
if i in string.letters:
# only collect letters, not punctuation marks or spaces
letters.append(i)
# count how many of each
letter_count = Counter(letters)
# For each letter, print the count:
for letter,count in letter_count.iteritems():
print "{}: {}".format(letter,count)
这会给你:
C: 1
I: 1
L: 1
N: 1
Q: 1
P: 1
V: 2
a: 24
c: 19
b: 5
e: 44
d: 10
g: 6
f: 4
i: 44
h: 2
j: 1
m: 17
l: 27
o: 17
n: 18
q: 4
p: 10
s: 32
r: 19
u: 34
t: 31
v: 1
我从MySQL表中提取,所以我的数据在字典中。怎么能 我合并了所有选定条目的数据?
第一步是将所有数据收集到某种类型的集合中,让我们说清单:
letters = []
cur.execute(some_query) # See the Python database API for what is going on here
results = cur.fetchone()
while results:
the_text = results[0] # if its the first column
for i in the_text.split():
# By default, split() will separate on whitespace,
# so each i is a word.
for letter in i:
if letter in string.letters:
letters.append(letter)
results = cur.fetchone() # get the next result
答案 1 :(得分:1)
您可以按字母拆分所有标题并将其添加到设置中。在集中,您将获得所有独特的字符。简单的例子是:
all_you_titles_string = 'title1 title2 ti tl e3'
result_set = set()
[result_set.add(letter) for letter in all_you_titles_string.replace(' ', '')]
print result_set