
Hallo! Kennt jemand eine einfache Art um Koordinaten aus einem ASCII file als Zahlen und nicht als String in eine Liste einzulesen? Mein ASCII file hat folgende Form: 47408983,9620543,441.357422 47408983,9620543,441.443348 usw. Wenn ich nun input=open('file','r') und dann S=input.readlines() ausführe, erhalte ich eine Liste mit Strings und nicht Zahlen. Danke für jegliche Tipps. Mfg Daniela _______________________________________________ Python-de maillist - Python-de@python.net http://python.net/mailman/listinfo/python-de

Hallo Daniela! Schau mal im string Modul nach irgendwie so import string for x in lst: y = string.atof(x) mfg christian
Hallo!
Kennt jemand eine einfache Art um Koordinaten aus einem ASCII file als Zahlen und nicht als String in eine Liste einzulesen? Mein ASCII file hat folgende Form:
47408983,9620543,441.357422 47408983,9620543,441.443348 usw.
Wenn ich nun input=open('file','r') und dann S=input.readlines() ausführe, erhalte ich eine Liste mit Strings und nicht Zahlen. Danke für jegliche Tipps.
Mfg Daniela
_______________________________________________ Python-de maillist - Python-de@python.net http://python.net/mailman/listinfo/python-de
_______________________________________________ Python-de maillist - Python-de@python.net http://python.net/mailman/listinfo/python-de

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hallo Daniela, ~ was hältst Du von folgender Lösung: import string input=open('file','r') S=input.readlines() for i in S: ~ ss=string.splitfields(i[:-1],",") ~ for j in ss: ~ print string.atof(j) Gruß Gerhard |>Hallo! |> |>Kennt jemand eine einfache Art um Koordinaten aus einem ASCII file als |>Zahlen und nicht als String in eine Liste einzulesen? |>Mein ASCII file hat folgende Form: |> |>47408983,9620543,441.357422 |>47408983,9620543,441.443348 |>usw. |> |>Wenn ich nun input=open('file','r') und dann S=input.readlines() ausführe, |>erhalte ich eine Liste mit Strings und nicht Zahlen. |>Danke für jegliche Tipps. |> |>Mfg Daniela - -- - ------------------------------------------------------ skequell ------ ~ Gerhard Quell Software & Knowledge Engineering ~ Schützenweg 3 eMail: gquell@skequell.de Fon: 0731-26400651 ~ 89275 Elchingen web : http://www.skequell.de Fax: 0731-26400652 - ---Aus Sicherheitsgründen: bitte keine Word-, Excel-Dateianhänge ----- -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFADSbjvHbZD0c+xTwRAlNqAJ491w282j/5yMc1/zV5CnvGsGA+8ACfd404 cpsGmCIFUkaXCKLWv1OFLRs= =XT4C -----END PGP SIGNATURE----- _______________________________________________ Python-de maillist - Python-de@python.net http://python.net/mailman/listinfo/python-de

gerhard quell schrieb:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Hallo Daniela,
~ was hältst Du von folgender Lösung:
import string input=open('file','r') S=input.readlines() for i in S: ~ ss=string.splitfields(i[:-1],",") ~ for j in ss: ~ print string.atof(j)
Sind atof und das string-Modul nicht deprecated? input=open('file','r') S=input.readlines() res = [] for i in S: res.append([ int(a) for a in i.split(',') ]) (nicht getestet) Tobias _______________________________________________ Python-de maillist - Python-de@python.net http://python.net/mailman/listinfo/python-de

On 1 Feb 2004, Tobias Herp <- Nummer-5@gmx.net wrote:
Sind atof und das string-Modul nicht deprecated?
Ja, nein.
input=open('file','r') S=input.readlines() res = [] for i in S: res.append([ int(a) for a in i.split(',') ])
(nicht getestet)
Das kann auch nicht funktionieren, da die Koordinaten auch Gleitkommazahlen sein können. Es gibt sicherlich mehrere alternative Möglichkeiten. Was man nimmt, hängt wohl sehr von der genauen Aufgabenstellung ab. def ret_coord (stream): return [eval(num) for line in stream.readlines() for num in line.split(',')] def ret_coord (stream): return [(lambda n: (n.find('.') > -1) and float(n) or int(n))(num) for line in stream.readlines() for num in line.split(',')] def ret_coord (stream): res = [] for line in stream: res.extend(map(lambda n: (n.find('.') > -1) and float(n) or int(n), line.split(','))) return res Obiges sind alles mehr oder minder äquivalente Lösungen (wenn man weiß, daß nur Koordinaten vorkommen, ist »eval« eine gute Alternative). def ret_coord (stream): for line in stream: yield map(lambda n: (n.find('.') > -1) and float(n) or int(n), line.split(',')) Als Generator gefällt es mir fast am besten. Man kann sich dann noch entscheiden, ob man die Koordinaten nicht gleich als Tupel zurückgibt. Karl -- Increasingly, people seem to misinterpret complexity as sophistication, which is baffling -- the incomprehensible should cause suspicion rather than admiration. Possibly this trend results from a mistaken belief that using a somewhat mysterious device confers an aura of power on the user. -- Niklaus Wirth _______________________________________________ Python-de maillist - Python-de@python.net http://python.net/mailman/listinfo/python-de

Am Tue, 20 Jan 2004 12:55:48 +0100 Schrieb dbuechel@tiscali.ch:
Hallo!
Kennt jemand eine einfache Art um Koordinaten aus einem ASCII file als Zahlen und nicht als String in eine Liste einzulesen? Mein ASCII file hat folgende Form:
47408983,9620543,441.357422 47408983,9620543,441.443348 usw.
Wenn ich nun input=open('file','r') und dann S=input.readlines() ausführe, erhalte ich eine Liste mit Strings und nicht Zahlen. Danke für jegliche Tipps.
Mfg Daniela
Hallo Daniela, erg = [] f = file('file','r') for line in f: erg.append(map(float, line.strip().split(","))) f.close() Gruß Fritz _______________________________________________ Python-de maillist - Python-de@python.net http://python.net/mailman/listinfo/python-de

Hallo! das module csv scheint noch nicht so bekannt zu sein, hab's selbst auch nur zur Kenntnis genommen und noch nicht probiert. Aber nach meiner Meinung die Variante, die eigentlich für diesen Fall geschaffen wurde: import csv,string reader = csv.reader(file("some.csv", "b")) for row in reader: print [ string.atof(fld) for fld in row ] Gruß, Dieter ---------- dbuechel@tiscali.ch schreibt am Dienstag, 20. Januar 2004 12:55:
Hallo!
Kennt jemand eine einfache Art um Koordinaten aus einem ASCII file als Zahlen und nicht als String in eine Liste einzulesen? Mein ASCII file hat folgende Form:
47408983,9620543,441.357422 47408983,9620543,441.443348 usw.
Wenn ich nun input=open('file','r') und dann S=input.readlines() ausführe, erhalte ich eine Liste mit Strings und nicht Zahlen. Danke für jegliche Tipps.
Mfg Daniela
_______________________________________________ Python-de maillist - Python-de@python.net http://python.net/mailman/listinfo/python-de
participants (7)
-
Christian Klinger
-
dbuechel@tiscali.ch
-
Dieter Neubauer
-
Fritz Cizmarov
-
gerhard quell
-
sigurd@12move.de
-
Tobias Herp