Python string/list with £ symbols failing writing to file?
Question: I am having issues writing data to a file due to `£` signs being in my
string/list.

For example in my code below, `x` is created by a series of appends from
various regex searchs, matches, subs and generic trims/splits.

    
    
    # -*- coding: utf-8 -*-
    x = [u'Loc ', u'352', '1', '51', '3D2', u'Student Total \xa3540.00', u'Discount \xa235.00', '\n', u'Rec ', u'352', '2', '51', '5S1', u'Student Total \xa3540.00', u'Discount \xa235.00', '\n']
    with open('test.txt','w') as dfile:
        dfile.write('\n'.join(x)) # UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 71: ordinal not in range(128)
        dfile.write(x) # TypeError: expected a character buffer object
    

I am trying to write x to file so it appears like:

    
    
    Loc
    352
    1
    51
    3D2
    Student Total £3540.00
    Discount £235.00
    
    Rec
    352
    2
    51
    5S1
    Student Total £3540.00
    Discount £235.00
    

Anyone know how I can do what I am trying to achieve?

**EDIT**

I now can't get it to compare and if it's different then save...

    
    
    with open('test.txt','r') as dfile:
        dfiler = dfile.read()
        dfiler = dfiler.decode("UTF-8")
        if dfiler == x:
            print "same, no need to save"
        else:            
            with open('test.txt','w') as result_end_datafile:
                dfile.write('\n'.join(x).encode("UTF-8"))
    


Answer: You need to encode the unicode string before writing:

    
    
    dfile.write('\n'.join(x).encode("UTF-8"))
    

Alternatively, you use use
[codecs.`open()`](http://docs.python.org/release/2.6/library/codecs.html#codecs.open)
in Python 2.x, thus passing the encoding as the argument while opening file:

    
    
    import codecs
    
    with codecs.open('test.txt', 'w', encoding="UTF-8") as dfile:
        dfile.write('\n'.join(x))
    

* * *

**Related:**

  * [UnicodeDecodeError when redirecting to file](http://stackoverflow.com/q/4545661/1679863)

