Seite 1 von 1
Import CSV (UTF-8)
Verfasst: Di Jan 25, 2022 6:19 pm
von DanielAC
Hallo zusammen,
ich möchte eine Reihe CSV-Dateien einlesen und mache das mit read.csv. Die Dateien sind im Wesentlichen gleich aufgebaut und es gibt in jeder Datei eine Spalte, die "Straße" heißt (das Problem tritt aber auch bei anderen Spalten mit ß, ä, ö oder ü auf). Manchmal wird "Straße" richtig importiert und manchmal als "Straße". Wenn ich zusätzlich "encoding = "UTF-8"" anwende, kommt "Stra�e" heraus. Beim Öffnen der Datei mit dem Editor sehe ich nichts Auffälliges.
Was kann ich jetzt machen? Das verhagelt mir irgendwie alles...
VG
Daniel
Re: Import CSV (UTF-8)
Verfasst: Di Jan 25, 2022 8:50 pm
von Athomas
Du solltest etwas mehr an Details rauslassen - etwa eine betroffene csv-Datei anhängen, und natürlich auch das Programmstück, das das Einlesen vornimmt.
Benutzt Du R "netto" oder ist RStudio mit von der Partie - und in welchen Versionen auf welchem Betriebssystem?
Re: Import CSV (UTF-8)
Verfasst: Di Jan 25, 2022 9:32 pm
von DanielAC
Versionsinfo (RStudio):
Code: Alles auswählen
R.Version()
$platform
[1] "x86_64-w64-mingw32"
$arch
[1] "x86_64"
$os
[1] "mingw32"
$system
[1] "x86_64, mingw32"
$status
[1] ""
$major
[1] "4"
$minor
[1] "1.2"
$year
[1] "2021"
$month
[1] "11"
$day
[1] "01"
$`svn rev`
[1] "81115"
$language
[1] "R"
$version.string
[1] "R version 4.1.2 (2021-11-01)"
$nickname
[1] "Bird Hippie"
Die Überschriftenzeile einer Datei hängt an. Diese will ich mit einem einfachen read.csv einlesen:
Code: Alles auswählen
read.csv("Test.csv", sep = ";", stringsAsFactors = FALSE, encoding = "UTF-8", header = F)
Ich habe alternativ auch read.table versucht, aber das Problem blieb dasselbe.
Re: Import CSV (UTF-8)
Verfasst: Di Jan 25, 2022 10:04 pm
von DanielAC
Sorry, noch eine Datei im Anhang. Mit der Test2.csv klappt das Einlesen nicht gescheit, mit der Test.csv (vorheriger Post) klappt es offenbar schon.
Re: Import CSV (UTF-8)
Verfasst: Di Jan 25, 2022 10:52 pm
von Athomas
Code: Alles auswählen
read.csv("http://forum.r-statistik.de/download/file.php?id=1634", sep = ";", fileEncoding = "UTF-8-BOM", stringsAsFactors = FALSE)
https://stackoverflow.com/questions/199 ... er-as-text
Schöne Grüße in die Versicherung!
Re: Import CSV (UTF-8)
Verfasst: Di Jan 25, 2022 10:54 pm
von EDi
Was ist den das encoding der Dateien?
Hab mal einen brute-force ansatz durchgeführt um das zu bestimmen...
Code: Alles auswählen
## this gives error
# Error in make.names(col.names, unique = TRUE) :
# invalid multibyte string 10
read.table(file = "http://forum.r-statistik.de/download/file.php?id=1633",
header = TRUE, sep = ";")
# Not sure what encoding this file is, lets
# try all file encodings there are (1173 in my case)
# this may take a while
encodings <- setNames(iconvlist(), iconvlist())
all <- lapply(encodings, function(enc) {
message("Trying ", enc)
try(read.table(file = "http://forum.r-statistik.de/download/file.php?id=1633",
header = TRUE, sep = ";", nrows = 3, fileEncoding = enc), silent = TRUE)
})
# lets check where the 10th column Straße was read in correctly and zero rows
encodings[sapply(all, function(df) {
isTRUE(names(df)[10] == "Straße") && nrow(df) == 0
})]
read.table(file = "http://forum.r-statistik.de/download/file.php?id=1633",
header = TRUE, sep = ";", fileEncoding = "windows-1252")
# alternatively
enc <- readr::guess_encoding(file = "http://forum.r-statistik.de/download/file.php?id=1633")
enc
read.table(file = "http://forum.r-statistik.de/download/file.php?id=1633",
header = TRUE, sep = ";", fileEncoding = enc$encoding[[1]])
enc <- readr::guess_encoding(file = "http://forum.r-statistik.de/download/file.php?id=1634")
enc
read.table(file = "http://forum.r-statistik.de/download/file.php?id=1634",
header = TRUE, sep = ";", fileEncoding = enc$encoding[[1]])
Für mich sieht das nach windows-1252 aus... Bin aber kein Windows Nutzer...
Re: Import CSV (UTF-8)
Verfasst: Mi Jan 26, 2022 12:49 pm
von DanielAC
Danke für die Hilfe!
Spannenderweise schneidet read.csv "Straße" bei "Stra" ab, wenn das Format nicht schön auf "Straße" schließen lässt (unter UTF-8-BOM, was ich gar nicht kannte).
Mit read.table scheint es aber zu klappen.
Gibt es eine Möglichkeit, zu erkennen, wie so ein Text formatiert ist, wenn im Editor alles völlig normal aussieht? Ich bin da ein wenig unerfahren.
Und war es so offensichtlich, dass es sich um ein Problem aus der Versicherung handelt?

Re: Import CSV (UTF-8)
Verfasst: Mi Jan 26, 2022 1:01 pm
von Athomas
Spannenderweise schneidet read.csv "Straße" bei "Stra" ab, wenn das Format nicht schön auf "Straße" schließen lässt (unter UTF-8-BOM, was ich gar nicht kannte).
Bei mir funxioniert's:
Code: Alles auswählen
> read.csv("http://forum.r-statistik.de/download/file.php?id=1634", sep = ";", fileEncoding = "UTF-8-BOM", stringsAsFactors = FALSE)
[1] Neuer.Datensatz KNR VSNR Produkt Produktnummer Name
[7] Vorname Anrede Geburtsdatum Straße PLZ Ort
[13] Telefon Werbezustimmung Vermittler Vertragsstatus Versicherungsbeginn Stornodatum
[19] Versicherungssumme Zahlweise Endkundenbeitrag VSt.Satz VSt.Betrag Risikoprämie.Netto
[25] Provision.assona Selbstbehalt Herstellerschlüssel Auftragsnummer Rahmennummer Kaufdatum
[31] Tarifgeneration
<0 Zeilen> (oder row.names mit Länge 0)
>
Vielleicht spielen da noch RStudio-Einstellungen mit!?
Und war es so offensichtlich, dass es sich um ein Problem aus der Versicherung handelt?
Ja - Zweiradversicherung

!