从目录中读取所有文本文件

从目录中读取所有文本文件

本文介绍了从目录中读取所有文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的python和我使用下面的代码拉动输出作为情绪分析:

  import json 
from watson_developer_cloud import ToneAnalyzerV3Beta
import urllib.request
import codecs
import csv
import os
import re
import sys
import collections
import glob
ipath ='C:/ TEMP /'#输入文件夹
opath ='C:/ TEMP / matrix /'#输出文件夹
reader = codecs.getreader utf-8)
tone_analyzer = ToneAnalyzerV3Beta(
url ='https://gateway.watsonplatform.net/tone-analyzer/api',
username ='ABCID',
password ='ABCPASS',
version ='2016-02-11')
path ='C:/ TEMP / *。txt'
file = glob.glob b $ b text = file.read()
data = tone_analyzer.tone(text ='text')
for cat in data ['document_tone'] ['tone_categories']:
print ('category:',cat ['category_name'])
for tone in cat ['tones']:
print(' - ',tone ['tone_name'],tone ['score'] )
#create file

在上面的代码中,我想要做的就是阅读文件和做情绪分析所有的文本文件存储在C:/ TEMP文件夹,但我不断收到和错误:'list'对象没有属性'读'



不确定我错了,我真的很感激任何帮助这一个。另外,有一种方法,我可以将输出写入CSV文件,如果我正在读取文件



ABC.txt,我创建一个输出CSV文件名为ABC。

解决方案
glob 返回文件列表,您需要遍历列表,打开每个文件,然后在文件对象上调用 .read

  files = glob.glob(path)
#在列表中循环获取每个文件
用于文件中的fle:
#打开文件,然后调用.read()获取文本
with open(fle)as f:
text = f.read()

不确定您要写什么,但会执行:

  b 
$ b files = glob.glob(path)
#在列表中循环获取每个文件
用于文件中的fle:
#打开文件,然后调用.read )获得文本
with open(fle)as f,open({}。csv.format(fle.rsplit(。,1)[1]),
text = f.read()
wr = writer(out)
data = tone_analyzer.tone(text ='text')
wr.writerow column,names])#编写col名称

然后调用 writerow 传递要为每一行写入的任何内容的列表。


I am new to python and I am using following code to pull output as sentiment analysis:

import json
from watson_developer_cloud import ToneAnalyzerV3Beta
import urllib.request
import codecs
import csv
import os
import re
import sys
import collections
import glob
ipath = 'C:/TEMP/' # input folder
opath = 'C:/TEMP/matrix/' # output folder
reader = codecs.getreader("utf-8")
tone_analyzer = ToneAnalyzerV3Beta(
    url='https://gateway.watsonplatform.net/tone-analyzer/api',
    username='ABCID',
    password='ABCPASS',
    version='2016-02-11')
path = 'C:/TEMP/*.txt'
file = glob.glob(path)
text = file.read()
data=tone_analyzer.tone(text='text')
for cat in data['document_tone']['tone_categories']:
    print('Category:', cat['category_name'])
    for tone in cat['tones']:
        print('-', tone['tone_name'],tone['score'])
        #create file

In the above code all I am trying to do is to read the file and do sentiment analysis all the text file stored in C:/TEMP folder but I keep getting and error :'list' object has no attribute 'read'

Not sure where I am going wrong and I would really appreciate any help with this one. Also, is there a way i can write the output to a CSV file so if I am reading the file

ABC.txt and I create a output CSV file called ABC.csv with output values.

Thank You

解决方案

glob returns a list of files, you need to iterate over the list, open each file and then call .read on the file object:

files = glob.glob(path)
# iterate over the list getting each file
for fle in files:
   # open the file and then call .read() to get the text
   with open(fle) as f:
      text = f.read()

Not sure what it is exactly you want to write but the csv lib will do it:

from csv import writer

files = glob.glob(path)
# iterate over the list getting each file
for fle in files:
   # open the file and then call .read() to get the text
   with open(fle) as f, open("{}.csv".format(fle.rsplit(".", 1)[1]),"w") as out:
       text = f.read()
       wr = writer(out)
       data = tone_analyzer.tone(text='text')
       wr.writerow(["some", "column" ,"names"]) # write the col names

Then call writerow passing a list of whatever you want to write for each row.

这篇关于从目录中读取所有文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:28