使用Python编写csv文件,在第2列中使用相应的类别

时间:2017-04-01 10:08:29

标签: python export-to-csv

预期输出是CSV文件,其中包含第1列中的生产描述。

下面的代码示例在第2列中输出带有产品描述的CSV文件。

#function to return the results i.e. categorize the sentence  
def categorize(sentence, show_details=False):
    results = think(sentence)
    results = [[i,r] for i,r in enumerate(results) if r>error_threshold ] 
    results.sort(key=lambda x: x[1], reverse=True) 
    return_results =[classes[r[0]] for r in results]
    return return_results

# code to write csv file, with the corresponding category of product in column 2
import itertools
a=[]
g=open('test1.csv','r+')
reader=csv.reader(g)
for i in itertools.product(*reader):
    for j in range(0,len(i)):
        a.append(categorize(i[j]))
writer=csv.writer(g)
writer.writerows(a)
g.close()
# the output shuld look like:
# iphone          Mobile
# skipping rope   sports'

0 个答案:

没有答案