I GOT THIS ERROR WHEN I RUN PYTHON

158 views
Skip to first unread message

Madhusudhanan Sambath

unread,
Mar 18, 2013, 11:38:45 AM3/18/13
to nltk-users, SentimentAI
hi to all ,
i have got this error when i try to write a str to a output.txt file
***********************************************************************************************************
i got this error

Traceback (most recent call last):
File "C:\Users\madhu\Desktop\yesnosplit.py", line 35, in <module>
f.write(x+"\n")
TypeError: can only concatenate list (not "str") to list

***********************************************************************************************************


My python Code: TO SPLIT THE SENTENCE AND SAVE IT TO A ANOTHER FILE



import re, math, collections, itertools
import nltk
from nltk.corpus import stopwords
import string

postext_filter=[]
negtext_filter=[]
alist=[]

stopwords=open('D:\stopwords.txt','r').read().split()


#reading pre-labeled input and splitting into lines
posSentences = open('D:\\yes.txt', 'r')
negSentences = open('D:\\no.txt', 'r')
posSentences = re.split(r'\n', posSentences.read())
negSentences = re.split(r'\n', negSentences.read())


posFeatures = []
negFeatures = []
#http://stackoverflow.com/questions/367155/splitting-a-string-into-words-and-punctuation
#breaks up the sentences into lists of individual words (as selected
by the input mechanism) and appends 'pos' or 'neg' after each list
for i in posSentences:
posWords = re.findall(r"[\w']+|[.,!?;]", i)
posFeatures.append(posWords)

for i in negSentences:
negWords = re.findall(r"[\w']+|[.,!?;]", i)
negFeatures.append(negWords)

poslist=posFeatures
with open('output.txt','w')as f:
for x in poslist:
f.write(x+"\n")

**********************************************************************************************************

KINDLY HELP ME
THANKS IN ADVANCE


REGARDS
S.MADHU
RESEARCH SCHOLAR,
ANNA UNIVERSITY, CHENNAI
INDIA-s...@gmail.com
RE

Michael Deeringer

unread,
Mar 18, 2013, 11:42:15 AM3/18/13
to nltk-...@googlegroups.com

posFeatures / poslist is a list of lists, so you need to convert each list x to some string before concatenating a newline.

~md

Sent from my mobile device

--
You received this message because you are subscribed to the Google Groups "nltk-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nltk-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Nigel Legg

unread,
Mar 18, 2013, 11:43:46 AM3/18/13
to nltk-...@googlegroups.com
does
with open('output.txt','w') as f:

    for x in poslist:
        f.write(x+"\n")
work? (note space between ")" and "as").


--
You received this message because you are subscribed to the Google Groups "nltk-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nltk-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.





--
Regards,
Nigel Legg
07722 652866
http://twitter.com/nigellegg
http://uk.linkedin.com/in/nigellegg

Madhusudhanan Sambath

unread,
Mar 18, 2013, 11:52:07 AM3/18/13
to nltk-...@googlegroups.com
hi
i am trying with that list but it will not work
how can i change the code to write a list in to text file...


thanks
S.Madhu

Madhusudhanan Sambath

unread,
Mar 18, 2013, 11:54:46 AM3/18/13
to nltk-...@googlegroups.com
hi
i have tried with onelis only,i thought i have to copry into another
list and wrte it on to a file .but tat also nt working
how can i change the code and write it to a file

thanks
S.Madhu

Michael Deeringer

unread,
Mar 18, 2013, 12:07:26 PM3/18/13
to nltk-...@googlegroups.com

Write str(x), not x.

Sent from my mobile device

Reply all
Reply to author
Forward
0 new messages