Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to write data in file

I have data stored in one file – delimiter tab
{‘id’: ‘123’, ‘name’: ‘pečnostní informační služba’}

When I am trying to read the data and write data in 2nd file using python code but getting error

  with open(output_file, 'w') as f_output, open(input_file,encoding = 'utf-8-sig') as f_input:

        reader = csv.DictReader(f_input,delimiter='\t')
        fieldnames =  reader.fieldnames


        writer = csv.DictWriter(f_output, fieldnames=fieldnames)
        writer.writeheader()

        for row in reader:
            print(row)
    
            writer.writerow(row)


Traceback (most recent call last):
  File "C:\Python_Projects\Python_extra_code\csv_example.py", line 180, in <module>
    writer.writerow(row)
  File "C:\Dev\Python3.11\Lib\csv.py", line 154, in writerow
    return self.writer.writerow(self._dict_to_list(rowdict))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Dev\Python3.11\Lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode character '\u010d' in position 69: character maps to <undefined>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

your output file uses ‘cp1252’ encoding. This encoding doesn’t have unicode character č which you are trying to save. You can replicate the error by running this two lines:

text = '\u010d' 
text.encode('cp1252')

Try fixing this error by saving in utf-8 encoding:

with open(output_file, 'w',encoding='utf-8') as f_output:
    text = '\u010d' 
    f_output.write(text)

Or by allowing to ignore errors while saving badly decoded characters:

with open(output_file, 'w',encoding='cp1252', errors='ignore') as f_output:
    text = '\u010d' 
    f_output.write(text)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading