Python NLTK从CSV准备数据以进行标记化

问题描述 投票:0回答:1

我是Python和NLTK的新手。从CSV导入文本后,我正在尝试使用NLTK在Python中准备用于标记化的文本。文件中只有一栏包含自由文本。我想隔离那个特定的列,我做了...。

import spacy
import pandas as pd
import numpy as np
import nltk
from nltk.tokenize import word_tokenize
import re
import unicodedata


pd.set_option('display.max_colwidth',50)

oiw = pd.read_csv(r'C:\Users\tgray\Documents\PythonScripts\Worksheets.csv')

text = oiw.drop(oiw.columns[[1,2,3]],axis=1)

for row in text:
    for text['value'] in row:
        tokens = word_tokenize(row)
print(tokens)

当我运行代码时,它给我的输出是['values'],即列名。如何使其余的行显示在输出中?

[值]栏中的示例数据:

这种方式在网上订购太容易了。

一切都很好。

对我来说太容易打破。

我希望收到的输出是:

['The','way','was','too','easy','to','order','online','Everything','is','great','It''s','for','me','break']
python csv nltk tokenize
1个回答
0
投票

您需要在段中进行的更正。

oiw = pd.read_csv(r'C:\Users\tgray\Documents\PythonScripts\Worksheets.csv')

text = oiw.drop(columns=[1,2,3]) # correctly dropping columns named 1 2 and 3 

for row in text['value']: # Correctly selecting the column 
    tokens = word_tokenize(row)
    print(tokens) # Will print tokens in each row
print(tokens) # Will print the tokens of the last row

因此,您将遍历数据框的正确列。

© www.soinside.com 2019 - 2024. All rights reserved.