How to join strings into one sentence separated by spaces

Question:

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.

I expected the output to be like this:

gfg best gfg I am I two two three..............
Asked By: Zulfi A

||

Answers:

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)
Answered By: Ivan Sushkov

The parameter end in python’s print() function defaults to n, so it will wrap automatically.
Default print() function:

#print(end="n")
print()

Set the parameter end="" to what you want. For example, end="*", then it will be linked with *.

solve:

with open("neg.csv") as g:
    for tweete in g:
        for word in tweete.split():
            if word not in words_list:
                print(word, end=" ")
Answered By: Jack Liu