0
red_voznje = []
def otvori_redvoznje(prevoznik,odrediste,peron,rezervacija,termin):
    datoteka = open("red_voznje.csv","r",encoding="utf-8")   
    for line in datoteka:
        vrednosti = line.rstrip().rsplit(",")
        recnik = {
                "prevoznik": vrednosti[0],
                "odrediste": int(vrednosti[1]),
                "peron": int(vrednosti[2]),
                "rezervacija": vrednosti[3],
                "termin": int(vrednosti[4])
            }
        red_voznje.append(recnik)

    return red_voznje

Here is the error i get:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
  • Does this answer your question? ["Unicode Error "unicodeescape" codec can't decode bytes... Cannot open text files in Python 3](https://stackoverflow.com/questions/1347791/unicode-error-unicodeescape-codec-cant-decode-bytes-cannot-open-text-file) – VPfB Dec 21 '21 at 19:40
  • Thanks for this it has removed the error but now doesnt load the file at all. – starter1234 Dec 21 '21 at 19:54

1 Answers1

0

could avoid unicode error with errors = 'ignore' in open()

eg datoteka = open("red_voznje.csv","r",encoding="utf-8", errors="ignore")

InhirCode
  • 338
  • 1
  • 4
  • 5
  • Thanks,But its still giving the same error even after i added that. – starter1234 Dec 21 '21 at 19:19
  • could switch to read binary 'rb' instead of read text 'rt' to get a byte literal – InhirCode Dec 21 '21 at 19:21
  • I cant change to binary because the assignment must stay in csv format. – starter1234 Dec 21 '21 at 19:24
  • it is still string so once you have the byte literal can just take off the "b' " at the start and the " ' " at the end before your csv retrieval/workings. To check what the byte literal looks like can print(datoteka) – InhirCode Dec 21 '21 at 19:29
  • Thanks for the help i will try it. – starter1234 Dec 21 '21 at 19:38
  • But i think that the error is more in the syntax of the code then in the open section. – starter1234 Dec 21 '21 at 19:39
  • yeah sorry - meant print(datoteka.read()) – InhirCode Dec 21 '21 at 19:53
  • Thank you so much for the help. – starter1234 Dec 21 '21 at 19:57
  • Formatted files have headers and sometimes footers with info for apps that use the file. Need to research csv format to find out what position in file data is at. These files are usually in binary which can get unicode errors since Python encoding seems to be problematic with these (to me anyway). Can use .seek() and data size if necessary and str() for text . Wikipedia often has good info about different file formats. – InhirCode Dec 22 '21 at 19:27