Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I output a mix of str, UTF-8, & int to a text file?
    primarykey
    data
    text
    <p>I have a tuple containing string data (UTF-8), binary (true/false/1/0) &amp; integer data that I want to output as a row in my output file. A portion of my code is:</p> <pre><code>### Python 2.73 import fileinput import re import time import codecs uIDfile = '\Python\Fav Test\ppl.ttxt' InFile = open(uIDfile) OutFile = codecs.open('C:\Python\Fav Test\S2.ttxt', encoding='utf-8', mode='w') for user in InFile: user = user [:-1] # user = unicode(user, 'utf-8').encode('utf-8') if 'NNNN' in user: break else: if '@N' in user: try: Grp = people_getGroups(user_id = user) g = 0 if GetAll: for group in Grp.find('groups').findall('group'): g += 1 fErr = '' uID = user gID = group.get('ID') gName = group.get('name') tup = '\"{0}\"\t\"{2}\"\t\"{1}\"\t''\t{3}\t{4}\t{5}\t{6}\n'.format(uNSID, gNSID, gName, bin1, bin2, int1, int2) OutFile.write(tup.encode('utf-8')) </code></pre> <p>I've tried several different versions of the "OutFile.write()" statement. The errors are listed below each.</p> <pre><code>OutFile.write(codecs.utf_8_decode(tup.encode('utf-8'))) TypeError: coercing to Unicode: need string or buffer, tuple found OutFile.write('\t'.join(codecs.utf_8_decode(tup.encode('utf-8')))) TypeError: sequence item 1: expected string or Unicode, int found OutFile.write('\t'.join(map(str, codecs.utf_8_decode(tup.encode('utf-8'))))) tup = '\"{0}\"\t\"{2}\"\t\"{1}\"\t""\t\"{3}\"\t\"{4}\"\t\"{5}\"\t\"{6}\"\n'.format(uNSID, gNSID, gName, str(bin1), str(bin2), str(int1), str(int2)) UnicodeEncodeError: "'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)" OutFile.write('\t'.join(map(str, codecs.utf_8_decode(tup.encode('utf-8'))))) tup = '\"{0}\"\t\"{2}\"\t\"{1}\"\t""\t\"{3}\"\t\"{4}\"\t\"{5}\"\t\"{6}\"\n'.format(uNSID, gNSID, gName, bin1, bin2, int1, int2) UnicodeEncodeError: "'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)" </code></pre> <p>Any help is sincerely appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload