0

I have a dataframe (let's call it df) that has a column (let's call it A).
Column A has the description value that has text (multiple sentences).

I want to write the text into a text file (text.txt) such that each line has just one sentence.

So, I want to split the contents of column A by full stop(.) and question mark(?) and write them into a text file for each row. All the sentences from column A will be in 1 file, so text.txt should iteratively append sentences for each row

This is what I have so far, I have split the column into list of sentences

import numpy as np
df['A'] = df['A'].apply(lambda x : str(x).split("."))
#np.savetxt('text.txt', A.values)

but I m not sure, how to proceed further, Can someone help?

martineau
  • 119,623
  • 25
  • 170
  • 301
Aayush Gupta
  • 434
  • 2
  • 13

1 Answers1

2

This code may help (insert after second line of code in the question)

# get string of all sentences
res = "\n".join(["\n".join(row) for row in df["A"]])
# file write
f = open("file.txt", "a")
f.write(res)
f.close()
MefAldemisov
  • 867
  • 10
  • 21
  • Thanks I would also like to split by ? along with the full stop, how do i modify your code – Aayush Gupta Jul 02 '20 at 14:48
  • You can run the line `df['A'] = df['A'].apply(lambda x : str(x).split("."))` with the `?` sign (2 times). Also [this](https://stackoverflow.com/questions/4998629/split-string-with-multiple-delimiters-in-python) question may be helpful. – MefAldemisov Jul 02 '20 at 14:52