使用列表和 for 循环处理 Python 列表中的 csv
Processing csv in Python lists using lists and for loops
我有一个正在读取的 csv,我正在从该 csv 的前 4 列中制作 4 个列表。
listofcelebs = (columns['name'])
listOfImages = (columns['image'])
listOfProfessions = (columns['profession'])
listOfBestWork = (columns['bestWork'])
我只需要阅读第一个列表中的项目,运行 对每个项目进行 Twitter 情绪分析并打印出结果。但是我也想打印出中间的其他列表。
我怎么打印出来
名人列表的第一项,
然后是列表 listOfImages 的第一项,
然后是列表 listOfProfessions 的第一项,
然后是列表 listOfBestWork 的第一项,
然后是 listofcelebs 列表第一项的 Twitter 情绪分析结果,
一个sperator即。 ”------------------------------------------------ ------------------ \n "
然后是名人名单的第二项,
然后是列表 listOfImages 的第一项,
等等等
最后我将结果存储在一个新的 csv 中,标题为 [name,image,profession,bestWork,overallSentiment])
这是我的代码目前给出的错误缩进不正确
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])
如果您确定您的列表具有相同的长度,您可以使用 zip
函数或对于长列表作为更好的选择,您可以使用 itertools.izip
将您的列表压缩在一起:
from itertools import izip
my_lists=[listofcelebs,listOfImages ,listOfProfessions ,listOfBestWork]
for i,j,k,z in izip(*my_lists):
print (i,j,k,z)
print "--------------------------------------------------------------------- \n "
对于小列表:
my_lists=[listofcelebs,listOfImages ,listOfProfessions ,listOfBestWork]
for i,j,k,z in zip(*my_lists):
print (i,j,k,z)
print "--------------------------------------------------------------------- \n "
如果你想在每一行打印:
my_lists=[listofcelebs,listOfImages ,listOfProfessions ,listOfBestWork]
for i,j,k,z in zip(*my_lists):
print i,'\n',j,'\n',k,'\n',z,'\n'
print "--------------------------------------------------------------------- \n "
正如我评论的那样,您已经准备好迭代输入文件的每一行,因此在读取新行(带有附加数据项)后立即将其写入输出可能是有意义的,而不是将所有数据存储在一堆列表中:
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"]
这比您当前的代码更短更简单!
我有一个正在读取的 csv,我正在从该 csv 的前 4 列中制作 4 个列表。
listofcelebs = (columns['name'])
listOfImages = (columns['image'])
listOfProfessions = (columns['profession'])
listOfBestWork = (columns['bestWork'])
我只需要阅读第一个列表中的项目,运行 对每个项目进行 Twitter 情绪分析并打印出结果。但是我也想打印出中间的其他列表。
我怎么打印出来
名人列表的第一项,
然后是列表 listOfImages 的第一项,
然后是列表 listOfProfessions 的第一项,
然后是列表 listOfBestWork 的第一项,
然后是 listofcelebs 列表第一项的 Twitter 情绪分析结果,
一个sperator即。 ”------------------------------------------------ ------------------ \n "
然后是名人名单的第二项,
然后是列表 listOfImages 的第一项,
等等等
最后我将结果存储在一个新的 csv 中,标题为 [name,image,profession,bestWork,overallSentiment])
这是我的代码目前给出的错误缩进不正确
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])
如果您确定您的列表具有相同的长度,您可以使用 zip
函数或对于长列表作为更好的选择,您可以使用 itertools.izip
将您的列表压缩在一起:
from itertools import izip
my_lists=[listofcelebs,listOfImages ,listOfProfessions ,listOfBestWork]
for i,j,k,z in izip(*my_lists):
print (i,j,k,z)
print "--------------------------------------------------------------------- \n "
对于小列表:
my_lists=[listofcelebs,listOfImages ,listOfProfessions ,listOfBestWork]
for i,j,k,z in zip(*my_lists):
print (i,j,k,z)
print "--------------------------------------------------------------------- \n "
如果你想在每一行打印:
my_lists=[listofcelebs,listOfImages ,listOfProfessions ,listOfBestWork]
for i,j,k,z in zip(*my_lists):
print i,'\n',j,'\n',k,'\n',z,'\n'
print "--------------------------------------------------------------------- \n "
正如我评论的那样,您已经准备好迭代输入文件的每一行,因此在读取新行(带有附加数据项)后立即将其写入输出可能是有意义的,而不是将所有数据存储在一堆列表中:
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"]
这比您当前的代码更短更简单!