# Buggy code
def _write_comment(self, filename, comment):
    """Write comment to file."""

    def write_header_line(fd, header_field, header_content):
        """Write comment header line."""
        if header_content is None:
            return
        header_content = str(header_content).replace("\n", " ")
        line = ".. " + header_field + ": " + header_content + "\n"
        fd.write(line.encode("utf8"))

    with open(filename, "wb+") as fd:
        write_header_line(fd, "id", comment["id"])
        write_header_line(fd, "status", comment["status"])
        write_header_line(fd, "approved", comment["approved"])
        write_header_line(fd, "author", comment["author"])
        write_header_line(fd, "author_email", comment["email"])
        write_header_line(fd, "author_url", comment["url"])
        write_header_line(fd, "author_IP", comment["ip"])
        write_header_line(fd, "date_utc", comment["date"])
        write_header_line(fd, "parent_id", comment["parent"])
        write_header_line(fd, "wordpress_user_id", comment["user_id"])
        fd.write(("\n" + comment["content"]).encode("utf8"))


# Traceback (UnicodeEncodeError)
Traceback (most recent call last):
File "/path/lib/python2.7/site-packages/doit/doit_cmd.py", line 168, in run
return command.parse_execute(args)
File "/path/lib/python2.7/site-packages/doit/cmd_base.py", line 122, in parse_execute
return self.execute(params, args)
File "/path/lib/python2.7/site-packages/nikola/plugin_categories.py", line 127, in execute
return self._execute(options, args)
File "/path/lib/python2.7/site-packages/nikola/plugins/command/import_wordpress.py", line 325, in _execute
self.import_posts(channel)
File "/path/lib/python2.7/site-packages/nikola/plugins/command/import_wordpress.py", line 982, in import_posts
self.process_item_if_post_or_page(item)
File "/path/lib/python2.7/site-packages/nikola/plugins/command/import_wordpress.py", line 963, in process_item_if_post_or_page
out_folder_slug = self.import_postpage_item(item, wordpress_namespace, 'posts', attachments)
File "/path/lib/python2.7/site-packages/nikola/plugins/command/import_wordpress.py", line 916, in import_postpage_item
self._write_comment(os.path.join(self.output_folder, out_folder, comment_filename), comment)
File "/path/lib/python2.7/site-packages/nikola/plugins/command/import_wordpress.py", line 722, in _write_comment
write_header_line(fd, "author", comment["author"])
File "/path/lib/python2.7/site-packages/nikola/plugins/command/import_wordpress.py", line 714, in write_header_line
header_content = str(header_content).replace('\n', ' ')
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 2: ordinal not in range(128)

# Fixed code
def _write_comment(self, filename, comment):
    """Write comment to file."""

    def write_header_line(fd, header_field, header_content):
        """Write comment header line."""
        if header_content is None:
            return
        header_content = unicode_str(header_content).replace("\n", " ")
        line = ".. " + header_field + ": " + header_content + "\n"
        fd.write(line.encode("utf8"))

    with open(filename, "wb+") as fd:
        write_header_line(fd, "id", comment["id"])
        write_header_line(fd, "status", comment["status"])
        write_header_line(fd, "approved", comment["approved"])
        write_header_line(fd, "author", comment["author"])
        write_header_line(fd, "author_email", comment["email"])
        write_header_line(fd, "author_url", comment["url"])
        write_header_line(fd, "author_IP", comment["ip"])
        write_header_line(fd, "date_utc", comment["date"])
        write_header_line(fd, "parent_id", comment["parent"])
        write_header_line(fd, "wordpress_user_id", comment["user_id"])
        fd.write(("\n" + comment["content"]).encode("utf8"))