Hello. I do not understand what is the error?

for element in NodeName: ListSite.write(''.join(element2[i].getAttribute('org') + '\t' + element.getElementsByTagName('ip')[0].childNodes[0] + '\t' + element.getElementsByTagName('domain')[0].childNodes[0] + '\t' + element.getElementsByTagName('url')[0].childNodes[0]'\t'+ '\n').encode('cp1251')); i += 1; 

The compiler swears like this:

TypeError: coercing to Unicode: need string or buffer, instance found

I tried to translate everything into str() , but the error remains, tried to open the file with the parameter bufering=-1 , also indicated the encoding for opening the file in 'utf-8'. Everything that I found in Google, I tried it. But, something I can not.

  • The last .encode('cp1251') line: .encode('cp1251') ==> .encode('utf-8') ??? - Nitive
  • @samoilow, there is no difference, that with cp1251, that reacts to the same with utf-8 - Artie Lay
  • Can I see how you open the file? - WorldCount
  • @WorldCount, of course ListSite = open (INPUT_DIR + NEW_RESOURCE, 'wb'); - Artie Lay

1 answer 1

What version of python?

join for what purpose?

 #Test1 a = "One" b = "Two" c = ''.join((a, b)) print c >>> OneTwo #Test2 a = "One" b = "Two" c = a + b print c >>> OneTwo 

join takes the object being iterated as a parameter, ie:

 print '-'.join("ups") >>> ups 

Upd.

Simply you first glue the string like this:

 elem1 + '\t' + elem2 + '\t' + elem3 + '\t' + elem4 + '\t'+ '\n' 

And then you glue the same string, but symbolically:

 ''.join("Тут уже склеенная вами строка") 

Those. do extra work.

In an amicable way, it is necessary so:

 ''.join((elem1, '\t', elem2, '\t', elem3, '\t', elem4, '\t\n')) 

Get the string:

 elem1 elem2 elem3 elem4 [переход на новую строчку] 
  • '' .join () I found in the open spaces of Google that I should do this, I use the version 2.7. - Artie Lay
  • @WorldCount, I try to translate each element to str (), and I have a known error that pops up> UnicodeEncodeError: 'ascii' codec 0-11: ordinal not in range (128) - Artie Lay
  • And the file from which you get the data in what encoding is? - WorldCount
  • @WorldCount, here I am a fool) I tried everything except> str (). Encode ('cp1251') Thank you so much, I would go further and stupid) - Artie Lay
  • @artemiy happy if I could help. - WorldCount