user3682157
user3682157

Reputation: 1695

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:

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. 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']

without the POS Tag!

I'm not sure why this isn't working as I am very new to Python 3. Any help would be much appreciated! Thank you!

Upvotes: 0

Views: 1380

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 122086

pos_tag creates and returns a new list; it doesn't modify its argument. Assign the new list back to the same name:

J = nltk.pos_tag(J)

Upvotes: 2

Related Questions