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

Read txt file into python

I have german wordlist which contain special charachters like ä,ö,ü. and a word e.g. like "Nährstoffe". But when i read the text file and create a dict from it i get a wrong word out of it.

Here is my code in python3:

import random
import csv
import os
permanettxtfile='wortliste.txt'
newlines = open(permanettxtfile, "r")
lines=newlines.read().split('\n')
random.shuffle(lines)

linkdict=dict.fromkeys(lines)
print(linkdict)

I get as output:

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

'Nährstoffe': None

But i want:

'Nährstoffe': None

How can i solve this issue? Is this an UTF-8 issue?

>Solution :

Try opening file in utf-8 encoding:

import random
import csv
import os

permanettxtfile='wortliste.txt'

with open(permanettxtfile, 'r', encoding='utf-8') as file:
    lines = file.read().split('\n')
    random.shuffle(lines)

linkdict = dict.fromkeys(lines)
print(linkdict)

Also don’t forget to close it with context manager as in my example or with newlines.close() for your example

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