Hallo wollte mich einmal vorstellen
Ich heisse richard und bin neu hier in der Community
Leider habe ich auch schon ein problem mit den strings denn ich würde gerne einen string einlesen und ihn zeichenweise vergleichen.Aber da alles in Python strings sind habe ich keine Ahnung wie ich sie ZEICHENWEISE vergleichen soll.Ich wollte nur einen kleinen Filter für Windows machen sodass ich beim einlesen von dateien nicht den doppelten \ schreiben muss.
Bitte schreibt mir bald zurück
lg
Richard
Strings zeichenweise vergleichen
-
- Python-Forum Veteran
- Beiträge: 16025
- Registriert: Freitag 20. Juni 2003, 16:30
- Kontaktdaten:
Falsch, in Python sind alles Objekte.richard5579 hat geschrieben:Aber da alles in Python strings sind
Die einzelnen Buchstaben eines Strings bekommst du so:richard5579 hat geschrieben:habe ich keine Ahnung wie ich sie ZEICHENWEISE vergleichen soll.
Code: Alles auswählen
for buchstabe in "Ich bin ein Satz.":
print buchstabe
Was ist denn ein kleiner Filter für Windows?richard5579 hat geschrieben:Ich wollte nur einen kleinen Filter für Windows machen
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
- sunmountain
- User
- Beiträge: 89
- Registriert: Montag 13. März 2006, 17:18
den Namen der Datei, die Du öffnen willst als Raw String schreibst, brauchst Du das auch nicht:
Der Schlüssel ist hier das 'r' vor dem String.
Das gibt es auch in Kombination mit Unicodestrings:
Siehe auch: http://docs.python.org/ref/strings.html :
When an "r" or "R" prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literal r"\n" consists of two characters: a backslash and a lowercase "n". String quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.
Der Raw String darf also nicht auf "\" enden,
das hier geht folglich in die Hose:
und muss so aussehen:
Code: Alles auswählen
fname = r'C:\TEMP\afile.txt'
f = open(fname,'r')
for line in f:
print line
Das gibt es auch in Kombination mit Unicodestrings:
Code: Alles auswählen
aUnicodeString = ur'Beispiel'
When an "r" or "R" prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literal r"\n" consists of two characters: a backslash and a lowercase "n". String quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.
Der Raw String darf also nicht auf "\" enden,
das hier geht folglich in die Hose:
Code: Alles auswählen
import os.path
if os.path.isdir(r"C:\TEMP\"):
print "Pfad existiert"
Code: Alles auswählen
import os.path
if os.path.isdir(r"C:\TEMP"):
print "Pfad existiert"
Zuletzt geändert von sunmountain am Montag 11. Dezember 2006, 10:43, insgesamt 3-mal geändert.
- sunmountain
- User
- Beiträge: 89
- Registriert: Montag 13. März 2006, 17:18
Das ist in Tcl so.richard5579 hat geschrieben: Aber da alles in Python strings sind