내가 읽고있는 csv가 있고 해당 csv의 처음 4 개 열에서 4 개의 목록을 만들고 있습니다.
listofcelebs = (columns['name'])
listOfImages = (columns['image'])
listOfProfessions = (columns['profession'])
listOfBestWork = (columns['bestWork'])
각 항목에 대한 트위터 감정 분석을 실행하고 결과를 출력하려면 첫 번째 목록의 항목 만 읽으면됩니다. 그러나 그 사이에 다른 목록도 인쇄하고 싶습니다.
어떻게 인쇄 할 수 있습니까?
명부 목록의 첫 번째 항목,
listOfImages 목록의 첫 번째 항목,
listOfProfessions 목록의 첫 번째 항목,
listOfBestWork 목록의 첫 번째 항목,
명단리스트의 첫 번째 항목에 대한 트위터 감정 분석 결과,
sperator 즉. "------------------------------------------------- -------------------- \ n "
명부 목록의 두 번째 항목,
listOfImages 목록의 첫 번째 항목,
기타 기타
결국 나는 [name, image, profession, bestWork, overallSentiment]라는 제목과 함께 새로운 csv에 결과를 저장합니다.
내 코드는 현재 잘못된 들여 쓰기 오류를 제공합니다.
import csv
import twittersearch
from collections import defaultdict
c = csv.writer(open("celebritiesBornTodayWithSentiment.csv", "wb"))
columns = defaultdict(list) # each value in each column is appended to a list
with open('celebritiesBornToday.csv') as f:
reader = csv.DictReader(f) # read rows into a dictionary format
for row in reader: # read a row as {column1: value1, column2: value2,...}
for (k,v) in row.items(): # go over each column name and value
columns[k].append(v) # append the value into the appropriate list
# based on column name k
listofcelebs = (columns['name'])
listOfImages = (columns['image'])
listOfProfessions = (columns['profession'])
listOfBestWork = (columns['bestWork'])
zippedListofCelebInfo = zip(listOfNames, listOfImages, listOfProfessions, listOfBestWork)
#giving headings to the columns of the final csv file
c.writerow(['name','image','profession','bestWork','overallSentiment'])
for name in zippedListofCelebInfo:
#Printing the Name of the Celebrity
print "Name of the celebrity: "+name
#Printing the Image of the Celebrity
print "Image: "+image
#Printing the Profession of the Celebrity
print "Profession: "+profession
#Printing the BestWork of the Celebrity
print "BestWork: "+bestWork
#calling twittersearch2 from the other file to derive tweet sentiment, celebrity's full name used as the query
overallSentiment = twittersearch.twittersearch2(name)
print "Overall Sentiment on Twitter : "+overallSentiment
# End of Celebrity Details
print "--------------------------------------------------------------------- \n "
#saving the name, image, profession, bestWork, overallSentiment of the celebrity into a csv file
c.writerow([name,image,profession,bestWork,overallSentiment])
내가 언급했듯이 이미 입력 파일의 각 행을 반복 할 준비가되어 있으므로 모든 데이터를 저장하는 대신 새 행 (추가 데이터 항목 포함)을 출력에 즉시 쓰는 것이 합리적 일 수 있습니다. 많은 목록 :
import csv
import twittersearch
with open('celebritiesBornToday.csv', "rb") as in_f:
reader = csv.DictReader(in_f)
with open("celebritiesBornTodayWithSentiment.csv", "wb") as out_f:
writer = csv.DictWriter(out_f, reader.fieldnames + ["overallSentiment"])
for row in reader:
row["overallSentiment"] = twittersearch.twittersearch2(row["name"])
writer.writerow(row)
print "Name of the celebrity:", row["name"]
print "Image:", row["image"]
print "Profession: ", row["profession"]
print "BestWork:", row["bestWork"]
print "Overall Sentiment on Twitter:", row["overallSentiment"]
현재 코드보다 훨씬 짧고 간단합니다!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다