Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7916967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:59:23+00:00 2026-06-03T14:59:23+00:00

I have a document in Spanish I’d like to format using Python. Problem is

  • 0

I have a document in Spanish I’d like to format using Python. Problem is that in the output file, the accented characters are messed up, in this manner: \xc3\xad.
I succeeded in keeping the proper characters when I did some similar editing a while back, and although I’ve tried everything I did then and more, somehow it won’t work this time.
This is current version of the code:

# -*- coding: utf-8 -*- 

import re
import pickle

inputfile = open("input.txt").read()

pat = re.compile(r"(@.*\*)")

mylist = pat.findall(inputfile)

outputfile = open("output.txt", "w")

pickle.dump(mylist, outputfile)

outputfile.close()

I’m using Python 2.7 on Windows 7.
Can anyone see any obvious problems? The inputfile is encoded in utf-8, but I’ve tried encoding it latin-1 too. Thanks.

To clarify: My problem is that the latin characters doesn’t show up properly in the output.
It’s solved now, I just had to add this line as suggested by mata:

inputfile = inputfile.decode('utf-8')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:59:24+00:00Added an answer on June 3, 2026 at 2:59 pm

    it the input file is encoded in utf-8, then you should decode it first to work with it:

    import re
    import pickle
    
    inputfile = open("input.txt").read()
    inputfile = inputfile.decode('utf-8')
    
    pat = re.compile(r"(@.*\*)")
    
    mylist = pat.findall(inputfile)
    
    outputfile = open("output.txt", "w")
    
    pickle.dump(mylist, outputfile)
    
    outputfile.close()
    

    the so created file will contain a pickled version of your list. it you would rather hava a human readable file, then you might want to just use a plain file.
    also a good way to deal with different encodings is using the codecs module:

    import re
    import codecs
    
    with codecs.open("input.txt", "r", "utf-8") as infile:
        inp = infile.read()
    
    pat = re.compile(r"(@.*\*)")
    mylist = pat.findall(inp)
    
    with codecs.open("output.txt", "w", "utf-8") as outfile:
         outfile.write("\n".join(mylist))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a document that looks something like <root> <element> <subelement1 /> <subelement2 />
I have a document that it's structure is like below. There are a lot
I have a document structure in Solr that looks something like this (irrelevant fields
I have a document library in SharePoint. When a new file is uploaded to
I have a document, something like this: <root> <A node=1/> <B node=2/> <A node=3/>
I have a document that I want to be flipped / rotated 180 degrees
I have a document that is an overview document within a folder, there are
i have the next code written in C#....it create a tex file using utf-8.....the
I've a text field which extends javax.swing.JTextField and also have document listener like this
I am just a beginner in python. I have document score= {1:0.98876, 8:0.12245, 13:0.57689}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.