python - UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 3: ordinal not in range(128) -
this question has answer here:
i got encoding error:
unicodeencodeerror: 'ascii' codec can't encode character u'\ufffd' in position 3: ordinal not in range(128)
at following python (pyspark) code, row data frame row:
def rowtoline(row): line = str(row[0]).strip() columnnum = 44 k in xrange(1, columnnum): line = line + "\t" line = line + str(row[k]).strip() # encoding error here return line
i tried join below:
def rowtoline(row): s = "\t" return s.join(row)
but values of row int, got errors:
typeerror: sequence item 19: expected string or unicode, int found
does know how fix this? thanks!
thanks everyone's suggestions!
i took padraic cunningham's idea , made modification handle int case. code below works.
def rowtoline(row): s = "\t" return s.join( x.encode("utf-8") if isinstance(x, basestring) else str(x).encode("utf-8") x in row)