【问题标题】:How to upload a text file using google.colab import files and then converting it into a set of words?如何使用 google.colab 导入文件上传文本文件,然后将其转换为一组单词?
【发布时间】:2020-11-23 03:30:34
【问题描述】:

这是我为西班牙语中的停用词制作的文本文件,然后我将其上传到 google collab: srop_words_spanish.txt

这是我的代码:

from google.colab import files
spa = files.upload()

如您所见,导入成功:

srop_words_spanish.txt(text/plain) - 3391 bytes, last modified: 11/22/2020 - 100% done
Saving srop_words_spanish.txt to srop_words_spanish (4).txt

但是,它作为字典上传。

{'srop_words_spanish.txt': b'alg\xc3\xban\r\nalguna\r\nalgunas\r\nalguno\r\nalgunos\r\nambos\r\nampleamos\r\nante\r\nantes\r\naquel\r\naquellas\r\naquellos\r\naqui\r\narriba\r\natras\r\nbajo\r\nbastante\r\nbien\r\ncada\r\ncierta\r\nciertas\r\ncierto\r\nciertos\r\ncomo\r\ncon\r\nconseguimos\r\nconseguir\r\nconsigo\r\nconsigue\r\nconsiguen\r\nconsigues\r\ncual\r\ncuando\r\ndentro\r\ndesde\r\ndonde\r\ndos\r\nel\r\nellas\r\nellos\r\nempleais\r\nem.....

我的目标是将字典转换为一组没有 \r 或 \n 的单词,或者直接打开文本文件,如下所示:

stopspa=set(line.strip() for line in open("srop_words_spanish.txt", 
encoding='UTF-8'))

我想知道如何做这两件事。

【问题讨论】:

    标签: python dictionary text set


    【解决方案1】:
    # clean up the dataset to remove unnecessary columns (eg. REG) 
    df=df.drop(['Marca temporal','Nombre y Apellidos','Correo electrónico'], axis = 1)
    df.columns = list(map(str, df.columns))
    %load_ext google.colab.data_table
    df
    [![enter image description here][1]][1]
    
    df['Comentarios'] = df['Comentarios'].replace('\n', '')
    df['Comentarios'] = df['Comentarios'].replace('\r', '')
    
    text=[]
    
    for i in range(len(df)):
      text.append(df.iloc[i,0])text= 
    
    " ".join([str(i) for i in text]) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      相关资源
      最近更新 更多