pandas 访问文本文件

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

我正在使用pandas访问一个文件,其内容如下:

English Word    anger   anticipation    disgust fear    joy negative    positive    sadness surprise    trust   Bengali Word
aback   0   0   0   0   0   0   0   0   0   0   বিস্মিত
abacus  0   0   0   0   0   0   0   0   0   1   অ্যাবাকাস

我正在尝试使用以下代码在 Google colab 中读取此文件。

import pandas as pd
cols = ['English Word','anger   ','anticipation ','disgust  ','fear ','joy  ','negative ','positive ','sadness  ','surprise ','trust    ','Bengali Word']
 
data=pd.read_csv('/content/gdrive/MyDrive/Bengali-NRC-EmoLex.txt', sep=' ', header=None, names=cols)
data

但是我的输出不正确。你能告诉我如何用 Python 读取上面的文本文件吗?

python pandas google-colaboratory text-files
1个回答
0
投票

我使用分隔符作为制表符。更新后的代码如下:

import pandas as pd
cols = ['English Word','anger','anticipation','disgust','fear','joy','negative','positive','sadness','surprise','trust','Bengali Word']
 
data=pd.read_csv('/content/gdrive/MyDrive/Bengali-NRC-EmoLex.txt', sep="\t", header=None, names=cols)
data
© www.soinside.com 2019 - 2024. All rights reserved.