I have 100,000 words as the result of a set function in Python 3 NLTK.
word_list1_total=set(words1_total)
I want to write word_list1_total
to a csv file. Then I want to open that csv file along with other similar csv files and perform word_tokenize() and set() functions on the combinations of those csv files. The problem is I can easily write word_list1_total
by doing this
with open('C:\\My_folder\\csv_file1.csv', 'a', newline='', encoding='utf-8-sig') as csvfile:
writer = csv.writer(csvfile, delimiter=',',quoting=csv.QUOTE_MINIMAL)
writer.writerow(word_list1_total)
However, the csv_file1.csv
file that is then created has say a size of 7 MB with 100,000 words in just 1 line in a csv file. That csv file then becomes impossible to use because of the length of that line.
How to make this file usable? Had the 100,000 words been in 100,000 lines with 1 word each, then, I think, it would have been more usable. Is there any way in which while creating this csv file, I can write every word to a new line in the csv file, so that instead of looking like this:
word_1,word_2,word_3,...word_100000
it looks like this
word_1,
word_2,
word_3,...
word_100000
I am writing my first Python programme. So please help.
You could iterate through your list word by word and write each word in a separate row instead. Using a for
loop, this would look as follows:
with open('C:\\My_folder\\csv_file1.csv', 'a', newline='', encoding='utf-8-sig') as csvfile:
writer = csv.writer(csvfile, delimiter=',',quoting=csv.QUOTE_MINIMAL)
for word in word_list1_total:
writer.writerow([word])
Edit: Note that each word is wrapped in a list in order to prevent each individual character being saved in a separate column.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.