-3

I am very new to coding (just about few months in Python) and NLTK (~1 month). I have a list of sentences that I filtered out with [ i for i in TokenizedSentences if "apple" in i ] and it looks like this--

itemDict["Apple"] = [ i for i in TokenizedSentences if "apple" in i ]

output:

["An apple a day, keeps a doctor away.", "My favorite desert is apple pie.", "Apple candy is sold out!!"]

I want to make each sentence a new line when I save the result to CSV file. A similar analogy would be Alt +Enter in Excel for PC and CTRL-OPT-RETURN in numbers for Mac. Is it possible? I have tried Textwrap, but the result is not exactly what I want.

Desired output:

["An apple a day, keeps a doctor away.",

"My favorite desert is Apple pie.",

"Apple candy is sold out!!"]

Thank you!

cya2017
  • 25
  • 5
  • 3
    Your desired output isn't really CSV, are you sure that this is what you want, with square brackets and all? – Thierry Lathuille Apr 21 '18 at 12:49
  • @ThierryLathuille, I am only few months into coding and just one month into NLTKL, thus I still rely heavily on excel/page and powerpoint/keynote to complete my study/research. And thats why I posted this question in the first place-- afterall, its easier to remove square brackets than to make each sentence a line. Hope this makes sense to you. – cya2017 Apr 21 '18 at 13:18
  • Excel won't expect the square brackets. Just write a normal CSV file. Use Python's [`csv.writer`](https://docs.python.org/3/library/csv.html#csv.writer) – Barmar Apr 21 '18 at 13:43

1 Answers1

0

Your itemDict["Apple"] is a list of strings. What you get when printing it is a representation of the list, not the list itself.

The way Python represents lists is by enclosing the textual representations of its elements in square brackets, separated by commas. The square brackets and commas are not part of the data.

To obtain the expected output of your question would mean transforming the textual representation of the list, and would give you something that isn't valid csv.

What you want is one sentence per line, and that's all.

It's best to use the csv module to do that, so that it will take care of the quoting etc.

import csv

sentences = ["An apple a day, keeps a doctor away.", "My favorite desert is apple pie.", "Apple candy is sold out!!"]

with open('sentences.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows([sentence] for sentence in sentences)

You'll get this nice csv file:

"An apple a day, keeps a doctor away."
My favorite desert is apple pie.
Apple candy is sold out!!
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
  • Thanks!! But I have multiple lists in a dictionary that I want to have a new line per sentence: _"Apple","Mango","Orange"_. I have tried below like (https://stackoverflow.com/questions/3348460/csv-file-written-with-python-has-blank-lines-between-each-row) suggested, but it didnt work. `with open("/Users/cya2017/Desktop/csv//Filename" + now + ".csv","w", newline = " ") as fOut: csvOut = csv.DictWriter(fOut,["Event","Date","Member","keywordCount","Apple","Mango","Orange"]) csvOut.writeheader() csvOut.writerows(Filename)` – cya2017 Apr 22 '18 at 04:25