Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a list of sentences to a single column in csv with Python

I started out with a CSV file with one column and many rows where each row contains a sentence. I wrote some python to remove the stopwords and generated a new csv file with the same format (1 column many rows of sentences, but now the sentences have their stopwords removed.) The only part of my code that is not working is writing to a new csv.

Instead of writing one sentence to one column, i got multiple columns where each row in one column contains one character of the sentence..

Here is an example of my new_text_list:

['"Although online site asset business, still essential need reliable dependable web hosting provider. When searching suitable web host website, one name recommend. Choose plan that\'s Best Business Today! Try Now 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, one name recommend. Choose plan that\'s Best Business Today! Try Now FREE 30 Days! Track sales expenses \x82"']

Here is an example of the output csv:

col1 col2
"      W
W      e
"      W
W      e
l
l

What am i doing wrong?

Here is my code:

def remove_stopwords(filename):
  new_text_list=[]
  cachedStopWords = set(stopwords.words("english"))
  with open(filename,"rU") as f:
    next(f)
    for line in f:
      row = line.split()
      text = ' '.join([word for word in row
                             if word not in cachedStopWords])
      # print text
      new_text_list.append(text)
  print new_text_list

  with open("output.csv",'wb') as g:
    writer=csv.writer(g)
    for val in new_text_list:
      writer.writerows([val])
like image 927
jxn Avatar asked Apr 10 '14 21:04

jxn


People also ask

How do I write a list of elements in a csv file in Python?

csv is the name of the file, the “w” mode is used to write the file, to write the list to the CSV file write = csv. writer(f), to write each row of the list to csv file writer. writerow() is used.

How do you write data in a csv file line by line in Python?

Use write() to write into a CSV file Call open(file, "w") to prepare file for writing. Call file. write(str) to write to file with str as the desired data. Each line should be separated by \n to write line by line.

How do I write a data list in a csv file?

The most common method to write data from a list to CSV file is the writerow() method of writer and DictWriter class. Example 1: Creating a CSV file and writing data row-wise into it using writer class.


1 Answers

with open("output.csv", 'wb') as g:
    writer = csv.writer(g)
    for item in new_text_list:
        writer.writerow([item])  # writerow (singular), not writerows (plural)

or

with open("output.csv", 'wb') as g:
    writer = csv.writer(g)
    writer.writerows([[item] for item in new_text_list])

When you use writerows, the argument should be an iterator of rows, where each row is a iterator of field values. Here, the field value is item. So a row could be the list, [item]. Thus, writerows can take a list of lists as its argument.

writer.writerows([val])

did not work because [val] is just a list containing a string, not a list of lists.

Now strings are also sequences -- a sequence of characters:

In [164]: list('abc')
Out[164]: ['a', 'b', 'c']

So writerows took [val] to be a list containing a row, val. Each character represented a field value. So the characters in your string got splattered. For example,

import csv
with open('/tmp/out', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(['Hi, there'])

yields

H,i,",", ,t,h,e,r,e
like image 61
unutbu Avatar answered Sep 20 '22 16:09

unutbu