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

I can't write into my created json file, but I can print out what needs to be written

I want to extract data from a database and put it into seperate JSON files. I can use the print command to print out what I want, but I can’t write it into the file. Here’s the code:

import sqlite3

db_path = 'D:\\PyCharm\\Projekte\\Vivia_Medikamente\\database-Kopie-Kopie.db'
db_connection = sqlite3.connect(db_path)
db_cursor = db_connection.cursor()
db_cursor.execute('SELECT * FROM articles;')
found_data = db_cursor.fetchall()

JSONnumber = 0

while JSONnumber < 299:
    DrugName = str(found_data[JSONnumber][4])
    JSONcontent = str(found_data[JSONnumber][7])
    f = open(DrugName + '.json', "w")
    f.write(JSONcontent)
    f.close()
    JSONnumber = JSONnumber + 1

print("done")

This is the error I get:

Traceback (most recent call last):
  File "D:/PyCharm/Projekte/Vivia_Medikamente/main.py", line 16, in <module>
    f.write(JSONcontent)
  File "C:\Users\Bennett\AppData\Local\Programs\Python\Python37\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 '\u2081' in position 225: character maps to <undefined>

I’ve tried to change the file to a .txt file but that did not help.

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 :

This is an encoding issue when writing the JSON content to the file.
To resolve this issue, you can try specifying a different encoding when opening the file, such as ‘utf-8’. Here’s an updated version of your code that uses utf-8 encoding:

import sqlite3

db_path = 'D:\\PyCharm\\Projekte\\Vivia_Medikamente\\database-Kopie-Kopie.db'
db_connection = sqlite3.connect(db_path)
db_cursor = db_connection.cursor()
db_cursor.execute('SELECT * FROM articles;')
found_data = db_cursor.fetchall()

JSONnumber = 0

while JSONnumber < 299:
    DrugName = str(found_data[JSONnumber][4])
    JSONcontent = str(found_data[JSONnumber][7])
    with open(DrugName + '.json', "w", encoding='utf-8') as f:
        f.write(JSONcontent)
    JSONnumber = JSONnumber + 1

print("done")

In this updated code, the open() function now includes the encoding='utf-8' parameter, which specifies the desired encoding for the file. Using the with statement ensures that the file is properly closed after writing.

If your JSON cannot be encoded in utf-8, you may need to consider alternative approaches, such as escaping or you might need to convert the characters before writing the file.

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