Writing a list of sentences to a single column in csv with Python -
i started out csv file 1 column , many rows each row contains sentence. wrote python remove stopwords , generated new csv file same format (1 column many rows of sentences, sentences have stopwords removed.) part of code not working writing new csv.
instead of writing 1 sentence 1 column, got multiple columns each row in 1 column contains 1 character of sentence..
here example of new_text_list:
['"although online site asset business, still essential need reliable dependable web hosting provider. when searching suitable web host website, 1 name recommend. choose plan that\'s best business today! try free 30 days! track sales expenses \x82"', '"although online site asset business, still essential need reliable dependable web hosting provider. when searching suitable web host website, 1 name recommend. choose plan that\'s best business today! try free 30 days! track sales expenses \x82"']
here example of output csv:
col1 col2 " w w e " w w e l l
what doing wrong?
here code:
def remove_stopwords(filename): new_text_list=[] cachedstopwords = set(stopwords.words("english")) open(filename,"ru") f: next(f) line in f: row = line.split() text = ' '.join([word word in row if word not in cachedstopwords]) # print text new_text_list.append(text) print new_text_list open("output.csv",'wb') g: writer=csv.writer(g) val in new_text_list: writer.writerows([val])
with open("output.csv", 'wb') g: writer = csv.writer(g) item in new_text_list: writer.writerow([item]) # writerow (singular), not writerows (plural)
or
with open("output.csv", 'wb') g: writer = csv.writer(g) writer.writerows([[item] item in new_text_list])
when use writerows
, argument should iterator of rows, each row iterator of field values. here, field value item
. row list, [item]
. thus, writerows
can take list of lists argument.
writer.writerows([val])
did not work because [val]
list containing string, not list of lists.
now strings sequences -- sequence of characters:
in [164]: list('abc') out[164]: ['a', 'b', 'c']
so writerows
took [val]
list containing row
, val
. each character represented field value. characters in string got splattered. example,
import csv open('/tmp/out', 'wb') f: writer = csv.writer(f) writer.writerows(['hi, there'])
yields
h,i,",", ,t,h,e,r,e
Comments
Post a Comment