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..............
>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)