Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

批量处理怎么弄呢?就是想导入一个文本,每一行代表一篇文章,将文本转换为拼音。不知为什么老提示:list index out of range #13

Open
zuyinup opened this issue May 19, 2016 · 2 comments

Comments

@zuyinup
Copy link

zuyinup commented May 19, 2016

No description provided.

@zuyinup zuyinup changed the title 批量处理怎么弄呢?就是想导入一个文本,每一行代表一篇文章,将文本转换为拼音。不知为什么老提示 批量处理怎么弄呢?就是想导入一个文本,每一行代表一篇文章,将文本转换为拼音。不知为什么老提示:list index out of range May 19, 2016
@zuyinup
Copy link
Author

zuyinup commented May 19, 2016

代码如下,报错:list index out of range
from pinyin import PinYin
f = open('1.txt','r')
lines = f.readlines()
for line in lines:
test = PinYin()
test.load_word()
# print(lines)
print "out: %s" % str(test.hanzi2pinyin(string=line))

print result

f.close()

@realpp
Copy link

realpp commented Jan 23, 2017

1、从txt取出的,每行都会有一个换行符,要去掉,不然会报错。加一个:strip()
print "out: %s" % str(test.hanzi2pinyin(string=line.strip()))

2、这两行不要放在循环里
test = PinYin()
test.load_word()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants