Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to join strings into one sentence separated by spaces

I have this code:

with open("wordslist.txt") as f:
    words_list = {word.removesuffix("\n") for word in f}

with open("neg.csv") as g:
    for tweete in g:
        for word in tweete.split():
            if word not in words_list:
                print(word)

and the output is like this:

gfg
best
gfg
I
am
I
two
two
three
..............

I want to remove the newline (enter) so it will be one big sentence (there are like 4500+ words). How to join the words and remove the newline (replace each newline with space) so it became one big sentence.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I expected the output to be like this:

gfg best gfg I am I two two three..............

>Solution :

You can append them to a list and than do " ".join(your_list)
Or you can create an empty string x = ""
And in in your iteration do smth like x += word

Here is example for the 1st solution

import csv

# Open the CSV file and read its contents
with open('file.csv', 'r') as csv_file:
    reader = csv.reader(csv_file)
    next(reader)  # Skip the header row
    
    # Initialize an empty list to store the column values
    column_values = []
    
    # Retrieve the values from the specified column and append them to the list
    for row in reader:
        column_values.append(row[0])  # Replace 0 with the index of the desired column
        
    # Create a sentence with whitespace between the words
    sentence = ' '.join(column_values)
    print(sentence)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading