Inserting a column of pinyin for a column of Chinese characters using Python -
Inserting a column of pinyin for a column of Chinese characters using Python -
i'm current using http://lxyu.github.io/pinyin/ working fine.
import pinyin print pinyin.get(u'你好')
and 'nihao' correct.
with csv file of next format:
陈升 "mon mon" 草蜢 李宇春 ...
however, when effort this:
with open('chinesenames.csv','rb') fin, open('output.csv','wb') fout: reader = csv.reader(fin, delimiter=' ', quotechar='|') author = csv.writer(fout) line in reader: writer.writerow(pinyin.get(line))
i get:
traceback (most recent phone call last): file "singerpinyin.py", line 12, in <module> writer.writerow(pinyin.get(line)) file "/usr/local/lib/python2.7/dist-packages/pinyin/pinyin.py", line 30, in s = compat.u(s) file "/usr/local/lib/python2.7/dist-packages/pinyin/compat.py", line 12, in u homecoming unicode(s, "utf-8") typeerror: coercing unicode: need string or buffer, list found
trying:
writer.writerow(pinyin.get(u'line'))
would give wrong output:
l,i,n,e l,i,n,e l,i,n,e ...
any help appreciated.
solved: should pinyin.get(line[0]).
python python-2.7 csv
Comments
Post a Comment