简体   繁体   English

如何在Python 3中将POS Tagger NLTK用于导入的CSV文件

[英]How to use a POS Tagger NLTK for an Imported CSV File in Python 3

I have a question -- Right now I have code that imports a CSV file where the first column is full of words in the following format: 我有一个问题-现在我有导入CSV文件的代码,其中第一列包含以下格式的单词:

This
Is
The
Format

Once this CSV file is uploaded and read by Python, I want to be able to tag these words using an NLTK POS Tagger. 将此CSV文件上传并由Python读取后,我希望能够使用NLTK POS Tagger标记这些单词。 Right now, my code goes like this 现在,我的代码是这样的

Import CSV
with open(r'C:\Users\jkk\Desktop\python.csv', 'r') as f:
reader = csv.reader(f)
J = []
for row in reader:
   J.extend(row)
import nltk
nltk.pos_tag(J)
print(J)

However, when I print it, I only get: 但是,当我打印它时,只会得到:

['This ', 'Is ', 'The', 'Format'] ['This','Is','The','Format']

without the POS Tag! 没有POS标签!

I'm not sure why this isn't working as I am very new to Python 3. Any help would be much appreciated! 我不确定为什么它不起作用,因为我是Python 3的新手。我们将不胜感激! Thank you! 谢谢!

pos_tag creates and returns a new list ; pos_tag 创建并返回一个新列表 it doesn't modify its argument. 它不会修改其参数。 Assign the new list back to the same name: 将新列表分配回相同的名称:

J = nltk.pos_tag(J)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM