Les scripts PYTHON

Site de reference : http://unstableme.blogspot.com

LE CUMUL D'UNE COLONNE
  1. Cumuler et creer des fichiers si limite atteinte WHILE1 #part1001
  2. Cumuler et creer des fichiers si limite atteinte WHILE LEN #part1002
  3. Cumuler et creer des fichiers si limite atteinte RANGE LEN #part1003
  4. Cumuler les valeurs d'une colonne mono fichier + trait separe si limite atteinte - mono fichier #part1004
  5. Cumuler les valeurs d'une colonne d'un seul fichier - sans remise a 0 - mono fichier #part1005
  6. Cumuler les valeurs d'une colonne de plusieurs fichiers [var crochet] #part1006
  7. Cumuler les valeurs d'une colonne avec remise a zero a une limite fixee (exemple long) #part1007
LA LIGNE : compter - numeroter
  1. Compter le nombre de lignes d'un fichier : SUM LEN #part1008
  2. Compter le nombre de lignes d'un fichier : LEN #part1009
  3. Compter le nombre de lignes d'un fichier : BUF SIZE #part1010
  4. Compter le nombre de lignes de plusieurs fichiers : CUMUL LEN #part1011
  5. Numeroter les lignes d'un fichier avec WHILE LINE #part1012
  6. Numeroter les lignes d'un fichier avec WHILE 1 #part1013
LA LIGNE : compter des patterns - des lignes contenant des patterns
  1. Compter nombre de lignes contenant plusieurs patterns dans un seul fichier #part1014
  2. Compter nombre de lignes contenant plusieurs patterns dans plusieurs fichiers #part1015
  3. Compter directement des patterns (ecrits ds 1 fichier a part) dans plusieurs fichiers #part1016
  4. Compter les occurrences d'1 caractere pour chaque ligne #part1017
  5. Compter les occurrences d'1 mot pour chaque ligne #part1018
  6. Compter le nombre de mots d'un fichier #part1019
  7. Compter les visites a l'ouverture d'un script #part1020
LA LIGNE : inserer (pour selectionner les lignes doubles)
  1. Inserer un trait de separation avant un pattern (exemple 1) #part1021
  2. Inserer un trait de separation avant un pattern (exemple 2) #part1022
  3. Inserer un trait de separation dans un fichier si la colonne 1 change RANGE LEN #part1023
  4. Inserer un signe sur une ligne si la colonne 1 change : selectionner les doubles, triples .. RANGE LEN #part1275
  5. Inserer un trait de separation dans un fichier si la colonne 3 change RANGE LEN #part1024
  6. Inserer un trait de separation dans un fichier si la colonne 1 change (exemple long) #part1025
  7. Inserer un trait de separation dans un fichier si (n+1 < n) en colonne 3 (cumul deja fait) #part1026
  8. Inserer un trait de separation dans un fichier si (n+1 = n+1) en colonne 3 (cumul deja fait) #part1027
LA LIGNE : ajouter
  1. Ajouter une ligne vide ou ecrite au debut ou a la fin d'un fichier (exemple court) #part1028
  2. Ajouter une ligne au debut et a la fin d'un fichier (exemple long) #part1029
  3. Ajouter une ligne au debut et a la fin de plusieurs fichiers (methode rapide) #part1030
  4. Ajouter des lignes au debut et a la fin de plusieurs fichiers [var crochet] #part1031
  5. Ajouter un meme chiffre a toutes les fins de lignes #part1032
  6. Ajouter du texte en debut ou fin de ligne MATCH #part1033
  7. Ajouter des digits "0" devant des chiffres pour qu'ils aient tous la meme longueur LEN #part1034
  8. Ajouter une colonne de prepos au contenu d'un fichier avec out multi fichiers #part1035
LA LIGNE : grouper - degrouper - horizontal - vertical
  1. Grouper horizontalement toutes les lignes ayant meme colonne 1 - dic (entree mono fichier) #part1036
  2. Grouper horizontalement toutes les lignes ayant meme colonne 1 - dic (entree multi fichiers) #part1037
  3. Grouper tout le fichier sur une seule ligne horizontale #part1038
  4. Degrouper : coller la colonne 1 a chaque valeur horizontale #part1039
  5. Modifier ligne horizontale en colonne verticale - out multi fichiers #part1040
  6. Capturer des caracteres en fin de ligne oubien colonnes #part1041
LA LIGNE : operer avec 2 fichiers : capturer et supprimer
  1. Substituer des patterns selon un fichier pattern-correspondance #part1042
  2. Capturer une ligne si un pattern est contenu dans un autre fichier avec FOR #part1043
  3. Capturer une ligne si un pattern est contenu dans un autre fichier avec (IF ANY) #part1044
  4. Capturer une ligne si un pattern est contenu dans un autre fichier avec MATCH #part1045
  5. Capturer des paragraphes si la colonne 1 change avec WHILE 1 #part1046
  6. Marquer des lignes si contenues dans un autre fichier INTERSECTION #part1047
  7. Supprimer des lignes si des mots sont dans un autre fichier - IF ANY #part1048
  8. Supprimer des lignes de plusieurs fichiers si des mots sont dans un fichier [var crochet] #part1049
  9. Un fichier a 2 colonnes commande l'habillage d'un autre fichier #part1050
  10. Un fichier a 2 colonnes commande le decoupage en plusieurs fichiers (avec debut-fin) #part1051
  11. Renommer plusieurs fichiers selon fichier 2 colonnes (col1=ancien - col2=nouveau) #part1052
LA LIGNE : operer avec 2 fichiers : MODIFIER avec IF ANY
  1. Modifier une syntaxe selon un 2eme fichier IF ANY #part1272
  2. Substituer un pattern selon un 2eme fichier #part1053
COMPARER 2 LISTES
  1. Supprimer ou modifier des elements d'une liste selon une autre liste RANGE LEN #part1279
  2. Verifier si elements d'une petite liste sont dans grande liste RANGE LEN #part1054
  3. Verifier si elements d'une grande liste sont dans petite liste RANGE LEN #part1055
  4. Verifier si elements d'une petite liste sont dans grande liste #part1056
  5. Verifier si elements d'une grande liste sont dans petite liste #part1057
LE DICTIONNAIRE : changer plusieurs valeurs dans 1 seul fichier avec un tableau a 2 colonnes
  1. Substituer (remplacer) des patterns selon un fichier pattern-correspondance DIC #part1058
LA LIGNE : capturer (PARTIE 1/3)
  1. Capturer un pattern avec FIND (mono fichier) #part1059
  2. Capturer plusieurs patterns SEARCH REGEX #part1060
  3. Capturer un pattern en ignorant la casse (avec format) SEARCH #part1061
  4. Capturer un pattern en ignorant la casse (sans format) SEARCH #part1062
LA LIGNE : capturer (PARTIE 2/3)
  1. Capturer des lignes (grep rapide) avec IF ANY - REGEX (mono fichier) #part1063
  2. Capturer des lignes avec SEARCH - REGEX (mono fichier) #part1064
  3. Capturer plusieurs patterns sur une meme ligne SEARCH (colorama) #part1065
  4. Capturer des patterns avec SEARCH #part1066
  5. Capturer des patterns avec FNMATCH (resultat en liste) #part1067
  6. Capturer un pattern avec FINDITER REGEX #part1068
  7. Capturer plusieurs series de patters differents SEARCH #part1069
  8. Capturer des lignes avec choix du pattern et de son occurrence avec COUNT #part1070
LA LIGNE : capturer avec STARTSWITH (PARTIE 3/3)
  1. Capturer un pattern avec STARTSWITH #part1071
  2. Capturer plusieurs patterns multi regex avec STARTSWITH [var crochet] - exemple 1 #part1072
  3. Capturer plusieurs patterns multi regex avec STARTSWITH - exemple 2 #part1073
  4. Capturer un pattern avec ENDSWITH #part1074
LA LIGNE : lire - ecrire - selectionner - remplacer - verifier
  1. Lire une ligne si elle contient une majuscule (avec capitalize) #part1283
  2. Changer la premiere lettre en majuscule (avec upper) #part1277
  3. Lire premiere et derniere ligne #part1075
  4. Lire 2 lignes apres chaque apparition d'un marqueur (grep -A) #part1076
  5. Lire une ligne (moins bon) [var crochet] #part1077
  6. Lire la ligne 3 en partant de la fin #part1078
  7. Lire les N premieres lignes d'un fichier (exemple 1 court) #part1079
  8. Lire les N premieres lignes d'un fichier (exemple 2 long) #part1080
  9. Lire les N premieres lignes de plusieurs fichiers #part1081
  10. Lire un fichier en sens inverse : de la fin au debut #part1082
  11. Lire un fichier avec WITH OPEN #part1083
  12. Ecrire dans un fichier un nombre avec str #part1276
  13. Selectionner des lignes contenant 3 espaces SEARCH #part1084
  14. Selectionner des portions de lignes delimitees par un separateur (SEARCH - regex) #part1085
  15. Selectionner des portions de lignes delimitees par un separateur SPLIT #part1086
LA LIGNE : marquer
  1. Marquer nom du fichier devant les lignes (in-out multi fichiers) #part1087
  2. Marquer nom du fichier devant les lignes (in-out multi fichiers) #part1088
  3. Marquer nom du fichier devant les lignes (in-out multi fichiers) #part1089
  4. Marquer nom du fichier devant les lignes (in-out mono fichier) #part1090
  5. Marquer un titre et le repeter s'il ne change pas avec RANGE LEN (in-out mono fichier) #part1091
  6. Marquer un titre et le repeter s'il ne change pas (in-out mono fichier) #part1092
  7. Marquer un titre et le repeter s'il ne change pas + trait separ - mono fichier WHILE1 - FLAG (in-out mono fichier) #part1093
  8. Marquer un texte unique devant les lignes MAP LAMBDA #part1094
LA LIGNE : supprimer
  1. Supprimer la ligne 1 dans un fichier #part1095
  2. Supprimer la derniere ligne de plusieurs fichiers #part1096
  3. Lire et stopper a un pattern dans plusieurs fichiers BREAK #part1097
  4. Supprimer un blanc au debut ou fin de ligne - supprimer un retour chariot #part1098
  5. Supprimer des lignes avec STARTSWITH #part1099
  6. Supprimer des groupes de mots avec SUB [var crochet] #part1100
LA COLONNE
  1. Supprimer une colonne avec SPLIT - MATCH - SUB #part1280
  2. Supprimer une colonne avec SUB #part1101
  3. Capturer la premiere et derniere colonne FINDALL #part1102
  4. Des mots contenus dans un fichier vont supprimer des lignes dans un autre fichier #part1270
  5. Supprimer la premiere et derniere colonne et portions de textes SUB #part1103
  6. Arrondir une colonne a la decimale inferieure ou superieure #part1104
IF ANY (SANS VAR=[)
  1. IF ANY - SANS VAR=[ - fichier2:readsplitlines + fichier1:with open + IFANY @ Capturer des lignes #part1105
  2. IF ANY - SANS VAR=[ - fichier2:readsplitlines + fichier1:readlines + IFANY @ Colorier plusieurs mots dans un texte #part1107
  3. IF ANY - SANS VAR=[ - fichier2:readsplitlines + fichier1:withopen + IFANY @ Supprimer des lignes si des mots sont dans un autre fichier #part1108
  4. IF ANY - SANS VAR=[ - REGEX + fichier1:for + IFANY @ Lire un fichier ligne par ligne #part1109
  5. IF ANY - SANS VAR=[ - pattern + fichier1:readlines + IFANY @ Tester l'existence d'un pattern dans un fichier #part1110
  6. IF ANY - SANS VAR=[ - pattern + FOR:readlines + IFANY @ Tester l'existence d'un pattern dans plusieurs fichiers #part1111
IF ANY (AVEC VAR=[)
  1. IF ANY - AVEC VAR=[ - fichier2:readsplitlines + IFANY @ Supprimer des lignes selon un deuxieme fichier [var crochet] #part1112
  2. IF ANY - AVEC VAR=[ - fichier2:readsplitlines + fpath IFANY @ XARGS : Rechercher une syntaxe dans des sous repertoires [var crochet] #part1113
  3. IF ANY - AVEC VAR=[ - fichier2:readsplitlines + IFANY @ Tester l'existence d'un mot dans un fichier [var crochet] #part1114
  4. IF ANY - AVEC VAR=[ - fichier2:readsplitlines + IFANY @ Capturer des patterns (ecrits ds 1 fichier a part) dans plusieurs fichiers [var crochet] #part1115
  5. IF ANY - AVEC VAR=[ - IFANY REGEX SPLIT @ Rechercher un pattern [var crochet] #part1116
GREP (commande BASH) signifiant en PYTHON : "CAPTURE DE MOTS"
  1. Capture avec IF ANY + mono REGEX (raw_input) #part1117
  2. Capture avec IF ANY + multi REGEX (rapide - en desordre) #part1118
  3. Capture avec IF IN + multi REGEX (numero de ligne precise) #part1119
  4. Capture avec IF ANY + fichier2: read splitlines (out mono fichier) #part1120
  5. Capture avec IF IN + fichier2 (out multi fichier) #part1121
  6. Capture avec [var crochet] incluant SEARCH #part1122
  7. Capture avec [var crochet] incluant MATCH #part1123
  8. Capture avec [var crochet] incluant STARTSWITH + REGEX #part1124
  9. Capture avec 'contenu +=' #part1125
  10. Capture avec SEARCH #part1126
  11. Capture avec RANGE LEN (idem que 'grep -B' en Bash) #part1127
  12. Capture avec RANGE LEN une ligne avant un pattern #part1284
TECHNIQUE DE CAPTURE
  1. Technique de capture : readlines : tableau selectionne 1 colonne #part1128
  2. Technique de capture : readlines - tableau RANGE LEN selectionne des lignes (mono fichier) #part1129
  3. Technique de capture : readlines - tableau RANGE LEN selectionne des lignes (multi fichier) #part1130
  4. Technique de capture : readline - WHILE 1 ajoute titres devant lignes (multi fichier) #part1131
  5. Technique de capture : withopen - SPLIT selectionne des colonnes (mono fichier) #part1132
  6. Technique de capture : open lines - SPLIT coupe en mots (mono fichier) #part1133
  7. Technique de capture : open lines - CUMUL += lines (mono fichier) #part1134
  8. Technique de capture : read splitlines dans [var crochet] - WHILE boucle sur fichiers a ouvrir (out mono fichier) #part1135
LA LISTE
  1. MIN et MAX des elements d'une liste : shortest - longest #part1136
LE TABLEAU
  1. Capturer une ligne et la modifier avec un tableau #part1137
WHILE 1 - WHILE LINE
  1. WHILE 1 : boucle lisant un fichier controle par un autre fichier #part1282
  2. WHILE : lire plusieurs lignes avec WHILE [var crochet] #part1138
  3. WHILE 1 : boucle pour lire un fichier ligne par ligne (exemple 1/2) #part1139
  4. WHILE LINE : boucle pour lire un fichier ligne par ligne (exemple 2/2) #part1140
  5. WHILE 1 : boucle pour assembler plusieurs fichiers #part1141
CONVERTIR en HEXA et ASCII
  1. Convertir en HEXA et ASCII #part1281
SEARCH
  1. Capturer un pattern entre 2 dieses avec SEARCH #part1142
  2. Capturer un pattern avec SEARCH #part1143
RANGE LEN
  1. Marquer des lignes si patterns uniques dans une colonne avec RANGE LEN #part1273
  2. Capturer des patterns avec ISDIGIT et RANGE LEN #part1144
  3. Marquer un titre et le repeter s'il ne change pas avec RANGE LEN - mono fichier #part1145
PASTE de FICHIERS et de LISTES (ZIP)
  1. PASTE de 2 fichiers #part1146
  2. PASTE de plusieurs fichiers #part1147
  3. PASTE de 2 listes : ZIP (old) #part1148
MAITRISER LA COLONNE ET LA LIGNE
  1. Ajouter des colonnes RANGE LEN #part1149
  2. Capturer des lignes si les patterns d'une colonne changent RANGE LEN #part1150
LES OCCURRENCES : compter chaque mot d'un fichier
  1. Compter les occurrences de chaque mot d'un fichier [var crochet] #part1151
  2. Compter les occurrences de chaque mot d'un fichier (count) #part1152
  3. Compter les occurrences de chaque mot d'un fichier (uniq_words) - tri avec [var crochet] #part1153
  4. Compter les occurrences de chaque mot d'un fichier - dict (cumul+=1) #part1154
LES OCCURRENCES : compter un pattern
  1. Numeroter les lignes qui suivent un meme titre RANGE LEN - FLAG #part1155
  2. Numeroter les lignes qui suivent un meme titre + separer par 1 trait (methode longue : PASTE ZIP) #part1156
  3. Modifier la derniere occurrence d'un pattern d'une ligne SUB #part1157
  4. Compter le nombre de mots pour chaque ligne (ecrit en fin de ligne) LEN FINDALL #part1158
  5. Compter le nombre de plusieurs mots ou d'expressions dans un fichier #part1274
  6. Compter cumuler les occurrences d'un pattern pour chaque ligne SPLIT COUNT - exemple 1/3 #part1159
  7. Compter cumuler les occurrences d'un pattern pour chaque ligne LEN SPLIT - exemple 2/3 #part1160
  8. Compter cumuler les occurrences d'un pattern pour chaque ligne et totaliser - exemple 3/3 #part1161
  9. Compter cumuler les occurrences des patterns en colonne 1 RANGE LEN #part1162
  10. Compter cumuler les occurrences d'un pattern - resultat en fin de fichier #part1163
  11. Repeter une ligne selon un nombre mis dans une colonne #part1164
  12. Capturer le mot le plus long d'un fichier SPLIT LEN #part1165
  13. Marquer toutes les positions d'un pattern choisi - exemple 1 [var crochet] #part1166
  14. Marquer toutes les positions d'un pattern choisi - exemple 2 [var crochet] #part1167
UNIQ - CHAMP
  1. Ajouter des digits 0000 si un champ est vide #part1168
  2. Selectionner le nombre de champs qu'on choisit pour chaque ligne #part1169
  3. Compter le nombre de champs de chaque ligne et regrouper les lignes (avec boucle FOR) #part1170
  4. Compter le nombre de champs de chaque ligne en les regroupant avec LEN LINE - SPLIT #part1171
  5. Supprimer le dernier champ de chaque ligne #part1172
  6. Marquer les lignes selon le nombre de champs - LEN #part1173
  7. Maitriser le nombre de champs pour chaque ligne (WHILE LEN) #part1174
UNIQ - SUPPRIMER DOUBLES
  1. Supprimer les lignes doubles SET ADD (exemple 1/3) #part1175
  2. Supprimer les lignes doubles UNIQ APPEND (exemple 2/3) #part1176
  3. Supprimer les lignes doubles RANGE LEN (exemple 3/3) #part1177
UNIQ - CUMULER OCCURRENCES DE PATTERNS
  1. Occurrences uniques des lignes et renommer les sorties - WHILE LEN #part1178
  2. Occurrences uniques dans une colonne WHILE - POP #part1179
  3. Occurrences uniques dans une colonne WHILE - PASS #part1180
DOUBLES : detecter les lignes doubles
  1. Capturer les lignes doubles (ou triples) RANGE LEN #part1181
  2. Detecter les lignes doubles RANGE LEN #part1182
POP : deleter des elements
  1. Deleter des elements d'une liste POP #part1183
SORT
  1. Trier un fichier en conservant les doubles avec SORTED #part1184
  2. Trier un fichier en deletant les doubles SORTED SET #part1185
  3. Trier un fichier en choisissant la colonne du tri SORT KEY #part1186
  4. Trier une liste de fichiers en conservant les doubles SORT KEY #part1187
CONCATENER
  1. Concatener avec : [var crochet] incluant : if in + replace #part1188
  2. Concatener avec : [var crochet] incluant : SEARCH #part1189
  3. Concatener avec : [var crochet] incluant : SEARCH IFANY ISSPACE STARTSWITH #part1190
  4. Concatener avec : [var crochet] incluant : READLINES #part1191
  5. Concatener avec : [var crochet] incluant : READ SPLITLINES #part1192
  6. Concatener avec : fichier2:readsplitlines + [var crochet] incluant : IFANY #part1193
  7. Concatener avec : READ #part1194
  8. Concatener avec : READ #part1195
  9. Concatener avec : READ SPLITLINES #part1196
  10. Concatener avec : READ SPLITLINES #part1197
  11. Concatener avec : WHILE 1 (+ nom des fichiers devant lignes) #part1198
  12. Concatener avec : WHILE LINE READLINE #part1199
  13. Concatener avec : WHILE LINE READLINE COUNT #part1200
  14. Concatener avec : WHILE LINE READLINE COUNT #part1201
  15. Concatener avec : WHILE LINE READLINE COUNT #part1202
  16. Concatener avec : FOR + SEARCH #part1203
  17. Concatener avec : APPEND #part1204
  18. Concatener et capturer plusieurs patterns dans plusieurs fichiers [var crochet] #part1205
PARAGRAPHE
  1. Capturer des paragraphes si la colonne 1 change avec WHILE 1 - in:monofichier-out:multifichier #part1206
  2. Capturer des paragraphes si la colonne 1 change avec FOR - in:monofichier-out:multifichier #part1207
  3. Capturer des paragraphes avec pattern de debut et de fin - in+out:multifichier #part1208
  4. Capturer des paragraphes avec pattern de debut et de fin - in:monofichier-out:multifichier #part1209
  5. Capturer des paragraphes avec pattern de debut et de fin et rstrip - in:monofichier-out:multifichier #part1210
  6. Capturer des paragraphes avec pattern de debut et de fin WHILE + WHILE - in+out monofichier #part1211
  7. Capturer des paragraphes avec choix sur le pattern de debut #part1212
  8. Capturer des paragraphes avec seulement un pattern de debut #part1213
LE REPERTOIRE CONTENANT DES SOUS REPERTOIRES ET DES FICHIERS
  1. Deleter le repertoire s'il existe ; creer le repertoire s'il n'existe pas ; changer de repertoire #part1214
FICHIER : OUVRIR UN FICHIER EN STRING
  1. Chaque ligne est en STRING #part1215
  2. Chaque mot est en STRING #part1216
  3. Une STRING contient toutes les lignes #part1217
  4. Une STRING contient tous les mots [var crochet] #part1218
FICHIER : OUVRIR UN FICHIER EN LISTE
  1. Chaque ligne est en LISTE #part1219
  2. Une LISTE contient toutes les lignes #part1220
  3. Une LISTE contient tous les mots [var crochet] #part1221
XARGS : chercher un pattern dans fichiers, repertoires, sous-repertoires
  1. XARGS : Rechercher un pattern avec READ et NAME #part1222
  2. XARGS : Rechercher un pattern (depart = liste de fichiers) regex [var crochet] (exemple 1) #part1223
  3. XARGS : Rechercher un pattern : scan d'un seul repertoire [var crochet] (exemple 2) #part1224
  4. XARGS : Rechercher une syntaxe dans des sous repertoires - WALK (ouverture en string) (exemple 3) #part1225
  5. XARGS : Rechercher une syntaxe dans des sous repertoires (ouverture en liste) : exemple complet 1 #part1226
  6. XARGS : Rechercher une syntaxe dans des sous repertoires - WALK (ouverture en liste) [var crochet] exemple complet 2 #part1227
SUB
  1. Modifier la derniere occurrence d'un pattern avec SUB #part1228
  2. Modifier une portion de ligne avec SUB [var crochet] #part1229
  3. Modifier syntaxe avec SUB REPLACE - out multi fichiers #part1230
COLORIER UN FOND DE TEXTE
  1. Colorier plusieurs mots dans un texte #part1231
REPLACE
  1. Remplacer un pattern avec REPLACE #part1232
JOINDRE - ENUMERATE - PASTE
  1. Joindre 2 lignes consecutives sur 1 seule ligne #part1233
  2. Coller 2 files side by side (= PASTE en bash) #part1234
  3. Joindre 2 lignes consecutives, deleter la 3 eme #part1235
  4. Joindre des lignes apres marquage RANGE LEN ENUMERATE #part1236
LISTER ET COMPTER LES FICHIERS D'UN REPERTOIRE
  1. Compter les fichiers d'un repertoire FNMATCH #part1237
  2. Compter les fichiers d'un repertoire APPEND #part1238
  3. Lister un repertoire avec 'getstatusoutput' #part1239
  4. Lister un repertoire avec 'startswith' #part1240
  5. Lister un repertoire avec 'search' #part1241
TESTER - VIDE - GETSIZE - ISSPACE
  1. Supprimer les lignes vides d'un fichier ISSPACE #part1244
  2. Tester si un fichier est vide #part1242
  3. Deleter un fichier s'il est vide #part1243
  4. Creer des fichiers vides dont les noms sont contenus dans un fichier #part1245
  5. Tester si la derniere ligne contient 1 pattern - multi fichiers #part1246
  6. Tester l'existence d'un mot dans un fichier IF ANY #part1247
  7. Tester l'existence d'un mot dans plusieurs fichiers IF ANY #part1248
  8. Tester l'existence d'un mot dans un fichier [var crochet] #part1249
COPIER
  1. Copier un fichier et le renommer avec la date du jour #part1271
  2. Copier un fichier dans un autre repertoire (ou changer de repertoire) #part1250
  3. Copier ou changer de repertoire plusieurs fichiers de meme prefixe dans un autre repertoire #part1251
  4. Copier plusieurs repertoires dans un autre repertoire - WALK FNMATCH #part1252
RENOMMER
  1. Renommer un fichier #part1253
  2. Renommer plusieurs fichiers : exemple 1 #part1254
  3. Renommer plusieurs fichiers : exemple 2 #part1278
  4. Renommer plusieurs fichiers selon correspondance noms anciens-nouveaux #part1255
  5. Renommer plusieurs fichiers en ajoutant 0 devant des digits #part1256
REMOVE
  1. Deleter un fichier (exemple 1) #part1257
  2. Deleter un fichier (exemple 2) #part1258
Inclure une commande BASH dans un script PYTHON
  1. Inclure une commande BASH avec OS.SYSTEM (exemple 1) #part1259
  2. Inclure une commande BASH avec OS.SYSTEM (exemple 2) #part1260
  3. Inclure une commande BASH avec OS.SYSTEM (exemple 3) #part1261
  4. Inclure une commande BASH avec GETSTATUSOUTPUT #part1262
FTP - MAIL
  1. Envoyer un fichier sur un compte FTP #part1263
  2. Envoyer un mail contenant le texte d'un fichier (sans piece attachee) #part1264
  3. Envoyer un mail contenant une piece attachee (texte-image-audio) #part1265
ITERTOOLS : operations math
  1. ITERTOOLS : generateur de imap ifilter chain groupby takewhile dropwhile #part1266
YIELD
  1. YIELD : raccourcit le temps d'execution d'un script #part1267
  2. YIELD genere un compteur (exemple 1) #part1268
  3. YIELD genere un compteur (exemple 2) #part1269

haut

Cumuler et creer des fichiers si limite atteinte WHILE 1 #part1001


#!/usr/bin/python

#@_1001y_cumul_colonne_chiffres_outplusieursfichiers!!while_readline_break_flag_count_cumul
#deb#

import time
import glob
import os

LIMIT=30

TIME=1

IN="in.txt"

# IN
# 8
# 5
# 13
# 3
# 10
# 5
# 16
# 7
# 3

##################

# deleter anciens fichiers

for currentFile in glob.glob('*'):
    e1 = "z-";
    if currentFile.startswith(e1):
        os.remove(currentFile)

print " ---> ok anciens fichiers deletes"
time.sleep(TIME)

###########

count = 1
cumul = 0
fp = open(IN , 'r')     #open#

while 1:       #while#
    flag=1       #flag#
    line = fp.readline()       # lecture ligne par ligne
    time.sleep(TIME)

    if not line:        # bloc d'arret pour la fin du fichier
        break        #break#

    w = int(line.strip())
    cumul = cumul + w

    if cumul > LIMIT:
        flag=0
        count = count + 1
        cumul = w

    flag=1
    o = open("z-" + str(count) + ".txt" , 'a')

    if flag:
        print w , cumul
        print >> o , w , cumul
o.close()

# resultat : creation de 3 fichiers
# z-1.txt
# 8 8
# 5 13
# 13 26
# 3 29

# z-2.txt
# 10 10
# 5 15

# z-3.txt
# 16 16
# 7 23
# 3 26

#fin#
#%_1001

haut

Cumuler et creer des fichiers si limite atteinte WHILE LEN #part1002


#!/usr/bin/python

#@_1002y_cumul_colonne_chiffres_count_outplusieursfichiers!!while_len_read_splitlines_flag_count_cumul
#deb#

import time
import glob
import os

LIMIT=30

TIME=2

IN="in.txt"

# IN
# 8
# 5
# 13
# 3
# 10
# 5
# 16
# 7
# 3

##################

# deleter anciens fichiers

for currentFile in glob.glob('*'):
    e1 = "z-";
    if currentFile.startswith(e1):
        os.remove(currentFile)

print " ---> ok anciens fichiers deletes"
time.sleep(TIME)

###########

k=0
cumul = 0
count = 1
fp = open(IN).read().splitlines()     #openreadsplitlines#

while k < len(fp):       #len#       #while#
    flag=1      #flag#
    time.sleep(TIME)

    w = int(fp[k])
    cumul = cumul + w
    k=k+1

    if cumul > LIMIT:
        flag=0
        count = count + 1
        cumul = w

    flag=1
    o = open("z-" + str(count) + ".txt" , 'a')

    if flag:
        print w , cumul
        print >> o , w , cumul

# resultat identique que le script precedent

#fin#
#%_1002

haut

Cumuler et creer des fichiers si limite atteinte RANGE LEN #part1003


#!/usr/bin/python

#@_1003y_cumul_colonne_chiffres_count_outplusieursfichiers!!readlines_range_len_flag_count_cumul
#deb#

import time
import glob
import os

LIMIT=30

TIME=3

IN="in.txt"

# IN
# 8
# 5
# 13
# 3
# 10
# 5
# 16
# 7
# 3

##################

# deleter anciens fichiers

for currentFile in glob.glob('*'):
    e1 = "z-";
    if currentFile.startswith(e1):
        os.remove(currentFile)

print " ---> ok anciens fichiers deletes"
time.sleep(TIME)

###########

cumul = 0
count = 1
#fp = [line.strip() for line in open(IN)]    # autre syntaxe
fp = open(IN , 'r').readlines()      #openreadlines#

for i in range(len(fp)):      #rangelen#
    flag=1        #flag#
    time.sleep(TIME)

    w = int(fp[i])
    cumul = cumul + w

    if total > LIMIT:
        flag=0
        count = count + 1
        cumul = w

    flag=1
    o = open("z-" + str(count) + ".txt" , 'a')

    if flag:

        print w , cumul
        print >> o , w , cumul

# resultat identique que le script precedent

#fin#
#%_1003

haut

Cumuler les valeurs d'une colonne d'un seul fichier et separer par 1 trait si limite atteinte #part1004


#!/usr/bin/python

#@_1004y_cumul_colonne_chiffre_et_inserer_trait_separe!!while_readlines_split_flag_break_cumul
#deb#

import time
import glob
import os

# BUT : separer par 1 trait quand cumul > LIMIT
# ecriture dans 1 seul fichier

LIMIT=30

IN="in-stm4.txt"
OUT="out-stm5.txt"

TIME=0

# IN :
# 1001 8
# 1002 5
# 1003 13
# 1004 3
# 1005 10
# 1006 5
# 1007 16
# 1008 7
# 1009 3
# end 0     # <--- ligne ajoutee manuellement (oubien avec "write")


# cumul + trait separation  --->  resultat ds 1 seul fichier

cumul = 0

f1 = open (IN , 'r')      #open#
f2 = open (OUT , "w")

while 1:       #while#

    line = f1.readline()

    col0 = line.split()[0]       #split#
    col1 = line.split()[1]

    w = int(col1.strip())
    cumul = cumul + w

    time.sleep(TIME)
    if "end" in line:
        break        #break#

    if cumul < LIMIT:
        flag=1      #flag#

    if cumul > LIMIT:
        flag=1
        cumul = w
        print "----------"
        f2.write("---------------\n")

    if flag:
        print col0 , col1 , cumul
        cumul2 = str(cumul)
        road = col0 , col1 , cumul2
        roadb = ' '.join(road)
        f2.write(roadb + "\n")
f2.close()

# resultat : 1 seul fichier :
# 1001 8 8
# 1002 5 13
# 1003 13 26
# 1004 3 29
# ---------------
# 1005 10 10
# 1006 5 15
# ---------------
# 1007 16 16
# 1008 7 23
# 1009 3 26

#fin#
#%_1004

haut

Cumuler les valeurs d'une colonne d'un seul fichier #part1005


#!/usr/bin/python

#@_1005y_cumul_colonne_de_digits!!forinopen_split_cumul
#deb#

IN="in.txt"
OUT="out.txt"

# IN :
# 52 /nfs/u/comp-new1-segou-segin.txt
# 4 /nfs/u/1-0-CONCAT-NEWY-10-28.txt
# 32 /nfs/u/1-0-DICO-WR-UKFR-10-26.txt
# 6 /nfs/u/1-0-entier-parag-WR-FR-10-26.txt

# BUT = cumul des valeurs

cumul  = 0
newlist = [line.strip() for line in open (IN)]    #forinopen#     # ouvrir lignes en liste
#print newlist
fw = open(OUT , 'w')
for line in newlist:
    w = line.split(" ")[0]       #split#
    w2 = int(w)
    cumul = cumul + w2
    cumul2 = str(cumul)
print cumul
fw.write(cumul2)
fw.close()

# resultat :
# 94

#fin#
#%_1005

haut

Cumuler les valeurs d'une colonne de plusieurs fichiers [var crochet] #part1006


#!/usr/bin/python

#@_1006y_cumul_colonne_de_digits_inoutplusieursfichiers!!forinlistdir_cumul_split_forinopen
#deb#

import os

PATH="/Python27/tests-2018-04"

# x-1.txt :
# 5
# 10

# x-2.txt :
# 7
# 16


newlist1 = [i for i in os.listdir(PATH) if i[0:2] == "x-"]      #forinlistdir#
#print newlist
for file1 in newlist1:

    cumul  = 0
    f2 = open("y" + file1 , 'w')

    var=[      #var=[#
    line1
    for line1 in open (file1)       #forinopen#
    ] #finduvar#

    for i in var:
        j = int(i)
        cumul = cumul + j
    print cumul
    cumul2 = str(cumul)
    f2.write(cumul2)
f2.close()

# resultat :
# yx-1.txt
# 15

# yx-2.txt
# 23

#fin#
#%_1006

haut

Cumuler les valeurs d'une colonne avec remise a zero a une limite fixee #part1007


#!/usr/bin/python

#@_1007y_cumul_valeurs_d_1_colonne_avec_remise_a_zero!!while_split_read_splitlines_shutil_copy_cumul_sum_splitlines
#deb#

import os
import glob
import time
import shutil

TIME=1

IN="data.txt"
ORIG="0hyd-start.txt"

LIMIT=20
LOOPNB=5

################

# ORIG :
# 1001 5
# 1002 7
# 1003 3
# 1004 6
# 1005 9
# 1006 4
# 1007 11
# 1008 5
# 1009 4
# 1010 8


# deleter anciens resultats

for currentFile in glob.glob('*'):
    e1 = "2a-in-";
    if currentFile.startswith(e1):
        os.remove(currentFile)

for currentFile in glob.glob('*'):
    e2 = "3a-out-";
    if currentFile.startswith(e2):
        os.remove(currentFile)

#############################

# sauvegarder l'original

shutil.copy2(ORIG, '2a-in-1.txt')       #copy#

print " ---> ok copie de l'original ds : 2a-in-1.txt"
time.sleep(TIME)

##################

# cumul de colonne

i = 1
while i < LOOPNB:       #while#
    print ""
    print "#########  CUMUL : " , i , "    depart de la boucle i = " , i
    cumul = 0

    with open("2a-in-" + str(i) + ".txt" , 'r') as f2:     #withopen#
        o = open("3a-out-" + str(i) + ".txt" , "a")
        for line in f2:
            col1 = line.split()[0]       #split#
            col2 = line.split()[1]
            cumul += int(col2)
            if cumul < LIMIT:

                print >> o , col1 , cumul
    f2.close()
    o.close()

    with open("3a-out-" + str(i) + ".txt" , "r") as h:
        new = open("2a-in-" + str(i+1) + ".txt" , 'w')
        new = sum(1 for _ in h)
        print " --- nb lignes du cumul 3a-out- [:var]   : " , new , " jusqu'a LIMIT = " , LIMIT

        data1 = open("3a-out-" + str(i) + ".txt").read().splitlines()       #openreadsplitlines#
        data1b = data1[:new]
        for z1 in data1b:
            print z1

        data2 = open("2a-in-" + str(i) + ".txt").read().splitlines()
        data2b = data2[new:]
        print " --- ce qui reste a cumuler dans les fichiers 2a-in-  [new:]  jusqu'a LIMIT = " , LIMIT , " : "
        for z2 in data2b:
            print z2
            new.write(z2 + "\n")
        i += 1
    new.close()

# on peut ensuite concatener les fichiers resultats : 3a-out-...txt
# en separant chaque fichier par un trait de separation -------

# resultat :
# #########  CUMUL :  1     depart de la boucle i =  1
# --- nb lignes du cumul 3a-out-  [:new] :  3  jusqu'a LIMIT =  20
# 1001 5
# 1002 12
# 1003 15
# --- ce qui reste a cumuler dans les fichiers 2a-in-  [new:]   jusqu'a LIMIT =  20  :
# 1004 6
# 1005 9
# 1006 4
# 1007 11
# 1008 5
# 1009 4
# 1010 8

# #########  CUMUL :  2     depart de la boucle i =  2
# --- nb lignes du cumul 3a-out-  [:new]  :  3  jusqu'a LIMIT =  20
# 1004 6
# 1005 15
# 1006 19
# --- ce qui reste a cumuler dans les fichiers 2a-in-  [new:]   jusqu'a LIMIT =  20  :
# 1007 11
# 1008 5
# 1009 4
# 1010 8

# #########  CUMUL :  3     depart de la boucle i =  3
# --- nb lignes du cumul 3a-out-  [:new]  :  2  jusqu'a LIMIT =  20
# 1007 11
# 1008 16
# --- ce qui reste a cumuler dans les fichiers 2a-in-  [new:]   jusqu'a LIMIT =  20  :
# 1009 4
# 1010 8

# #########  CUMUL :  4     depart de la boucle i =  4
# --- nb lignes du cumul 3a-out-  [:new]  :  2  jusqu'a LIMIT =  20
# 1009 4
# 1010 12
# --- ce qui reste a cumuler dans les fichiers 2a-in-  [new:]   jusqu'a LIMIT =  20  :

#fin#
#%_1007

haut

Compter le nombre de lignes d'un fichier : SUM LEN #part1008


#!/usr/bin/python

#@_1008y_compter_lignes_d'1_fichier!!sum_readlines_len_count
#deb#

IN="in.txt"

# IN :
# data1 line1
# data2 line2
# data3 line3

# methode 1 :

num_lines1 = sum(1 for line in open (IN))     #sumforinopen#    # count
print "nombre de lignes du fichier : " , num_lines1    # 3

# methode 2 :

fd = open(IN).readlines()     #openreadlines#
num_lines2 = int(len(fd))       #len#
print "nombre de lignes du fichier : " , num_lines2    # 3

# resultat :
# nombre de lignes du fichier :  3

#fin#
#%_1008

haut

Compter le nombre de lignes d'un fichier : LEN #part1009


#!/usr/bin/python

#@_1009y_count_nombre_de_lignes_d_1_fichier_exemple_1!!len_readlines_count
#deb#

START="in.txt"
# she plans to attempt @@ the record again in June
# his aim is @@ to get rich quickly

# methode 1 :
linecount = len(open(OUT , 'r').readlines())        #openreadlines#       #len#
print "le fichier : " , OUT , " a %d lignes" % linecount

# print "nb lignes du fichier : " , OUT , " : " , linecount

# methode 2 :
with open (START, 'r') as f:        #withopen#
    linecount = len(f.readlines())
    print linecount

# resultat
# 2

#fin#
#%_1009

haut

Compter le nombre de lignes d'un fichier : BUF SIZE #part1010


#!/usr/bin/python

#@_1010y_count_nombre_de_lignes_d_1_fichier!!read_while_count_size
#deb#

START="in.txt"

# START
# she plans to attempt @@ the record again in June
# his aim is @@ to get rich quickly

f = open (START)    #open#
lines = 1
buf_size = 1024 * 1024
read_f = f.read
buf = read_f(buf_size)

if not buf:
    print "not buf"
while buf:       #while#
    lines += buf.count('\n')
    buf = read_f(buf_size)
    print lines - 1

f.close()

# resultat
# 2

#fin#
#%_1010

haut

Compter le nombre de lignes de plusieurs fichiers : CUMUL LEN #part1011


#!/usr/bin/python

#@_1011y_count_nombre_lignes_inplusieursfichiers!!forinlistdirif_splitlines_len_join_cumul
#deb#

import os
import glob
import time

OUT="out.txt"

PATHHOME="/Python27/tests-2017-11"

TIME=0.5

# z-1.txt :
# line1
# line2

# z-2.txt :
# data1
# data2
# data3


# deleter les anciens resultats

for currentFile in glob.glob('*'):
    e1 = "out-3009";
    if currentFile.startswith(e1):
        os.remove(currentFile)

print " ---> ok anciens fichiers deletes"
time.sleep(TIME)

##################

# nb lignes de plusieurs fichiers

newlist = [i for i in os.listdir(PATHHOME) if i[0:2] == "z-"]      #forinlistdir#
#print newlist

for file in newlist:
    file=file.strip()
    f24 = open(OUT , "a")

    cumul=0
    f23 = open(file , 'r')      #open#
    for lines in f23:
        w = lines.splitlines()
        cumul=cumul+len(w)       #len#

    print file , " : nb lignes : " , str(cumul)
    road = file , " : nb lignes : " , str(cumul)
    road2 = ' '.join(road)
    f24.write(road2 + "\n")
f24.close()

# resultat :
# z-1.txt  : nb lignes :  2
# z-2.txt  : nb lignes :  3

#fin#
#%_1011

haut

Numeroter les lignes d'un fichier avec WHILE LINE #part1012


#!/usr/bin/python

#@_1012y_numeroter_lignes_d'1_fichier!!while_readline_count
#deb#

IN="in.txt"
OUT2="out2.txt"

# IN :
# data1 line1
# data2 line2
# data3 line3

count = 1
fyw = open(OUT2 , 'w')     #open#
f = open(IN , 'r')
line = f.readline()
while line:       #while#
    print str(count) , line.strip()
    y = str(count)
    y2 = y + " " + line.strip()
    line = f.readline()
    fyw.write(y2 + "\n")
    count +=1
fyw.close()

# resultat :
# 1 data1 line1
# 2 data2 line2
# 3 data3 line3

#fin#
#%_1012

haut

Numeroter les lignes d'un fichier avec WHILE 1 #part1013


#!/usr/bin/python

#@_1013y_numeroter_lignes_d_1_fichier_avec_WHILE 1!!while_readline_count_break
#deb#

IN="2-in.txt"
OUT3="out3.txt"

# IN :
# data1 line1
# data2 line2
# data3 line3


count = 0
fd = open(IN , 'r')     #open#
fzw = open(OUT3 , 'w')
while 1:       #while#
    line = fd.readline()
    print count+1 , line.strip()
    z2 = str(count+1)
    z3 = z2 + " " + line.strip()
    fzw.write(z3 + "\n")
    count +=1
    if not line:
        break        #break#
fzw.close()

# resultat :
# 1 data1 line1
# 2 data2 line2
# 3 data3 line3

#fin#
#%_1013

haut

Compter nombre de lignes contenant plusieurs patterns dans un fichier #part1014


#!/usr/bin/python

#@_1014y_count_lignes_contenant_plusieurs_patterns_dans_un_seul_fichier!!count_join
#deb#

PATTERN1="to"
PATTERN2="at"

IN="in.txt"
OUT="out.txt"

# IN
# to make an attempt at doing something - at doing something
# she made every attempt to put him at ease


count1=0
count2=0
f42 = open (IN ,'r')         #open#
f43 = open(OUT , 'w')

for line42 in f42:
    if PATTERN1 in line42:
        count1=count1+1

    if PATTERN2 in line42:
        count2=count2+1

print "nb de lignes contenant : " ,  PATTERN1 , " : " , count1
print "nb de lignes contenant : " ,  PATTERN2 , " : " , count2

t1 = str(count1)
t2 = str(count2)

w1 = "nb de lignes contenant : " , PATTERN1 , " : " , t1
w1b = ''.join(w1)

w2 = "nb de lignes contenant : " , PATTERN2 , " : " , t2
w2b = ''.join(w2)

f43.write(w1b + "\n" + w2b)
f43.close()

# resultat :
# nb de lignes contenant :  to  :  2
# nb de lignes contenant :  at  :  2

#fin#
#%_1014

haut

Compter nombre de lignes contenant plusieurs patterns dans plusieurs fichiers #part1015


#!/usr/bin/python

#@_1015y_count_plusieurs_patterns_dans_plusieurs_fichiers!!forinlistdirif
#deb#

import os

PATTERN1="#op#"
PATTERN2="#closed#"

PATH="/Python27/tests-2018-05d"


# a-part1.txt :
# part901 @@%@@ #op#
# part901 @@%@@
# part901 @@%@@ import time
# part901 @@%@@ import glob
# part901 @@%@@ import os
# part901 @@%@@
# part901 @@%@@ LIMIT=30
# part901 @@%@@ #closed#
# part901 @@%@@
# part901 @@%@@ #%_1015


# a-part2.txt :
# part903 @@%@@ #op# #op#
# part903 @@%@@
# part903 @@%@@ for currentFile in glob.glob('*'):
# part903 @@%@@     e1 = "z-";
# part903 @@%@@     if currentFile.startswith(e1):
# part903 @@%@@         os.remove(currentFile)
# part903 @@%@@ #closed#
# part903 @@%@@
# part903 @@%@@ #%_1015

##########

count1=0
count2=0
newlist = [k for k in os.listdir(PATH) if k[0:6] == "a-part"]      #forinlistdir#
for file in newlist:

    f = open(file , 'r')

    for line in f:

        if PATTERN1 in line:
            print line.strip()
            count1=count1+1

        if PATTERN2 in line:
            print line.strip()
            count2=count2+1

print ""
print "nb de lignes contenant #op# : " , count1
print "nb de lignes contenant #closed# : " , count2

# resultat :
# part901 @@%@@ #op#
# part901 @@%@@ #closed#
# part903 @@%@@ #op# #op#
# part903 @@%@@ #closed#

# nb de lignes contenant #op# :  2
# nb de lignes contenant #closed# :  2

#fin#
#%_1015

haut

Compter directement des patterns (ecrits ds 1 fichier a part) dans plusieurs fichiers #part1016


#!/usr/bin/python

#@_1016y_count_dans_plusieurs_fichiers_plusieurs_patterns_stockes_dans_fichier!!read_splitlines_forinlistdir
#deb#

import os

GOODWORDS="1-good_words.txt"

VERIF1="2-verif-start-end1.txt"
VERIF2="2-verif-start-end2.txt"

PATTERN1="#op#"
PATTERN2="#closed#"

# HOME
PATH="/Python27/tests-2018-05d"

TIME=1

# 1-good_words.txt
# #op#
# #closed#


# a-part1.txt
# part901 @@%@@ #op#
# part901 @@%@@
# part901 @@%@@ import time
# part901 @@%@@ import glob
# part901 @@%@@ import os
# part901 @@%@@
# part901 @@%@@ LIMIT=30
# part901 @@%@@ #closed#
# part901 @@%@@
# part901 @@%@@ # 901


# a-part2.txt
# part903 @@%@@ #op# #op#
# part903 @@%@@
# part903 @@%@@ for currentFile in glob.glob('*'):
# part903 @@%@@     e1 = "z-";
# part903 @@%@@     if currentFile.startswith(e1):
# part903 @@%@@         os.remove(currentFile)
# part903 @@%@@ #closed#
# part903 @@%@@
# part903 @@%@@ # 903


# 1/2_ capture des LIGNES contenant les patterns   ---> liste de LIGNES

f41 = open(VERIF1 , 'w')
good_words = open(GOODWORDS).read().splitlines()           #openreadsplitlines#

newlist40 = [k for k in os.listdir(PATH) if k[0:6] == "a-part"]      #forinlistdir#
for new40 in newlist40:


    var=[        #var=[#
    line1
    for line1 in open(new40)        #forinopen#
    if any(good_word in line1 for good_word in good_words)       #ifany#
    ] #finduvar#

    w1 = ''.join(var)
    print w1                # print les lignes

#    w2 = new40 , w1
#    w2b = ''.join(w2)
    f41.write(w1 + "\n")
f41.close()

print "--------------------"
#################################

# 2/2_ capture des PATTERNS directement    ---> liste de PATTERNS

count1=1
count2=1
f42 = open (VERIF1 ,'r')
f43 = open (VERIF2 ,'w')

for line42 in f42:
    line42 = line42.strip()
    w = line42.split()
    for z in w:
	if any(good_word in z for good_word in good_words):
            print line42.split("@@%@@")[0] , z

# resultat :
# part901 @@%@@ #op#
# part901 @@%@@ #closed#

# part903 @@%@@ #op# #op#
# part903 @@%@@ #closed#

# --------------------
# part901  #op#
# part901  #closed#
# part903  #op#
# part903  #op#
# part903  #closed#

#fin#
#%_1016

haut

Compter les occurrences d'1 caractere pour chaque ligne #part1017


#!/usr/bin/python

#@_1017y_count_occurrence_d'1_caractere_pour_chaque_ligne!!read_splitlines_split
#deb#

import re

IN="1-data1b.txt"

OUT1="2-out1.txt"
OUT1B="2-out1b.txt"

OUT2="2-out2.txt"
OUT3="2-out3.txt"
OUT4="2-out4.txt"
OUT5="2-out5.txt"


PATTERN="&"         # 1 seul caractere                # 1/1 : changer ici

# IN :
# part901  @@@  && LE CUMUL D'UNE COLONNE && Cumuler
# part904  @@@ & LE CUMUL D'UNE COLONNE && Cumuler
# part905  @@@  LE CUMUL D'UNE COLONNE && Cumuler


################################

# 1/6_ numeroter les lignes

count = 1
fyw = open(OUT1 , 'w')     #open#
f = open(IN , 'r')
line = f.readline()
while line:       #while#
#    print "line" , str(count) ,  " @%@ "  , line.strip()
    y = str(count)
    y2 = "line " + y + " @%@ " + line.strip()       # line1 part4001  @@@
    line = f.readline()
    fyw.write(y2 + "\n")
    count +=1
fyw.close()

#################################

# 2/6_ ajouter digits '0' devant les numeros

with open(OUT1 , "r") as fww:      #withopen#         # OUT1="2-out1.txt"
    fd4b = open (OUT1B , "w")
    for line3b in fww:
        b = line3b.split("@%@")[0].split("line")[1].strip()       #split#
        c = line3b.split("@%@")[1].strip()

        if len(b) == 1:            #len#
#            print "line00" + ''.join(b) + " " + c               # line001 part4001  @@@
            fd4b.write("line00" + ''.join(b) + " " + c + "\n")

        if len(b) == 2:
#            print "line0" + ''.join(b) + " " + c
            fd4b.write("line0" + ''.join(b) + " " + c + "\n")

        if len(b) == 3:
#            print "line" + ''.join(b) + " " + c
            fd4b.write("line" + ''.join(b) + " " + c + "\n")

fd4b.close()

################################

# 3/6_ occurrence du PATTERN    (ici pour chaque ligne)

#count=1              # count place ici : cumul de toutes les occurrences de toutes les lignes
f3 = open(OUT2 , 'w')
mylist = open(OUT1B).read().splitlines()       #openreadsplitlines#

for line in mylist:
    count=1           # count place ici : remet le compteur a zero apres chaque ligne
    for k in line:

        if k == PATTERN:
            z = str(count)
            w = line.split()[0]  , " @ occur : " , z , " " , k
            w2 = ''.join(w)
            f3.write(w2 + "\n")
            count+=1
f3.close()

#####################################

# 4/6_ grouper

d={}       #dic#
f32 = open (OUT3 , "w")              # OUT3="2-out3.txt"
for line31 in open (OUT2):     #open#

    line31=line31.strip().split(" @ ")       #split#
    d.setdefault(line31[0],[])        #set#
    d[line31[0]].append(line31[1])       #append#

for i,j in d.iteritems():
#    print i + " @@@ " + " ".join(j)
    f32.write(i + " @@@ " + " ".join(j) + "\n")
f32.close()

####################

# 5/6_ trier

fd = open (OUT3 , 'r')
fw = open (OUT4 , 'w')

t = fd.read().splitlines()

d = sorted(t)     # (t , reverse=True)     # tri en ordre inverse        #sort#

# print '\n'.join(d)
fw.write('\n'.join(d))
fw.close()

########################

# 6/6_ capturer la premiere et derniere colonne

f36 = open(OUT5 , 'w')        # OUT5="2-out5.txt"
f35 = open(OUT4 , 'r')
for line35 in f35:
    line35 = line35.strip()

    z1 = ' '.join(re.findall(r'\b\w+\b', line35)[:1])        #  1ere colonne     #find#

    z2 = ' '.join(re.findall(r'\b\w+\b', line35)[-1:])        # derniere colonne

    print z1 , " : nb occurrences de : " , PATTERN , " : " , z2
    new = z1 , " : nb occurrences de : " , PATTERN , " : " , z2
    new2 = ''.join(new)
    f36.write(new2 + "\n")
f36.close()

# resultat :
# line001  : nb occurrences de :  &  :  4
# line002  : nb occurrences de :  &  :  3
# line003  : nb occurrences de :  &  :  2

#fin#
#%_1017

haut

Compter les occurrences d'1 mot pour chaque ligne #part1018


#!/usr/bin/python

#@_1018y_count_occurrence_d'1_mot_pour_chaque_ligne!!read_splitlines_split
#deb#

import re

IN="1-0-0-HORIZONTAL-mini.txt"

OUT1="2-out1.txt"
OUT1B="2-out1b.txt"

OUT2="2-out2.txt"
OUT3="2-out3.txt"
OUT4="2-out4.txt"
OUT5="2-out5.txt"


PATTERN="fichier"         # mot recherche   # ecrit en minuscule ou majuscule ( avec  "(?i)")              # 1/1 : changer ici

# IN :
# part904  @@@  LE CUMUL D'UNE COLONNE && Cumuler les valeurs d'une colonne mono fichier + trait separe si limite atteinte - mono fichier
# part905  @@@  LE CUMUL D'UNE COLONNE && Cumuler les valeurs d'une colonne d un seul fichier - sans remise a 0 - mono fichier
# part906  @@@  LE CUMUL D'UNE COLONNE && Cumuler les valeurs d'une colonne de plusieurs fichiers [var crochet]
# part909  @@@  LA LIGNE : compter - numeroter && Compter le nombre de lignes d un fichier : SUM LEN
# part910  @@@  LA LIGNE : compter - numeroter && Compter le nombre de lignes d un fichier : LEN


################################

# 1/6_ numeroter les lignes

count = 1
fyw = open(OUT1 , 'w')     #open#
f = open(IN , 'r')
line = f.readline()
while line:            #while#
#    print "line" , str(count) ,  " @%@ "  , line.strip()
    y = str(count)
    y2 = "line " + y + " @%@ " + line.strip()
    line = f.readline()
    fyw.write(y2 + "\n")
    count +=1
fyw.close()

#################################

# 2/6_ ajouter digits '0' devant les numeros

with open(OUT1 , "r") as fww:      #withopen#         # OUT1="2-out1.txt"
    fd4b = open (OUT1B , "w")
    for line3b in fww:
        b = line3b.split("@%@")[0].split("line")[1].strip()       #split#
        c = line3b.split("@%@")[1].strip()

        if len(b) == 1:                #len#
#            print "line00" + ''.join(b) + " " + c               # line001 part4001  @@@
            fd4b.write("line00" + ''.join(b) + " " + c + "\n")

        if len(b) == 2:
#            print "line0" + ''.join(b) + " " + c
            fd4b.write("line0" + ''.join(b) + " " + c + "\n")

        if len(b) == 3:
#            print "line" + ''.join(b) + " " + c
            fd4b.write("line" + ''.join(b) + " " + c + "\n")

fd4b.close()

################################

# 3/6_ occurrence du PATTERN    (ici pour chaque ligne)

#count=1
f3 = open(OUT2 , 'w')
mylist = open(OUT1B).read().splitlines()       #openreadsplitlines#

for line in mylist:
    count=1               # count place ici : remet le compteur a zero apres chaque ligne
    new = line.split()        #split#
    for k in new:
        if k == PATTERN:
            z = str(count)
            w = line.split()[0] , " @ occur : " , z
            w2 = ''.join(w)
            f3.write(w2 + "\n")
            count+=1
f3.close()

#####################################

# 4/6_ grouper

d={}       #dic#
f32 = open (OUT3 , "w")              # OUT3="2-out3.txt"
for line31 in open (OUT2):     #open#

    line31=line31.strip().split(" @ ")
    d.setdefault(line31[0],[])        #set#
    d[line31[0]].append(line31[1])       #append#

for i,j in d.iteritems():
#    print i + " @@@ " + " ".join(j)            # line001 @@@ occur : 1 # occur : 2 #
    f32.write(i + " @@@ " + " ".join(j) + "\n")
f32.close()

####################

# 5/6_ trier

fd = open (OUT3 , 'r')
fw = open (OUT4 , 'w')

t = fd.read().splitlines()

d = sorted(t)     # (t , reverse=True)     # tri en ordre inverse        #sort#

# print '\n'.join(d)
fw.write('\n'.join(d))
fw.close()

########################

# 6/6_ capturer la premiere et derniere colonne

f36 = open(OUT5 , 'w')        # OUT5="2-out5.txt"
f35 = open(OUT4 , 'r')
for line35 in f35:
    line35 = line35.strip()

    z1 = ' '.join(re.findall(r'\b\w+\b', line35)[:1])        #  1ere colonne     #find#

    z2 = ' '.join(re.findall(r'\b\w+\b', line35)[-1:])        # derniere colonne

    print z1 , " : nb occurrences du mot : " , PATTERN , " : " , z2
    new = z1 , " : nb occurrences du mot : " , PATTERN , " : " , z2
    new2 = ''.join(new)
    f36.write(new2 + "\n")
f36.close()

# resultat :
# line001  : nb occurrences du mot  :  fichier  :  2
# line002  : nb occurrences du mot  :  fichier  :  2
# line004  : nb occurrences du mot  :  fichier  :  1
# line005  : nb occurrences du mot  :  fichier  :  1

#fin#
#%_1018

haut

Compter le nombre de mots d'un fichier #part1019


#!/usr/bin/python

#@_1019y_count_nombre_mots_d_1_fichier!!split_len_count_cumul
#deb#

import os
import re

# in.txt :
# data 1
# data 2
# data 3

cumul=0
f = open ('in.txt' ,'r')      #open#
fw = open('out.txt' , 'w')

for lines in f:
    f1=lines.split()        #split#       # ajouter   lines()   # pour compter lignes
    cumul=cumul+len(f1)       #len#

print 'word count :', str(cumul)
road = str(cumul)
fw.write(road)
fw.close()

# resultat :
# word count : 6

#fin#
#%_1019

haut

Compter les visites a l'ouverture du script #part1020


#!/usr/bin/python

#@_1020y_count_visites_ouverture_du_script!!read_count_visite
#deb#

# creer un fichier  compteur.txt  contenant : 0

fichier = open("compteur.txt" , "r")     #open#
nbr_visiteurs = int(fichier.read())

fichier = open("compteur.txt" , "w")
fichier.write(str(nbr_visiteurs + 1))
print nbr_visiteurs+1 , " visites"
fichier.close()

# resultat :
# 3 visites    # augmente de 1 a chaque execution du script

#fin#
#%_1020

haut

Inserer un trait de separation avant un pattern (exemple 1) #part1021


#!/usr/bin/python

#@_1021y_inserer_trait_separation_avant_pattern_exemple1!!join
#deb#

IN="1-in.txt"
OUT="1-out.txt"

# BUT : inserer un trait de separation si 1 pattern est present sur 1 ligne
#             au debut ou milieu de la ligne

# in.txt :
# 32 FR Selon la profondeur de l'eau dans laquelle se deplace le tsunami
# 32 UK Depending on the depth of the water in which the tsunami is traveling
# 35 FR Tous ces effets sont bien connus et peuvent se produire lorsque la conscience se deplace dans un etat entre endormir completement et completement eveille.
# 35 UK All of these effects are well known and may occur when one's consciousness shifts into a state between being fully asleep and fully awake .

with open (IN , 'r') as road2:     #withopen#
    fd3 = open (OUT , 'w')
    for ligne2 in road2:
        if "FR" in ligne2:
            ligne2 = "---------------\n" , ligne2
            ligne2b = ''.join(ligne2)
            print ligne2b.strip()
            fd3.write(ligne2b)
        else:
            ligne2c = ''.join(ligne2)
            print ligne2c.strip()
            fd3.write(ligne2c)
fd3.close()
road2.close()

# resultat :
# ---------------
# 32 FR Selon la profondeur de l'eau dans laquelle se deplace le tsunami
# 32 UK Depending on the depth of the water in which the tsunami is traveling
# ---------------
# 35 FR Tous ces effets sont bien connus et peuvent se produire lorsque la conscience se deplace dans un etat entre endormir completement et completement eveille.
# 35 UK All of these effects are well known and may occur when one's consciousness shifts into a state between being fully asleep and fully awake .

#fin#
#%_1021

haut

Inserer un trait de separation avant un pattern (exemple 2) #part1022


#!/usr/bin/python

#@_1022y_inserer_trait_separation_avant_1_pattern!!replace
#deb#

# in.txt :
# y-1150012.txt @
# data1
# y-1150012.txt @
# data2
# y-1150012.txt @
# data3

with open ("in.txt" , 'r') as road2:      #withopen#
    fd3 = open ("out.txt" , 'w')
    for ligne2 in road2:
        ligne2 = ligne2.replace("y-", "-------------\ny-")       #replace#
        print ligne2.strip()
        fd3.write(ligne2)
fd3.close()
road2.close()

# resultat
# -------------
# y-1150012.txt @
# data1
# -------------
# y-1150012.txt @
# data2
# -------------
# y-1150012.txt @
# data3

#fin#
#%_1022

haut

Inserer un trait de separation dans un fichier si la colonne 1 change RANGE LEN #part1023


#!/usr/bin/python

#@_1023y_inserer_trait_separation_ds_1_fichier_si_colonne_1_change!!readlines_range_len_split_startswith_break
#deb#

# zin1.txt :
# accepter      @@      accept      @@      transitive verb
# accepter      @@      agree      @@      transitive verb
# accommoder      @@      accommodate      @@      transitive verb
# accompagner      @@      attend      @@      transitive verb
# accomplir      @@      accomplish      @@      transitive verb
# accomplir      @@      achieve      @@      transitive verb


# 1/2 : ajouter des lignes a la fin

open ("zin2.txt" , "w").write( open ("zin1.txt").read() + "end\nend\n")     #openread#

######################

# 2/2 : inserer 1 trait a chaque changement ds colonne 1

fd30 = open ("zin2.txt" , 'r').readlines()       #openreadlines#
fd31 = open ("zin3.txt" , "w")

for i in range(len(fd30)):      #rangelen#

    element1 = fd30[i].split(" ")[0]       #split#
    element2 = fd30[i+1].split(" ")[0]
    if element2 == element1:
        print fd30[i].strip()
        fd31.write(fd30[i])

    if element2 != element1:
        print fd30[i] , "--------------------------------------"
        fd31.write(fd30[i] + "--------------------------------------\n")

    if element2.startswith("end"):   #startswith#
        break        #break#

fd31.close()

# resultat :
# accepter      @@      accept      @@      transitive verb
# accepter      @@      agree      @@      transitive verb
# --------------------------------------
# accommoder      @@      accommodate      @@      transitive verb
# --------------------------------------
# accompagner      @@      attend      @@      transitive verb
# --------------------------------------
# accomplir      @@      accomplish      @@      transitive verb
# accomplir      @@      achieve      @@      transitive verb
# --------------------------------------

#fin#
#%_1023

haut

Inserer un signe sur une ligne si la colonne 1 change : selectionner les doubles, triples .. #part1275


#!/usr/bin/python

#@_1275y_count_numeroter_lignes_qui_suivent_1_meme_signe_avec_remise_a_zero!!readlines_range_len_startswith_count_break
#deb#

# inserer un signe sur une ligne si la colonne 1 change et selectionner les doubles, triples ...

RESULT12="7-file1.txt"
RESULT13="7-file2.txt"
RESULT14="7-file3.txt"
RESULT15="7-file4.txt"
RESULT16="7-file5.txt"
RESULT17="7-file6.txt"

# RESULT12 :
# toto
# toto
# data1
# data1
# data1
# data2
# data3
# data3
# data4
# data5
# data5
# data5
# Jn 15,1-8
# Jn 15,1-8

########################

# 1/5 ajouter "end" en fin de fichier

open (RESULT13 , "w").write( open (RESULT12).read() + "end")       #openread#

#########################

# 2/5 separer par un signe les groupes semblables : ajouter un signe si la colonne 1 change

fd13 = open (RESULT13).readlines()       #openreadlines#
fd14 = open (RESULT14 , "w")

for i in range(len(fd13)):      #rangelen#
    
    if fd13[i+1] != fd13[i]:     #startswith#
        print fd13[i].strip() , "\n%"
        fd14.write(fd13[i] + "%\n")

    if fd13[i+1] == fd13[i]:     #startswith#
        print fd13[i].strip()
        fd14.write(fd13[i])

    if fd13[i+1].startswith("end"):
        break                                   #break#
fd14.close()

print "------------------------------"

#####################################

# 3/5 numeroter les lignes avec remise a zero au signe : %

count = 1

fd15 = open (RESULT14).readlines()       #openreadlines#
fd16 = open (RESULT15 , "w")

for i in range(len(fd15)):      #rangelen#

    if fd15[i].startswith("%"):     #startswith#
        print fd15[i].strip()
        fd16.write(fd15[i])
        count = 1

    if not fd15[i].startswith("%"):              #startswith#
	print count , fd15[i].strip()
        fd16.write(str(count) + " " + fd15[i])
        count = count + 1

fd16.close()

print "------------------------------"

######################################

# 4/5 ajouter "end" en fin de fichier

open (RESULT16 , "w").write( open (RESULT15).read() + "end")     #openread#

##################################

# 5/5 ne garder que les doubles, triples, quadruples ...

fd17 = open (RESULT16).readlines()       #openreadlines#
fd18 = open (RESULT17 , "w")

for i in range(len(fd17)):      #rangelen#
    
    if fd17[i+1].startswith("%") and fd17[i].startswith("1"):     #startswith#
        pass

    if not (fd17[i+1].startswith("%") and fd17[i].startswith("1")):      #startswith#
        print fd17[i].strip()
        fd18.write(fd17[i])

    if "end" in fd17[i+1]:
        break                     #break#
fd18.close()

# resultat :
# 1 toto
# 2 toto
# %
# 1 data1
# 2 data1
# 3 data1
# %
# %
# 1 data3
# 2 data3
# %
# %
# 1 data5
# 2 data5
# 3 data5
# %
# 1 Jn 15,1-8
# 2 Jn 15,1-8
# %

#fin#
#%_1275

haut

Inserer un trait de separation dans un fichier si la colonne 3 change RANGE LEN #part1024


#!/usr/bin/python

#@_1024y_inserer_un_trait_de_separation_si_colonne3_change!!read_readlines_range_len_break_split
#deb#


# in4.txt : 6 colonnes
# S @ 97-2520 @ 4968 163148
# S @ 97-2520 @ 4969 163159
# S @ 97-2520 @ 4970 163210
# S @ SF1724 @ 717 152056
# S @ SF1724 @ 718 152106
# S @ SF1724 @ 1870 184159
# S @ SF1724 @ 1871 184212
# S @ SF1732 @ 721 215615
# S @ SF1732 @ 722 215626
# S @ SF1732 @ 1865 014230
# S @ SF1732 @ 1866 014242
# S @ SF1740 @ 721 075653
# S @ SF1740 @ 722 075703
# S @ SF1740 @ 723 075714
# S @ SF1740 @ 724 075723


# 1/2_ ajouter 2 lignes a la fin du fichier:

open ("in5.txt" , "w").write ( open("in4.txt").read() + "end @ end @ end\n")       #openread#

#######

# 2/2_ tableau : capture des premieres et dernieres lignes ayant meme pattern en colonne 3

fw2 = open ("out2.txt","w")
f2 = open ("in5.txt").readlines()        #openreadlines#

for i in range(len(f2)):      #rangelen#

    if "end" in f2[i+1]:
        break        #break#

    if f2[i+1].split("@")[1] != f2[i].split("@")[1]:       #split#

        print f2[i].strip()
        print "---"
        print f2[i+1].strip()

        m1 = f2[i].strip()
        m2 = "---"
        m3 = f2[i+1].strip()

#        print m1 + "\n" + m2 + "\n" + m3 + "\n"
        fw2.write(m1 + "\n" + m2 + "\n" + m3 + "\n")
fw2.close()

# resultat :
# S @ 97-2520 @ 4970 163210
# ---
# S @ SF1724 @ 717 152056
# S @ SF1724 @ 1871 184212
# ---
# S @ SF1732 @ 721 215615
# S @ SF1732 @ 1866 014242
# ---
# S @ SF1740 @ 721 075653

#fin#
#%_1024

haut

Inserer un trait de separation dans un fichier si la colonne 1 change - zip paste shift #part1025


#!/usr/bin/python

#@_1025y_inserer_trait_separation_ds_1_fichier_si_colonne_1_change!!splitlines_zip_split_break_read_join
#deb#

IN="1-separe-trait2.txt"
PASTE="1-separe-trait-paste.txt"
RESULT="1-separe-trait-result.txt"

# IN :        # contient plusieurs colonnes

# accepter     %     accept     %     transitive verb @@
# accepter     %     agree     %     transitive verb @@
# accommoder     %     accommodate     %     transitive verb @@
# accompagner     %     attend     %     transitive verb @@
# accomplir     %     accomplish     %     transitive verb @@
# accomplir     %     achieve     %     transitive verb @@
# end @@
# end @@


# shift de la ligne 1 + PASTE (affichage cote a cote)
#      + insertion d'1 trait de separation

f1 = open (RESULT ,'w')

data = open(IN , 'r').read().splitlines()     #openreadsplitlines#
datanew = data[1:]                  # ligne 1 supprimee

w = zip(data , datanew)             # creation du PASTE
for j in w:
    road = ' '.join(j)

    line1 = road.split("@@")[0]       #split#
    line2 = road.split("@@")[1]

    col1line1 = line1.split()[0]
    col1line2 = line2.split()[0]

    if col1line1 == col1line2:
        print line1
        f1.write(line1 + "\n")

    if col1line1 != col1line2:
        print line1 , "\n--------------"
        road = line1 , "\n--------------"
        road2 = ''.join(road)
        f1.write(road2 + "\n")

    if "end" in road:
        break        #break#

f1.close()

# resultat :

# accepter     %     accept     %     transitive verb
# accepter     %     agree     %     transitive verb
# --------------
# accommoder     %     accommodate     %     transitive verb
# --------------
# accompagner     %     attend     %     transitive verb
# --------------
# accomplir     %     accomplish     %     transitive verb
# accomplir     %     achieve     %     transitive verb
# --------------
# end

#fin#
#%_1025

haut

Inserer un trait de separation dans un fichier si (n+1 < n) en colonne 3 (cumul deja fait) #part1026


#!/usr/bin/python

#@_1026y_inserer_trait_separation_ds_1_fichier_si_colonne_3_change!!readlines_range_len_split_break_tableau
#deb#

CONCAT1="4-inter1.txt"
CONCAT2="4-inter2.txt"
CONCAT3="4-inter3.txt"

################

# CONCAT1
# 1001 5 5
# 1002 7 12
# 1003 3 15
# 1004 6 6
# 1005 9 15
# 1006 4 19
# 1007 11 11
# 1008 5 16
# 1009 4 4
# 1010 8 12

# BUT :  separer par 1 trait si n+1 < n en colonne 3 (avec 1 tableau)

# 1/2 : ajouter des lignes a la fin (pour eviter un message d'erreur)

open (CONCAT2 , "w").write(open (CONCAT1).read() + "end 0 0\nend 0 0\n")      #openread#

######

# 2/2 : inserer 1 trait a chaque changement ds colonne 3

fd30 = open (CONCAT2 , 'r').readlines()      #openreadlines#
fd31 = open (CONCAT3 , "w")
for i in range(len(fd30)):      #rangelen#

    element1 = fd30[i].split()[2]       #split#
    element2 = fd30[i+1].split()[2]
    w = int(element2) - int(element1)
    wb = str(w)

    if not "-" in wb:
        print fd30[i].strip()
        fd31.write(fd30[i])

    if "-" in wb:
        print fd30[i].strip() , "\n---------------------"
        fd31.write(fd30[i] + "---------------------" + "\n")

    if "end" in fd30[i]:
        break        #break#
fd31.close()

# resultat :
# 1001 5 5
# 1002 7 12
# 1003 3 15
# ---------------------
# 1004 6 6
# 1005 9 15
# 1006 4 19
# ---------------------
# 1007 11 11
# 1008 5 16
# ---------------------
# 1009 4 4
# 1010 8 12
# ---------------------
# end 0 0

#fin#
#%_1026

haut

Inserer un trait de separation dans un fichier si (n+1 = n+1) en colonne 3 (cumul deja fait) #part1027


#!/usr/bin/python

#@_1027y_inserer_trait_separation_ds_1_fichier_si_2_colonnes_egales!!range_len_split_join_break_read_tableau
#deb#

DATA1="1-file1.txt"
DATA2="1-file2.txt"
DATA3="1-file3.txt"


# CONCAT1 :
# 1001 8 8
# 1002 6 14
# 1003 14 28
# 1004 3 3
# 1005 11 14
# 1006 5 19
# 1007 17 17
# 1008 7 24
# 1009 4 28
# 1010 10 10
# 1011 7 17


# ajouter 1 ligne au debut et a la fin de la concatenation

open(DATA2 , 'w').write("stm stm stm\n" + open(DATA1).read() + "end end end")      #openread#

#############################

# utilisation d'un tableau
# inserer trait si n+1 = n+1 en colonne 3

fd12 = open (DATA2 , 'r').readlines()      #openreadlines#
fd13 = open (DATA3 , 'w')
for i in range(len(fd12)):      #rangelen#

    element0 = fd12[i].split()[0]       #split#
    element1 = fd12[i].split()[1]
    element2 = fd12[i].split()[2]

    element3 = fd12[i+1].split()[1]
    element4 = fd12[i+1].split()[2]
    time.sleep(TIME)

    if element3 == element4:
        print element0 , element1 , element2 , "\n------------"
        road1 = element0 , element1 , element2 , "\n------------"
        road1b = ' '.join(road1)
        fd13.write(road1b + "\n")

    if element3 != element4:
        print element0 , element1 , element2
        road2 = element0 , element1 , element2
        road2b = ' '.join(road2)
        fd13.write(road2b + "\n")

    if "end" in fd12[i+1]:
        break        #break#

# resultat :
# stm stm stm
# ------------
# 1001 8 8
# 1002 6 14
# 1003 14 28
# ------------
# 1004 3 3
# 1005 11 14
# 1006 5 19
# ------------
# 1007 17 17
# 1008 7 24
# 1009 4 28
# ------------
# 1010 10 10
# 1011 7 17
# ------------

#fin#
#%_1027

haut

Ajouter une ligne vide ou ecrite au debut ou a la fin d'un seul fichier (exemple court) #part1028


#!/usr/bin/python

#@_1028y_ajouter_lignes_debut_ou_fin_d_1_fichier_one_line!!read
#deb#

# 1/2_ ajouter ligne au debut d'1 fichier

open(OUT , "w").write("start\n" + open(IN).read())    #openread#

# 2/2_ ajouter ligne a la fin d'1 fichier

open(OUT , "w").write(open(IN).read() + "end\n")

#fin#
#%_1028

haut

Ajouter une ligne au debut et a la fin d'un seul fichier #part1029


#!/usr/bin/python

#@_1029y_ajouter_lignes_debut_et_fin_d_1_fichier !!rien
#deb#

import re

IN="in.txt"
OUT="out.txt"

# IN
# line1 data1
# line2 todelete data2
# line3 hello data3


fw = open(OUT , 'w')
with open(IN , 'r') as f1:     #withopen#
    print "start\nstart"
    fw.write("start\nstart" + "\n")

    for line in f1:
        print line.strip()
        fw.write(line)

    print "end\nend"
    fw.write("end\nend" + "\n")

f1.close()
fw.close()

# resultat :
# start
# start
# line1 data1
# line2  todelete data2
# line3 hello data3
# end
# end

#fin#
#%_1029

haut

Ajouter une ligne au debut et a la fin de plusieurs fichiers (methode rapide) #part1030


#!/usr/bin/python

#@_1030y_ajouter_1_ligne_au_debut_ou_fin_inoutplusieursfichiers!!forinlistdirif_read
#deb#

import os

# in1.txt :
# data part3238
# data part3238

# in2.txt :
# data part973
# data part973

# ajouter 1 ligne a la fin de plusieurs fichiers

newlist1 = [i for i in os.listdir(PATH) if i[0:2] == "in"]      #forinlistdir#
#print newlist1

for file1 in newlist1:

    open("z-" + file1 , 'w').write(open(file1).read() + "end\nend\n")     #openread#

# resultat :
# in1.txt :
# data part3238
# data part3238
# end
# end

# in2.txt :
# data part973
# data part973
# end
# end

#fin#
#%_1030

haut

Ajouter des lignes au debut et a la fin de plusieurs fichiers [var crochet] #part1031


#!/usr/bin/python

#@_1031y_ajouter_lignes_debut_et_fin_de_inoutplusieursfichiers!!forinlistdirif_forinopen_join
#deb#

import os

# HOME
PATH="/Python27/2017-09"

# z-1.txt
# 1001 8 8
# 1002 6 14

# z-2.txt
# 1004 3 3
# 1005 11 14

newlist = [i for i in os.listdir(PATH) if i[0:2] == "z-"]    # i[-4:] == ".txt"       #forinlistdir#
#print newlist

for file1 in newlist:
    fz1 = open("y-" + file1 , 'w')

    print "start ---"
    fz1.write("start ---" + "\n")    # ecrit "start" au debut des fichiers y-

    var=[line1 for line1 in open(file1)]    #forinopen#      #var=[#
    #finduvar#
    print ''.join(var)
    print "end ---"
    w1b = ''.join(var)
    fz1.write(w1b)
    fz1.write("end ---" + "\n")    # ecrit "end" a la fin des fichiers y-
fz1.close()

# resultat : creation de fichiers  y-
   # y-z-1.txt :
# start ---
# 1001 8 8
# 1002 6 14
# end ---

   # y-z-2.txt :
# start ---
# 1004 3 3
# 1005 11 14
# end ---

#fin#
#%_1031

haut

Ajouter un meme chiffre a toutes les fins de lignes #part1032


#!/usr/bin/python

#@_1032y_ajouter_1_meme_chiffre_fin_de_ligne!!read_split_join
#deb#

# z.txt :
# 40

# in.txt :
# line1416@ veils  2
# line1417@ ventures    venturing    ventured  4
# line1418@ verifies    verifying    verified  4
# line1419@ vibrates    vibrating    vibrated    vibrations  5


new = open("z.txt" , 'r').read().split()     #openreadsplit#

f6 = open("in.txt" , 'r')       #open#
f7 = open("out2.txt" , 'w')
for line6 in f6:
    line6 = line6.strip()
    for j in new:

        print line6 , "  " , j
        road = line6 , "  " , j
        road2 = ''.join(road)
        f7.write(road2 + "\n")

f7.close()

# resultat : out2.txt
# line1416@ veils  2  40
# line1417@ ventures    venturing    ventured  4  40
# line1418@ verifies    verifying    verified  4  40
# line1419@ vibrates    vibrating    vibrated    vibrations  5  40

#fin#
#%_1032

haut

Ajouter du texte au debut ou fin de ligne d'un fichier avec condition MATCH #part1033


#!/usr/bin/python

#@_1033y_ajouter_texte_en_debut_ou_fin_de_ligne!!replace_format_match
#deb#

# 1_ si une ligne debute par '(espace)[a-zA-Z]' alors la marquer devant par '%% (espace)'
# 2_ marquer toutes les fins de ligne par '@!!@'

import re

START="1-in.txt"
OUT="1-out.txt"

# START
# @ arise
# intransitive verb
# 1.     ( appear ,  happen )     survenir ,    se presenter
#  a doubt arose in his mind   un doute est apparu dans son esprit
#  if the need arises   en cas de besoin
#  if the occasion arises   si l'occasion se presente
# 2.     ( result )   resulter

with open(START, 'r') as before:      #withopen#
    town = open (OUT , "w")
    for line in before:
        new = line.replace("\n" , "")       #replace#
        new2 = "{0} @!!@".format(new)

        if re.match('^ [a-zA-Z]', line):      #match#
            syntaxe1 = ("%% " + new2.strip())
            print syntaxe1
            town.write("{0}\n".format(syntaxe1))
        else:
            syntaxe2 = new2.strip()
            print syntaxe2
            town.write("{0}\n".format(syntaxe2))

before.close()
town.close()

# resultat
# @ arise  @!!@
# intransitive verb  @!!@
# 1.     ( appear ,  happen )     survenir ,    se presenter   @!!@
# %% a doubt arose in his mind   un doute est apparu dans son esprit  @!!@
# %% if the need arises   en cas de besoin   @!!@
# %% if the occasion arises   si l'occasion se presente  @!!@
# 2.     ( result )   resulter   @!!@

#fin#
#%_1033

haut

Ajouter des digits "0" devant des chiffres pour qu'ils aient tous la meme longueur avec LEN #part1034


#!/usr/bin/python

#@_1034y_ajouter_0_devant_chiffres_pour_1_meme_longueur!!split_len_join
#deb#

IN="2-in.txt"
OUT="2-out.txt"

# IN
# 5 : data1 line1
# 13 : data2 line2
# 514 : data3 line3

# ajouter des digits '0' devant des chiffres

with open(IN , "r") as fww:      #withopen#
    fd4b = open (OUT , "w")
    for line3b in fww:
        b = line3b.split(" : ")[0]       #split#
        c = line3b.split(" : ")[1].strip()
        if len(b) == 1:       #len#
            print "00" + ''.join(b) + " " + c
            fd4b.write("00" + ''.join(b) + " " + c + "\n")

        if len(b) == 2:
            print "0" + ''.join(b) + " " + c
            fd4b.write("0" + ''.join(b) + " " + c + "\n")

        if len(b) == 3:
            print "" + ''.join(b) + " " + c
            fd4b.write("" + ''.join(b) + " " + c + "\n")

fww.close()
fd4b.close()

# resultat
# 0005 : data1 line1
# 0013 : data2 line2
# 0514 : data3 line3

#fin#
#%_1034

haut

Ajouter une colonne de prepos au contenu d'un fichier avec out multi fichiers #part1035


#!/usr/bin/python

#@_1035y_ajouter_1_colonne_de_prepos_au_contenu_d_1_fichier_avec_out_multi_fichiers!!forinlistdir_read_splitlines
#deb#

import os

PREPO="1-prepo.txt"

PATH="/Python27/tests-2018-02"

# fichiers :
# w-abandoned.txt      # contenant :  abandoned
# w-allowed.txt      # contenant :  allowed

# PREPO :
# aboard
# about
# above


newlist = [i for i in os.listdir(PATH) if i[0:2] == "w-"]      #forinlistdir#
#print newlist

for file1 in newlist:
    new = open (file1).read().splitlines()     #openreadsplitlines#
    for k in new:
#        print (k)
        f2 = open(PREPO , 'r')
        f3 = open("vw-" + k + ".txt" , 'w')    # out multi fichiers
        for i in f2:
            i = i.strip()
            print k , i.strip()
            road = k + " " + i
            f3.write(road + "\n")
f3.close()

# resultat :

# vw-abandoned.txt :
# abandoned aboard
# abandoned about
# abandoned above

# vw-allowed.txt :
# allowed aboard
# allowed about
# allowed above

#fin#
#%_1035

haut

Grouper horizontalement toutes les lignes ayant meme colonne 1 - dic (entree mono fichier) #part1036


#!/usr/bin/python

#@_1036y_grouper_horizontalement_tout_sur_1_ligne_si_meme_colonne_1_mono_fichier!!dic_setdefault_append_iteritems_replace_join
#deb#

# line1@abandons
# line1@abandoning
# line1@abandoned
# line1@-
# line1@-
# line1115@ringing
# line1115@ring
# line1115@rings
# line1115@ringing
# line1115@ringed
# line1115@-
# line1115@-

# utiliser un dictionnaire

d={}       #dic#
f32 = open ("out.txt" , "w")
for line31 in open ("in.txt"):     #open#
#    print line31.strip()

    line31=line31.strip().split("@")       #split#
    d.setdefault(line31[0],[])        #set#
    d[line31[0]].append(line31[1])       #append#

for i,j in d.iteritems():
    i = i.replace("line" , "line ")       #replace#
    print i + " @@@ " + " ".join(j)
    f32.write(i + " @@@ " + " ".join(j) + "\n")
f32.close()

# resultat :
# line 1115 @@@ ringing ring rings ringing ringed - - - -
# line 1 @@@ abandons abandoning abandoned - - -

#fin#
#%_1036

haut

Grouper horizontalement toutes les lignes ayant meme colonne 1 - dic (entree multi fichiers) #part1037


#!/usr/bin/python

#@_1037y_grouper_horizontalement_tout_sur_1_ligne_si_meme_colonne_1_inplusieursfichiers!!dic_forinopen_split_setdefault_append_iteritems_join
#deb#

LISTE="liste-files.txt"
OUT="out.txt"

# BUT : grouper tout sur 1 ligne

# LISTE :
# file1.txt
# file2.txt

# file1.txt
# abide  @@@  tolerate
# abide  @@@  last
# abide  @@@  stay

# file2.txt
# accept  @@@  receive
# accept  @@@  take on
# accept  @@@  say yes to
# accept  @@@  acknowledge
# accept  @@@  stand
# accept  @@@  welcome

# utiliser un dictionnaire

d={}        #dic#
fout = open (OUT , "w")
for w in open (LISTE):      #open#
    w = w.strip()
    print w

    for line in open (w):
        line = line.strip().split("@@@")       #split#
        d.setdefault(line[0],[])       #set#
        d[line[0]].append(line[1])       #append#

for i,j in d.iteritems():

    print i + "@@@" + " -".join(j)
    fout.write(i + "@@@" + " -".join(j) + "\n")

fout.close()

# resultat : OUT
# accept  @@@  receive -  take on -  say yes to -  acknowledge -  stand -  welcome
# abide  @@@  tolerate -  last -  stay

#fin#
#%_1037

haut

Grouper tout le fichier sur une seule ligne (entree multi fichiers) #part1038


#!/usr/bin/python

#@_1038y_grouper_tout_le_fichier_sur_1_ligne_de_inplusieursfichiers!!forinlistdir_join
#deb#

import os
import glob

PATH="/Python27/tests-2018-02"


# v-abide.txt :
# to accept or submit to; suffer ;
# to comply (with) ;
# to withstand or sustain; endure ;

# v-account.txt :
# money deposited at a bank ;
# they transferred their publicity account to a new agent ;
# to consider or reckon ;

##########

# deleter ancien fichier

for currentFile in glob.glob('*'):
    e1 = "out";
    if currentFile.startswith(e1):
        os.remove(currentFile)

################

newlist = [i for i in os.listdir(PATH) if i[0:3] == "v-a"]      #forinlistdir#
#print newlist

for file1 in newlist:
    file1=file1.strip()

    fw = open ("out.txt" , "a")

    with open(file1) as f2:      #withopen#
        w = " ".join(line1.strip() for line1 in f2)
        print file1 , " : " , w
        w2 = file1 , " : " , w
        w3 = ''.join(w2)
        fw.write(w3 + "\n")
fw.close()

# resultat :
# v-abide.txt  :  to accept or submit to; suffer ; to comply (with) ; to withstand or sustain; endure ;
# v-account.txt  :  money deposited at a bank ; they transferred their publicity account to a new agent ; to consider or reckon ;

#fin#
#%_1038

haut

Degrouper : coller la colonne 1 a chaque autre colonne #part1039


#!/usr/bin/python

#@_1039y_degrouper_coller_col1_aux_autres_col!!read_splitlines_split
#deb#

# in.txt
# line1481@ writes    writing    wrote
# line1483@ sings  singing    sang


mylist3 = open("in.txt").read().splitlines()        #openreadsplitlines#
f30 = open("out.txt" , 'w')
for i3 in mylist3:
    for word3 in i3.split():    # pour chaque mot        #split#
        if "@" in word3:
            mark = word3      # marqueur = colonne1
        if not "@" in word3:
            print mark , word3
            w = mark + word3
            f30.write(w + "\n")
f30.close()

# resultat
# line1481@ writes
# line1481@ writing
# line1481@ wrote
# line1481@ -
# line1483@ sings
# line1483@ singing
# line1483@ sang
# line1483@ -
# line1483@ -

#fin#
#%_1039

haut

Modifier ligne horizontale en colonne verticale - out multi fichiers #part1040


#!/usr/bin/python

#@_1040y_modifier_ligne_horizontale_en_colonne_verticale_inoutplusieursfichiers!!forinlistdir_split_replace_sub
#deb#

import os
import re
import time
import glob

PATH="/Python27/tests-2018-03"

# r-cle1.txt :
# cle1 @@@ 4037,4041,4085,4086

# r-cle2.txt :
# cle2 @@@ 4033,4034,4081,4101

newlist3 = [i for i in os.listdir(PATH) if i[0:2] == "r-"]      #forinlistdir#
#print newlist3

newlist4 = sorted(newlist3)        #sort#

for file6 in newlist4:
    file6 = file6.strip()
    file6b = file6.split("-")[1].split(".")[0]       #split#
    print "----------"
    f27 = open("q-" + file6b + ".txt" , 'w')
    f26 = open(file6 , 'r')         #open#
    for line26 in f26:

#        line26 = line26.replace("," , "\n")      # cette syntaxe suffit pour 1 seul fichier       #replace#

        line26 = line26.replace("," , "\n").replace("@@@ " , "@@@ \n")
        line26 = re.sub('.*? @@@' , '' , line26)        #sub#
        line26 = re.sub(' \n' , '' , line26)
        print line26
        f27.write(line26)
f27.close()

# resultat :
# q-cle1.txt :
# 4037
# 4041
# 4085
# 4086

# q-cle2.txt :
# 4033
# 4034
# 4081
# 4101

#fin#
#%_1040

haut

Capturer des caracteres en fin de ligne oubien colonnes #part1041


#!/usr/bin/python

#@_1041y_capture_ligne_colonne_tableau!!tableau_readlines_isdigit_join
#deb#

import os
import re
import time
import glob

IN="in2.txt"
OUT="out.txt"
OUT2="out2.txt"

# IN
# line1 user
# line2 25
# line3 data
# line4 100

# en colonne - verticale : capture des caracteres de fin de ligne

f2 = open(OUT , "w")
f1 = open(IN , 'r')       #open#
for line in f1:
    line = line.strip()
    print line[-3:]
    f2.write(line[-3:] + "\n")

    if line[-1:].isdigit():      #isdigit#
        print line.strip()
        f2.write(line + "\n")
f2.close()

# resultat :
# ser
#  25
# line2 25
# ata
# 100
# line4 100

#####

# capturer en ligne horizontale : capture ciblee de lignes

print ""

f3 = open(IN , 'r').readlines()       #openreadlines#
f4 = open(OUT2 , 'w')

# w = f3[-2]        # avant derniere ligne seulement
w1 = f3[-2:]       # les 2 dernieres lignes
w1a = ''.join(w1)
print w1a            # derniere ligne
f4.write(w1a)

# resultat :
# line3 data
# line4 100

#fin#
#%_1041

haut

Substituer des patterns selon un fichier pattern-correspondance #part1042


#!/usr/bin/python

#@_1042y_substituer_patterns_selon_fichier_pattern_correspondance!!forinopen_split
#deb#

#    in.txt :
# attempt
# attempt
# attend
# avail
# bring

#    start.txt :
# attempt attempts
# attend attends
# avail avails
# avert averts

f2 = open("out.txt" , 'w')
f1 = open("in.txt" , 'r')      #open#
for line1 in f1:
    line1 = line1.strip()
#    print (line1)

    for linecor in open("start.txt" , 'r'):       #forinopen#

        k1 = linecor.split()[0]       #split#
        k2 = linecor.split()[1]

        if line1 == k1:
            print k2
            f2.write(k2 + "\n")
f2.close()

#    resultat :  ajout d'un 's' dans les lignes de in.txt
#    noter que 'bring' n'est pas dans   start.txt  donc n'est pas dans le resultat
# attempts
# attempts
# attends
# avails

#fin#
#%_1042

haut

Capturer une ligne si un pattern est contenu dans un autre fichier avec FOR #part1043


#!/usr/bin/python

#@_1043y_capture_ligne_si_pattern_est_contenu_dans_un_autre_fichier!!read_split_readlines
#deb#

import os
import sys

# in.txt :
# applying a function or
# approaching or entering
# approaching exemple we raised
# approving attitude good will
# arguing until I arrived
# arising from my actions
# arising in the imagination
# arising out of land

# z.txt :
# aboard
# about
# above
# from
# in
# out
# until


prepo = open ("z.txt").read().split()       #openreadsplit#

f3 = open ("out.txt" , 'w')
ff = open ("in.txt" , 'r').readlines()      #openreadlines#

for j in prepo:
    print j
    for text2 in ff:

        z1 = text2.split(" ")[1].strip()       #split#

        if j == z1:
            print text2.strip()
            f3.write(text2)
f3.close()

# resultat :
# arguing until I arrived
# arising from my actions
# arising in the imagination
# arising out of land

#fin#
#%_1043

haut

Capturer une ligne si un pattern est contenu dans un autre fichier avec (IF ANY) #part1044


#!/usr/bin/python

#@_1044y_capture_ligne_si_pattern_contenu_dans_1_fichier!!read_splitlines_any
#deb#

# 1-ing.txt :
# working as
# working for

# 1-start.txt :
# The protesters went to Washington to march against increased taxes.
# I'm working as a teaching assistant to support myself as a graduate student in order to get an advanced degree.
# Let us mark the occasion by opening a bottle of champagne.
# An advantage of working for the business is that they provide a pension scheme for their employees.
# Wear this oxygen mask till the pressure returns to normal.


# capturer dans  1-start.txt  les patterns contenus dans cette liste : good_words
good_words = open ("1-ing.txt").read().splitlines()       #openreadsplitlines#

with open("1-start.txt") as oldfile:       #withopen#
    newfile = open ("1-out.txt" , 'w')
    for line in oldfile:
#        print line.strip()

        if any(good_word in line for good_word in good_words):     #ifany#
            print line.strip()
            newfile.write(line)
oldfile.close()
newfile.close()

# resultat :
# I'm working as a teaching assistant to support myself as a graduate student in order to get an advanced degree.
# An advantage of working for the business is that they provide a pension scheme for their employees.

#fin#
#%_1044

haut

Capturer une ligne si un pattern est contenu dans un autre fichier avec MATCH #part1045


#!/usr/bin/python

#@_1045y_capture_ligne_si_patterns_contenus_dans_1_fichier_avec_MATCH!!read_split_readlines_match
#deb#

import re

START="z-start.txt"
# Aimez la justice , vous qui gouvernez la terre
# car il se laisse trouver par ceux qui ne le mettent pas a l' epreuve
# sa puissance confond les insenses qui la provoquent
# Car la Sagesse ne peut entrer dans une ame qui veut le mal


PATTERN="1-1-select-site.txt"
# justice
# puissance

OUT="7-result.txt"

words = open (PATTERN).read().split()      #openreadsplit#

fs = open (START, 'r')
t = fs.readlines()         #openreadlines#

out = open(OUT , 'w')

for i in words:          # mots recherches
    print i
    out.write(i + "\n")
    for s in t:
        m = re.match(("(.* )" + '(?i)' + i + "(.*)") , s)      #match#
        if m:
            print s.strip()
            out.write(s)
    print "-----------------------------------------------------------"
    out.write("-----------------------------------------------------------" + "\n")
fs.close()
out.close()

# resultat :
# justice
# Aimez la justice , vous qui gouvernez la terre
# -----------------------------------------------------------
# puissance
# sa puissance confond les insenses qui la provoquent
# -----------------------------------------------------------

#fin#
#%_1045

haut

Capturer des paragraphes si la colonne 1 change avec WHILE 1 #part1046


#!/usr/bin/python

#@_1046y_paragraphes_crees_si_colonne_1_change_outplusieursfichiers!!readline_while_break_startswith
#deb#

import os
import glob

LETTRE="1-alpha.txt"

IN1="1-in1.txt"
IN2="1-in2.txt"

# LETTRE :
# a
# b


# deleter anciens resultats

for currentFile in glob.glob('*'):
    e1 = "zzz-";
    if currentFile.startswith(e1):
        os.remove(currentFile)

#########

# ajouter 1 ligne vide en ligne 1

open (IN2 , "w").write (" \n" + open (IN1).read())      #openread#

##############################

# creation d'autant de fichiers que de lettres : a b ..

f1 = open(LETTRE , 'r')       #open#
for lettre in f1:
    lettre = lettre.strip()
#    print lettre

    f2 = open(IN2 , 'r')
    line2 = f2.readline()      #openreadline#

    while 1:       #while#
        line2 = f2.readline()
        line2 = line2.rstrip()

        if not line2:
            break        #break#

        if line2.startswith(lettre):    #startswith#
            o = open("zzz-" + lettre + ".txt" , 'a')
            print line2
            print >> o , line2
o.close()

# resultats : creation de 2 fichiers
# zzz-a.txt :
# allowed
# abduct
# abducted

# zzz-b.txt :
# behaviour
# behind
# being

#fin#
#%_1046

haut

Marquer des lignes si contenues dans un autre fichier INTERSECTION #part1047


#!/usr/bin/python

#@_1047y_marquer_lignes_si_contenues_ds_2eme_fichier!!read_splitlines_set_intersection_join
#deb#

import os
import re

# BUT : comparer 2 listes
# si des elements d'1 petite liste (DELETE) sont contenus dans 1 grande liste (IN)
# et les marquer par 'NO'

DELETE="delete.txt"

IN="in.txt"

OUT="out.txt"

# IN :
# abandon
# abandoned
# abet
# abhor
# abide

# DELETE  (ses elements vont marquer 'NO' ds IN)
# abhor


fw = open(OUT , "w")

big = open(IN).read().splitlines()
small = open(DELETE).read().splitlines()      #openreadsplitlines#

com = set(big).intersection(set(small))     #set#

all = set(big)

other = list(all - com)

for i in other:
    print i , "@@"
    m1 = i , "@@"
    m1b = ' '.join(m1)
    fw.write(m1b + "\n")

for j in com:
    print j , "NO"
    m2 = j , "NO"
    m2b = ' '.join(m2)
    fw.write(m2b + "\n")

fw.close()

# resultat :
# abandon @@
# abet @@
# abandoned @@
# abide @@
# abhor NO

#fin#
#%_1047

haut

Supprimer des lignes si des mots sont dans un autre fichier avec IF ANY #part1048


#!/usr/bin/python

#@_1048y_deleter_lignes_si_des_mots_sont_dans_1_fichier!!read_splitlines_any_inforin
#deb#

IN = "in.txt"
OUT = "out.txt"
DELETE = "z-delete.txt"

# BUT : des patterns dans une liste vont deleter des lignes les contenant

# data1 = c2 9c
# data2 = c3 a9
# data3 = c3 ae
# data4 = c3 a7
# data5 = c3 a0

# DELETE :
# data1 =
# data2 =

bad_words = open (DELETE).read().splitlines()        #openreadsplitlines#

# print bad_words

with open(IN) as oldfile:       #withopen#
    newfile = open (OUT , 'w')
    for line in oldfile:
        if not any(bad_word in line for bad_word in bad_words):       #ifany#
            print line.strip()
            newfile.write(line)

oldfile.close()
newfile.close()

# resultat :
# data3 = c3 ae
# data4 = c3 a7
# data5 = c3 a0

#fin#
#%_1048

haut

Supprimer des lignes de plusieurs fichiers si des mots sont dans un fichier #part1049


#!/usr/bin/python

#@_1049y_deleter_lignes_inoutplusieursfichiers_selon_1_fichier!!read_splitlines_listdir_sort_forinopen_any_inforin_join
#deb#

import os

PATHHOME="/Python27/tests-2017-10"

BADWORDS="1-delete.txt"

# y-001.txt
# data 1
# data 2
# 100
# 101

# y-002.txt
# New data 1
# New data 2
# 200
# 201

# BADWORDS :
# data

bad_words = open(BADWORDS).read().splitlines()      #openreadsplitlines#

newlist = [i for i in os.listdir(PATHHOME) if i[0:2] == "y-"]      #forinlistdir#
#print newlist

newlist2 = sorted(newlist)        #sort#

for file1 in newlist2:
    f2 = open("z" + file1 , 'w')
    var=[        #var=[#
    line1
    for line1 in open(file1)      #forinopen#
    if not any(bad_word in line1 for bad_word in bad_words)      #ifany#
    ] #finduvar#

    print ''.join(var)
    w = ''.join(var)
    f2.write(w)

f2.close()

# resultats :
# zy-001.txt :
# 100
# 101

# zy-002.txt :
# 200
# 201

#fin#
#%_1049

haut

Un fichier a 2 colonnes commande l'habillage d'un autre fichier #part1050


#!/usr/bin/python

#@_1050y_commande_fichier_2_colonnes_regle_habillage_d_1_autre_fichier!!while_read_splitlines_len_split_break
#deb#

SEPAR="1-cut.txt"
IN="1-in.txt"
OUT="2-result.txt"

# 1-in.txt :
# S1101 1533 3017
# S1102 1503 3060
# S1103 1478 3094
# S1104 1474 3121
# S1105 1451 3196
# S1106 1434 3239
# S1107 1422 3273
# S1108 1392 3300
# S1109 1354 3374
# S1110 1324 3417

# 1-cut.txt :
# S1101 S1104
# S1105 S1107
# S1108 S1110


f1 = open(SEPAR , 'r')      #open#
fw = open(OUT , 'w')

k=0
for line1 in f1:
    start = line1.split()[0]       #split#
    end = line1.split()[1]

    f2 = open (IN).read().splitlines()      #openreadsplitlines#

    while k < len(f2):        #while#      #len#
        item1 = f2[k].split()[0]
        item1b = f2[k].split()[1]
        print item1 , item1b
        w = item1 + " " + item1b
        fw.write(w + "\n")

        if item1 == end:
            k=k+1
            print "----------"
            fw.write("-------------\n")
            break        #break#
        k=k+1

# resultat :
# S1101 1533
# S1102 1503
# S1103 1478
# S1104 1474
# -------------
# S1105 1451
# S1106 1434
# S1107 1422
# -------------
# S1108 1392
# S1109 1354
# S1110 1324
# -------------

#fin#
#%_1050

haut

Un fichier a 2 colonnes commande le decoupage en plusieurs fichiers (avec debut-fin) #part1051


#!/usr/bin/python

#@_1051y_commande_fichier_regle_decoupage_en_outplusieursfichiers!!while_len_split_splitlines_break_tableau_count_read
#deb#

SEPAR="1-cut.txt"
IN="1-in.txt"
OUT="2-result.txt"

# IN :
# SY1101 1533 3017
# SY1102 1503 3060
# SY1103 1478 3094
# SY1104 1474 3121
# SY1105 1451 3196
# SY1106 1434 3239
# SY1107 1422 3273
# SY1108 1392 3300
# SY1109 1354 3374
# SY1110 1324 3417

# SEPAR :          # (regle le decoupage)
# SY1101 SY1104           # col1 : debut
# SY1105 SY1107           # col2 : fin
# SY1108 SY1110

# utilisation d'1 tableau

f1 = open(SEPAR , 'r')       #open#

k=0
count=1
for line1 in f1:
    start = line1.split()[0]       #split#
    end = line1.split()[1]

    f2 = open (IN).read().splitlines()          #openreadsplitlines#
    fw = open("z-" + str(count) + "-" + start + ".txt" , 'w')

    while k < len(f2):         #while#     #len#
        item1 = f2[k].split()[0]
        item1b = f2[k].split()[1]
        print item1 , item1b
        w = item1 + " " + item1b
        fw.write(w + "\n")

        if item1 == end:
            k=k+1
            print ""
            fw.write("")
            count=count+1
            break        #break#
        k=k+1
fw.close()

# resultat : 3 fichiers
# z-1-SY1101.txt :
# SY1101 1533
# SY1102 1503
# SY1103 1478
# SY1104 1474

# z-2-SY1105.txt :
# SY1105 1451
# SY1106 1434
# SY1107 1422

# z-3-SY1108.txt :
# SY1108 1392
# SY1109 1354
# SY1110 1324

#fin#
#%_1051

haut

Renommer plusieurs fichiers selon un fichier a 2 colonnes #part1052


#!/usr/bin/python

#@_1052y_renommer_inoutplusieursfichiers_selon_fichier_2_colonnes!!split_path_exists_rename
#deb#

import os
import re

# BUT :  renommer les fichiers part... selon le fichier LI5

LI5="1-file-old-new.txt"

# il existe 3 fichiers dont les noms sont au depart :
# part123.txt
# part137.txt
# part151.txt

# LI5 :  (col1 = ancien  //  col2 = nouveau)
# 123  901
# 137  902
# 151  903

with open(LI5 , "r") as f1:       #withopen#
    for row in f1:
        old = row.split()[0]       #split#
        old2 = "part" + old + ".txt"
        new = row.split()[1]
        new2 = "part" + new + ".txt"

        if os.path.exists(old2):        #pathexists#
            os.rename(old2 , new2)
f1.close()

# resultat :  les 3 fichiers ont ete renommes :
# part901.txt
# part902.txt
# part903.txt

#fin#
#%_1052

haut

Modifier une syntaxe selon un 2eme fichier IF ANY #part1272


#!/usr/bin/python

#@_1272y_modifier_mot_syntaxe_avec_2_fichiers!!ifany
#deb#

import time

IN="1-tvorange-light.txt"

LISTETITRES="2-liste-titres-light.txt"

INTER1="3-inter1.txt"
INTER2="3-inter2.txt"
INTER3="3-inter3.txt"

pattern1="logo"

TIME=0

#####################

# IN : (son contenu)

# TNT
# logo_tf1
# 1 . TF1
# logo_FR2
# 2 . France 2

# Information Francaise
# Public Senat 
# 226 . Public Senat 24/24
# Chaine Internationale
# Arte
# 414 . Arte (en allemand)

# Decouverte et Art de vivre
# logo_ultra_nature
# 115 . ULTRA Nature
# Information Internationale
# France 24 anglais
# 232 . France 24 (en anglais)

# Les chaines locales
# Chaine TV des Vosges
# 350 . ViaVosges
# 8 Mont Blanc
# 343 . 8 Mont Blanc

######

# LISTETITRES : (son contenu)

# TNT
# Information
# Cha
# Art de vivre
# locales

######

# retirer les lignes commencant par   logo

f1 = open (IN , 'r')
lines = f1.readlines()       #openreadlines#
f1.close()

f1 = open (INTER1 , 'w')      

for i in lines:
    if not i.startswith(pattern1):    #startswith#
#        print i
        f1.write(i)
f1.close()

print "ok les lignes commencant par  logo  ont ete supprimees , resultat ds :" , INTER1
time.sleep(TIME)

####################

# supprimer les lignes vides

with open (INTER1 , 'r') as fwhab1:       #withopen#
    fwhab2 = open (INTER2 , 'w')
    w2 = "".join(line1 for line1 in fwhab1 if not line1.isspace())        #isspace#
#    print "".join(line1 for line1 in fwhab1 if not line1.isspace())
    fwhab2.write(w2 + "\n")

fwhab1.close() 
fwhab2.close()

print "ok des lignes vides ont ete supprimees , resultat ds :" , INTER2
time.sleep(TIME)

#############################

# ajouter des symboles devant les mots repertories dans le 2eme fichier :

good_words = open (LISTETITRES).read().splitlines()        #openreadsplitlines#

with open(INTER2) as oldfile:        #withopen#
    newfile = open (INTER3 , 'w')
    for line in oldfile:

        if any(good_word in line for good_word in good_words) \
        and not any("TV des Vosges" in line for good_word in good_words):      #ifany# #ifnotany#
            print "\n" , "-------------------------------------"
            print "@@ " , line.strip()
            time.sleep(TIME)            
            w3 = "\n" + "-------------------------------------" + "\n"
            w4 = "@@ " + line
            newfile.write(w3)
            newfile.write(w4)
            continue

        print line.strip()
        time.sleep(TIME)
        newfile.write(line)

oldfile.close()
newfile.close()

# resultat :
# -------------------------------------
# @@  TNT
# 1 . TF1
# 2 . France 2

# -------------------------------------
# @@  Information Francaise
# Public Senat
# 226 . Public Senat 24/24

# -------------------------------------
# @@  Chaine Internationale
# Arte
# 414 . Arte (en allemand)

# -------------------------------------
# @@  Decouverte et Art de vivre
# 115 . ULTRA Nature

# -------------------------------------
# @@  Information Internationale
# France 24 anglais
# 232 . France 24 (en anglais)

# -------------------------------------
# @@  Les chaines locales
# Chaine TV des Vosges
# 350 . ViaVosges
# 8 Mont Blanc
# 343 . 8 Mont Blanc

#fin#
#%_1272

haut

Substituer un pattern selon un 2eme fichier correspondance #part1053


#!/usr/bin/python

#@_1053y_substituer_pattern_selon_deuxieme_fichier_correspondance!!read_splitlines_split_vlookup
#deb#

# dans le cas de substitution dans plusieurs fichiers, concatener les fichiers avant

IN="1-substituer1.txt"
NEW="1-substituer-newlines.txt"
OUT="2-substituer-out.txt"

# IN :
# part926 @@%@@ fw = open (OUT2 , "w")
# part926 @@%@@
# part926 @@%@@ data1
# part926%%%
# --------------

# part999 @@%@@ line1 second file
# part999 @@%@@
# part999 @@%@@ IN="in.txt"
# part999 @@%@@ data2
# part999%%%
# --------------

# NEW :
# part926%%% @@@  ENUMERATE - PASTE
# part999%%% @@@  JOINDRE - Joindre 2 lignes


fw = open(OUT , 'w')

small_list = open(IN).read().splitlines()        #openreadsplitlines#

big_list = open(NEW).read().split()           #openreadsplit#
big_list2 = open(NEW).read().splitlines()

for road in small_list:

    if road in big_list:

        for j in big_list2:
            if road in j:
                print j
                fw.write(j + "\n")

    if road not in big_list:
        print road
        fw.write(road + "\n")
fw.close()

# resultat :
# part926 @@%@@ fw = open (OUT2 , "w")
# part926 @@%@@
# part926 @@%@@ data1
# part926%%% @@@  ENUMERATE - PASTE
# --------------

# part999 @@%@@ line1 second file
# part999 @@%@@
# part999 @@%@@ IN="in.txt"
# part999 @@%@@ data2
# part999%%% @@@  JOINDRE - Joindre 2 lignes
# --------------

#fin#
#%_1053

haut

Supprimer ou modifier des elements d'une liste selon une autre liste RANGE LEN #part1279


#!/usr/bin/python

#@_1279y_comparer_deux_listes_avec_boucle_"range len"_pour_supprimer_element_de_liste !!forinopen_range_len_continue_join
#deb#

import os
import time
import glob

IN="6-in.txt"
DELETE="6-delete.txt"
OUT="6-out.txt"

# IN :
# derniers X8
# diffamation X43
# difficiles X42
# dis X47
# disperse X52
# dit X50
# division X46
# dix X55
# doigt X42
# donner X4

# DELETE :
# diffamation
# disperse
# dix

###########################

# comparer deux listes avec boucle  "range len"  pour supprimer ou modifier un element d'une liste 

fw3 = open (OUT , 'w')

big_list1 = [line1.split(" ")[0].strip() for line1 in open (IN)]        #forinopen#
big_list2 = [line1.strip() for line1 in open (IN)]        #forinopen#

delete_list = [line2.strip() for line2 in open (DELETE)]

for i in range(len(big_list1)):      #rangelen#

    if big_list1[i] in delete_list:
        continue             # supprime la ligne

#        m1 = big_list1[i] , "  ok contenu ds les 2 listes"         # ecriture sur une ligne
#        m1b = ''.join(m1)
#        print m1b
#        fw3.write(m1b + "\n")

    else:
#        m2 = big_list[i] , "  NO"                 # ecriture sur une ligne
        m2 = big_list2[i] 
        m2b = ''.join(m2)
        print m2b
        fw3.write(m2b + "\n")
fw3.close()

# resultat :
# derniers X8
# difficiles X42
# dis X47
# dit X50
# division X46
# doigt X42
# donner X4

#fin#
#%_1279

haut

Verifier si elements d'une petite liste sont dans grande liste RANGE LEN #part1054


#!/usr/bin/python

#@_1054y_vlookup_elements_petite_liste_contenus_dans_grande_liste!!forinopen_range_len_join
#deb#

BIG="z-big.txt"
SMALL="z-small.txt"
OUT="z-out1.txt"

# BIG :
# abandon
# abduct
# abide
# absorb
# abstain
# abstract
# abuse
# accent
# accentuate
# accept

# SMALL :
# abide
# abstract
# accentuate
# awake

fw = open (OUT , 'w')

big_list = [line1.strip() for line1 in open (BIG)]        #forinopen#

small_list = [line2.strip() for line2 in open (SMALL)]

for i in range(len(big_list)):      #rangelen#

    if big_list[i] in small_list:
        m1 = big_list[i] , "  ok contenu ds les 2 listes"
        m1b = ''.join(m1)
        print m1b
        fw.write(m1b + "\n")

    else:
        m2 = big_list[i] , "  NO"
        m2b = ''.join(m2)
        print m2b
        fw.write(m2b + "\n")

fw.close()

# resultat :
# abandon   NO
# abduct   NO
# abide   ok contenu ds les 2 listes
# absorb   NO
# abstain   NO
# abstract   ok contenu ds les 2 listes
# abuse   NO
# accent   NO
# accentuate   ok contenu ds les 2 listes
# accept   NO

#fin#
#%_1054

haut

VLOOKUP : verifier si elements d'une grande liste sont dans petite liste RANGE LEN #part1055


#!/usr/bin/python

#@_1055y_vlookup_elements_grande_liste_contenus_dans_petite_liste!!forinopen_range_len_join
#deb#

BIG="z-big.txt"
SMALL="z-small.txt"
OUT="z-out2.txt"

# BIG :
# abandon
# abduct
# abide
# absorb
# abstain
# abstract
# abuse
# accent
# accentuate
# accept

# SMALL :
# abide
# abstract
# accentuate
# awake

fw = open (OUT , 'w')

small_list = [line3.strip() for line3 in open (SMALL)]       #forinopen#

big_list = [line4.strip() for line4 in open (BIG)]

for j in range(len(small_list)):      #rangelen#

    if small_list[j] in big_list:
        m1 = small_list[j] , "  ok"
        m1b = ''.join(m1)
        print m1b
        fw.write(m1b + "\n")

    else:
        m2 = small_list[j] , "    -> manque ds la grosse liste"
        m2b = ''.join(m2)
        print m2b
        fw.write(m2b + "\n")
fw.close()

# resultat :
# abide   ok
# abstract   ok
# accentuate   ok
# awake     -> manque ds la grosse liste

#fin#
#%_1055

haut

VLOOKUP : verifier si elements d'une petite liste sont dans grande liste #part1056


#!/usr/bin/python

#@_1056y_vlookup_patterns_petite_liste_contenus_dans_grande_liste!!read_split_join
#deb#

BIG="z-big.txt"
SMALL="z-small.txt"
OUT="z-out3.txt"

# BIG :
# abandon
# abduct
# abide
# absorb
# abstain
# abstract
# abuse
# accent
# accentuate
# accept

# SMALL :
# abide
# abstract
# accentuate
# awake

#########

fw = open(OUT , 'w')

# small_list = open(SMALL).read().split()        #openreadsplit#
small_list = open(SMALL).read().split("\n")        #openreadsplit#

# big_list = open(BIG).read().split()
big_list = open(BIG).read().split("\n")

for road in small_list:

    if road in big_list:
        m1 = road , " : ok in SMALL and BIG"
        m1b = ''.join(m1)
        print m1b
        fw.write(m1b + "\n")

    if road not in big_list:
        m2 = road + " : ok in SMALL but not in BIG"
        m2b = ''.join(m2)
        print m2b
        fw.write(m2b + "\n")
fw.close()

# resultat :
# abide  : ok in SMALL and BIG
# abstract  : ok in SMALL and BIG
# accentuate  : ok in SMALL and BIG
# awake  : ok in SMALL but not in BIG

#fin#
#%_1056

haut

VLOOKUP : verifier si elements d'une grosse liste sont dans petite liste #part1057


#!/usr/bin/python

#@_1057y_vlookup_elements_grosse_liste_contenus_dans_petite_liste!!read_split_join
#deb#

BIG="z-big.txt"
SMALL="z-small.txt"
OUT="z-out4.txt"

# BUT :
# check if patterns from a big file are present in a small list   ('ok' or '-')
# or how to know if patterns exist in a list

# BIG :
# abandon
# abduct
# abide
# absorb
# abstain
# abstract
# abuse
# accent
# accentuate
# accept

# SMALL :
# abide
# abstract
# accentuate
# awake

#######

fw = open (OUT , 'w')

big_list = open (BIG).read().split()       #openreadsplit#

road = open (SMALL).read().split()

for road in big_list:

    if road in small_list:
        m1 = road , " : ok"    # if road is in small_list : print 'ok'
        m1b = ' '.join(m1)
        print m1b
        fw.write(m1b + "\n")

    if road not in small_list:
        m2 = road , " : -"    # if road is not in small_list : print '-'
        m2b = ' '.join(m2)
        print m2b
        fw.write(m2b + "\n")
fw.close()

# resultat :
# abandon  : -
# abduct  : -
# abide  : ok
# absorb  : -
# abstain  : -
# abstract  : ok
# abuse  : -
# accent  : -
# accentuate  : ok
# accept  : -

#fin#
#%_1057

haut

Substituer (remplacer) des patterns selon un fichier pattern-correspondance DIC #part1058


#!/usr/bin/python

#@_1058y_dic_fichier1_col1_remplace_col2_ds_fichier2!!split_dic_trimmed_key_value_def_read_iteritems_replace_sub
#deb#

import re
import time
TIME=1

# BUT : remplacer de LI5 des valeurs de la colonne 1 par celles de la colonne 2 dans un fichier

IN="in.txt"
LI5="li5.txt"
LI6="li6.txt"
RESULTLI="1-RESULT.txt"

# LI5 :     #  col1 = ancienne valeur  ;  col2 = nouvelle valeur
# 1137 3001
# 1151 3002
# 1123 3003

# IN :
# data1 1137 line1
# example 2 data2 1151 line2
# line3 1123 data3
# line4 1289 data4


# habillage de LI5

f15 = open(LI5 , "r")        #open#
f16 = open(LI6 , "w")
for line15 in f15:
    road = line15.split()[0] + ":" + line15.split()[1]       #split#
#    print road
    f16.write(road + "\n")
f16.close()

print " ---> ok ajout de ':' entre 'ancien-nouveau' numero ds : " , LI6
time.sleep(TIME)

########################

# creation du dictionnaire : dic

dic = {}        #dic#
with open(LI6 , "r") as f17:        #withopen#
    for line17 in f17:
        trimmed_line17 = line17.strip()
        if trimmed_line17:
            (key, value) = trimmed_line17.split(':')
            dic[key]=value

# print dic        # {'1142': '3104', '1143': '3093', .............}

print " ---> ok creation du dictionnaire"
time.sleep(TIME)

#########################

# remplacement de  'ancien-nouveau'  numero ds  IN
f20 = open(RESULTLI , "w")
f19 = open(IN , 'r')
my_text = f19.read()       #openread#

def replace_all(text, dic):       #replace#
    for i, j in dic.iteritems():
        text = text.replace(i, j)
#        text = re.sub(r"'{}'".format(i), "'{}'".format(j), text)      #sub#
    return text

txt = replace_all(my_text , ::::::::::::::)
print txt
f20.write(txt)
f20.close()

print " ---> ok remplacement des anciens par tous les nouveaux numeros"
time.sleep(TIME)

# resultat :
# data1 3001 line1
# example 2 data2 3002 line2
# line3 3003 data3
# line4 1289 data4

#fin#
#%_1058

haut

Capturer un pattern avec FIND #part1059


#!/usr/bin/python

#@_1059y_capture_pattern_ds_1_fichier!!rawinput_True_False_find
#deb#

IN="in.txt"

# IN :
# to tolerate; put up with ; to abide
# to comply (with): (exemple :) to abide by the decision
# to remain faithful (to): (exemple :) to abide by your promise
# to dwell ; to abide
# to await in expectation ; to abide


w1 = raw_input("rechercher un mot : ")      #rawinput#


with open(IN) as f:      #withopen#
    found = False
    print ""
    for line in f:
        if w1 in line:
#        if line.find(" " + w1 + " ") != -1:      #find#
            print line.strip()
            found = True
    if not found:
        print ""
        print('The translation cannot be found!')
f.close()

# resultat :
# rechercher un mot : by

# to comply (with): (exemple :) to abide by the decision
# to remain faithful (to): (exemple :) to abide by your promise

#fin#
#%_1059

haut

Capturer plusieurs patterns dans un fichier SEARCH REGEX #part1060


#!/usr/bin/python

#@_1060y_capture_patterns_multi_regex!!search_group_regex
#deb#

import re

IN="in.txt"
OUT="out.txt"

# IN :

# @ arise
# intransitive verb
# 1.       [appear, happen]     survenir,   se presenter
#  a doubt arose in his mind   un doute est apparu dans son esprit
#  if the need arises   en cas de besoin
#  if the occasion arises   si l'occasion se presente
# 2.       [result]   resulter
#  a problem that arises from this decision   un probleme qui resulte OR decoule de cette decision
#  matters arising from the last meeting   des questions soulevees lors de la derniere reunion
# 3.       (literary) [person]     se lever
# [sun]     se lever, paraitre



regex = ["@ .*" , "^\[.*" , "^\d.*"]       #regex#

with open (IN, 'r') as road:      #withopen#
    fd = open (OUT, 'w')
    for ligne in road:
        for pattern in regex:
            m = re.search(pattern, ligne)      #search#
            if m:
                code = m.group(0)
                print code
                fd.write(code + "\n")

road.close()
fd.close()

# resultat :

# @ arise
# 1.       [appear, happen]     survenir,   se presenter
# 2.       [result]   resulter
# 3.       (literary) [person]     se lever
# [sun]     se lever, paraitre

#fin#
#%_1060

haut

Capturer un pattern en ignorant la casse (avec format) SEARCH #part1061


#!/usr/bin/python

#@_1061y_capture_pattern_ignorant_la_casse!!raw_input_readlines_search_format_groupe_join
#deb#

import re

IN="in.txt"
OUT="out.txt"

# IN :
# line1 data1
# line2 renommer
# line3 data2
# line4 Renommer
# line5 data3
# line6 RENOMMER


PATTERN = raw_input("taper 1 mot (lettres min ou max - ignore la casse) : ")       #rawinput#


f1 = open(IN , 'r').readlines()      #openreadlines#
f2 = open(OUT , 'w')

for s in f1:

    m = re.search(("(.*)" + '(?i)' + PATTERN + "(.*)") , s)      #search#

    if m:
        m1 = "{0}".format(m.group(1))
        m2 = "{0}".format(m.group(2))

        print m1 , PATTERN , m2
        w1 = m1 , PATTERN , m2
        w2 = ''.join(w1)
        f2.write(w2 + "\n")
f2.close()

# resultat :
# line2  renommer
# line4  renommer
# line6  renommer

#fin#
#%_1061

haut

Capturer un pattern en ignorant la casse (sans format) SEARCH #part1062


#!/usr/bin/python

#@_1062y_capture_pattern!!raw_input_readlines_search_groupe_join
#deb#

import re

IN="478-H-all.txt"
OUT1="out1.txt"
OUT2="out2.txt"

# ajouter un blanc devant toutes les lignes

fr = open(IN , 'r')
fw = open(OUT1 , 'w')

for g in fr:
#    print " " , g
    t = " " + g
    fw.write(t)
fw.close()

####################

# rechercher 1 mot oubien groupe de mots

PATTERN = raw_input("taper 1 mot (lettres min ou max - ignore la casse) : ")       #rawinput#
print ""

f1 = open(OUT1 , 'r').readlines()      #openreadlines#
f2 = open(OUT2 , 'w')

for s in f1:

    m = re.search("(.* )" + '(?i)' + PATTERN + "(.*)" , s)      #search#
    if m:
        code= m.group(0)
        print code
        f2.write(code + "\n")
f2.close()

#fin#
#%_1062

haut

Capturer des lignes (grep rapide) avec IF ANY - REGEX (mono fichier) #part1063


#!/usr/bin/python

#@_1063y_capture_lignes_avec_ifany_regex!!regex_split_ifany
#deb#

IN="1-exemple.txt"

# IN :
# don't forget to switch the lights off when you leave
# we'll carry on from where we left off
# we finished what was left of the cake


regex = ['when' , 'cake']         #regex#
w = ' '.join(regex)
print " ---> regex recherche : " , w

count=1
for line in open(IN):       #forinopen#
    line = line.split()       #split#
    m = ' '.join(line)

    if any(s in line for s in regex):       #ifany#
        print "ok regex est ds la ligne : " , count , " : " , m

    count+=1

# resultat :
#  ---> regex recherche :  when cake
# ok regex est ds la ligne :  1  :  don't forget to switch the lights off when you leave
# ok regex est ds la ligne :  3  :  we finished what was left of the cake

#fin#
#%_1063

haut

Capturer des lignes avec SEARCH - REGEX (mono fichier) #part1064


#!/usr/bin/python

#@_1064y_capture_lignes_avec_search!!search_group_regex
#deb#

import re

START="in.txt"
OUT="out.txt"

# START
# he doesn't see his drinking as a problem   il ne se considere pas comme un alcoolique
# All things considered ,  he's quite nice   Tout compte fait ,  il est aimable.
# ( believe )     croire ,    considerer
# 3.       ( bear in mind  points ,  facts )   prendre en consideration
# I consider it money well spent   je considere que c'est un bon investissement

regex = [".*consider.*"]       #regex#

with open (START, 'r') as road:       #withopen#
    fd = open (OUT, 'w')
    for ligne in road:
        m = re.search('.*(?<=   )', ligne)      #search#
        if m:
            code = m.group(0)
            for pattern in regex:
                n = re.search(pattern, code)
                if n:
                    code2 = n.group(0)
                    print code2
                    fd.write(code2 + "\n")
road.close()
fd.close()

# resultat
# All things considered ,  he's quite nice
# I consider it money well spent

#fin#
#%_1064

haut

Capturer plusieurs patterns sur une meme ligne SEARCH (colorama) #part1065


#!/usr/bin/python

#@_4291_capture_plusieurs_patterns_sur_1_meme_ligne!!rawinput_readlines_search_format
#deb#

from __future__ import print_function
import fixpath
from colorama import init, Fore, Back, Style

init()

import re

# doivent etre presents dans ce repertoire : 2 nouveaux repertoires :
#     colorama  et  colorama-0.3.6
# et 2 nouveaux fichiers (toujours dans ce meme repertoire):
#     fixpath.py et fixpath.pyc

############

START="1-in3.txt"

RESULTRED="1-out3.txt"

# START :
# line1 data1
# line2 data2
# line3 data1 data2

#######################

print (Style.BRIGHT + 'recherche dans : ' + START + '\n' , end='')

print ()

print (Style.NORMAL + '---> mot 1/2' , end='')
print ()

w1 = raw_input("taper 1 mot (give ..) : ")       # data1       #rawinput#

print (Style.NORMAL + '---> mot 2/2' , end='')
print ()

w2 = raw_input("taper 1 mot (esp - up - esp ..) - return si pas de mot : ")     # data2

print ()

#################

fs = open(START, 'r')
town = open (RESULTRED , "w")

t = fs.readlines()        #openreadlines#

for s in t:
    m = re.search(("(.*)" + w1 + "(.*)" + w2 + "(.*)") , s)       #search#

    if m:

        m1 = "{0}".format(m.group(1))
        m2 = "{0}".format(m.group(2))
        m3 = "{0}".format(m.group(3))

        print (Style.NORMAL + m1
            + Back.BLUE + Style.BRIGHT + w1
            + Style.NORMAL
            + Back.RESET + m2
            + Back.GREEN + Style.BRIGHT + w2
            + Style.NORMAL
            + Back.RESET + m3
            , end='')
        print ()

        town.write("{0}".format(s))

fs.close()
town.close()

###############################

# nb lignes de RESULTRED

linecount = len(open(RESULTRED, 'r').readlines())         #len#
print (Style.NORMAL + '####################' , end='')
print ()
print (Style.NORMAL + 'nb de resultats trouves : '
    + Style.BRIGHT , linecount
    , end='')
print ()

# resultat :
# ---> mot 1/2
# taper 1 mot (give ..) : data1
# ---> mot 2/2
# taper 1 mot (esp - up - esp ..) - return si pas de mot : data2

# line3 data1 data2
# ####################
# nb de resultats trouves :  1

#fin#
#%_1065

haut

Capturer des patterns avec SEARCH #part1066


#!/usr/bin/python

#@_1066y_capture_pattern_search!!search_replace
#deb#

import re

INTER4="inter4.txt"
INTER5="inter5.txt"

# INTER4 :
# Line 1
# @@ data
# %@
# Line 4
# End

with open (INTER4 , 'r') as fwhab4:      #withopen#
    fwhab5 = open (INTER5 , 'w')
    for line4 in fwhab4:
        if re.search("@@" , line4):      #search#
            w6 = line4.replace("@@" , "NEW1")       #replace#
            print line4.replace("@@" , "NEW1").strip()
            fwhab5.write(w6)
        elif re.search("%@" , line4):
            w7 = line4.replace("%@" , "NEW2")
            print line4.replace("%@" , "NEW2").strip()
            fwhab5.write(w7)

        else:
            print line4.strip()
            fwhab5.write(line4)

fwhab4.close()
fwhab5.close()

# resultat :
# Line 1
# NEW1 data
# NEW2
# Line 4
# End

#fin#
#%_1066

haut

Capturer des patterns avec FNMATCH (resultat en liste) #part1067


#!/usr/bin/python

#@_1067y_capture_grep_pattern_fnmatch_resultat_en_liste!!forinif_fnmatch_grep
#deb#

from fnmatch import fnmatchcase

addresses = [
    '5412 N CLARK ST',
    '1060 W ADDISON ST',
    '1039 W GRANVILLE AVE',
    '2122 N CLARK ST',
    '4802 N BROADWAY',
]                           # attention au crochet

a = [addr for addr in addresses if fnmatchcase(addr, '* ST')]      #match#

print a

# resultat
# ['5412 N CLARK ST', '1060 W ADDISON ST', '2122 N CLARK ST']

########

b = [addr for addr in addresses if fnmatchcase(addr, '54[0-9][0-9] *CLARK*')] # capturer des lignes (= grep)

print b

# resultat
# ['5412 N CLARK ST']

#fin#
#%_1067

haut

Capturer un pattern avec FINDITER REGEX #part1068


#!/usr/bin/python

#@_1068y_capture_pattern_avec_finditer!!finditer_group_regex
#deb#

import re

START="in.txt"
OUT="out.txt"

# @ arise
#  a doubt arose in his mind   un doute est apparu dans son esprit!!
#  if the need arises   en cas de besoin

with open (START , 'r') as fr:       #withopen#
    fd = open (OUT , 'w')
    for line in fr:

        regex = "\w+!!\W"       #regex#
        for i in re.finditer(regex, line):      #find#
            print i.group(0).strip()
            print line
            fd.write(line)
fr.close()
fd.close()

# resultat :
# esprit!!
#  a doubt arose in his mind   un doute est apparu dans son esprit!!

#fin#
#%_1068

haut

Capturer plusieurs series de patters differents avec SEARCH #part1069


#!/usr/bin/python

#@_1069y_capture_multi_patterns_search!!readlines_search_format_group_grep
#deb#

import re

IN="1-search.txt"

RESULTRED1="2-result-search.txt"

w1 = "@@%@@ pattern1"
w2 = "pattern1"
w3 = "open"
w4 = "fontcolor"

# IN
# line1 @@%@@ pattern1 @@%@@ fontcolor data1
# line2 @@%@@ pattern1 @@%@@ data2
# line3 @@%@@ pattern1 @@%@@ pattern1 data3
# line4 @@%@@ pattern1 @@%@@ data4 open file
# line5 @@%@@ pattern1 @@%@@ data5

########################


town = open (RESULTRED1 , "w")
fs = open(IN , 'r').readlines()     #openreadlines#

for s in fs:
    ma = re.search(("(.*)" + w1 + "(.*)" + w2 + "(.*)") , s)      #search#
    mb = re.search(("(.*)" + w1 + "(.*)" + w3 + "(.*)") , s)
    mc = re.search(("(.*)" + w1 + "(.*)" + w4 + "(.*)") , s)

    if ma:

        m1 = "{0}".format(ma.group(1))
        m2 = "{0}".format(ma.group(2))
        m3 = "{0}".format(ma.group(3))

        print m1 , w1 , m2 , w2 , m3
        h1 = m1 + w1 + m2 + w2 + m3
        town.write(h1 + "\n")

    if mb:

        m4 = "{0}".format(mb.group(1))
        m5 = "{0}".format(mb.group(2))
        m6 = "{0}".format(mb.group(3))

        print m4 , w1 , m5 , w3 , m6
        h2 = m4 + w1 + m5 + w3 + m6
        town.write(h2 + "\n")

    if mc:

        m7 = "{0}".format(mc.group(1))
        m8 = "{0}".format(mc.group(2))
        m9 = "{0}".format(mc.group(3))

        print m7 , w1 , m8 , w4 , m9
        h3 = m7 + w1 + m8 + w4 + m9
        town.write(h3 + "\n")

fs.close()
town.close()

# resultat :
# line1 @@%@@ pattern1 @@%@@ fontcolor data1
# line3 @@%@@ pattern1 @@%@@ pattern1 data3
# line4 @@%@@ pattern1 @@%@@ data4 open file

#fin#
#%_1069

haut

Capturer des lignes avec choix du pattern et de son occurrence avec COUNT #part1070


#!/usr/bin/python

#@_1070y_count_occurrence_choix_pattern_et_occurrence!!rawinput_count
#deb#

# printer 1 ligne avec choix du pattern et de son occurrence

wpattern = raw_input("entrer un pattern : ")      #rawinput#
woccur = raw_input("entrer une occurrence : ")

# in.txt :
# line1 data1
# line2 @ span @ data2
# line3 @ hello data3

print wpattern     #   @
print woccur     #   2
print ""

woccur2 = int(woccur)
print woccur2 , type(woccur2)
print ""

with open("in.txt" , 'r') as f:       #withopen#
    for line in f:
        if line.count(wpattern) == woccur2:
            print line
f.close()

# resultat :
# entrer un pattern : @
# entrer une occurrence : 2
# @
# 2

# 2 

# line2 @ span @ data2

#fin#
#%_1070

haut

Capturer un pattern avec STARTSWITH #part1071


#!/usr/bin/python

#@_1071y_capture_pattern_avec_starswith!!readlines_startswith
#deb#

# RESULT12 :
# newy-302-back- | transitive verb
# @@) 1. ( backwards - bicycle , car
# ) reculer
# @@) ( - horse
# ) faire reculer

fd12 = open (RESULT12, 'r').readlines()        #openreadlines#
fd13 = open (RESULT13 , "w")

for line12 in fd12:
    if line12.startswith(")"):    #startswith#
        print line12.strip()
        fd13.write(line12)

fd13.close()

# resultat :
# ) reculer
# ) faire reculer

#fin#
#%_1071

haut

Capturer plusieurs patterns multi regex avec STARTSWITH [var crochet] - exemple 1 #part1072


#!/usr/bin/python

#@_1072y_capture_patterns_multi_regex!!startswith_join_regex_replace
#deb#

regex1=("!" , "-")       #regex#
regex2=("@" , "%")

fz1 = open("zout.txt" , 'w')

var=[        #var=[#
# line.replace(".toto" , "\ntoto").replace(".finditer" , "\nfinditer") \       #replace#
line
for line in open("zin.txt")        #forinopen#

if line.startswith(regex1) or line.startswith(regex2)    #startswith#
] #finduvar#
print ''.join(var)
w1b = ''.join(var)
fz1.write(w1b)
fz1.close()

# resultat : zout.txt :
# ----- data1
# @ data2
# !! data3
# % text

#fin#
#%_1072

haut

Capturer plusieurs patterns multi regex avec STARTSWITH - exemple 2 #part1073


#!/usr/bin/python

#@_1073y_capture_patterns_multi_regex!!readlines_startswith_regex
#deb#

# fichier : zin.txt :
# line1
# ----- data1
# python
# @ data2
# script
# !! data3
# end of test
# % text


# regex s'emboite dans startswith()
regex=("!" , "--" , "@" , "%")       #regex#

f = open ("zin.txt" , 'r').readlines()       #openreadlines#
fw = open ("zout.txt" , 'w')

for line in f:
    if line.startswith(regex):    #startswith#
        print line.strip()
        fw.write(line)
fw.close()

# resultat : zout.txt :
# ----- data1
# @ data2
# !! data3
# % text

#fin#
#%_1073

haut

Capturer un pattern avec endswith #part1074


#!/usr/bin/python

#@_1074y_capture_pattern_avec_endswith!!split_endswith_tableau
#deb#

START="in.txt"

# START
# she plans to attempt @@ the record again in June
# his aim is @@ to get rich quickly

with open(START, 'r') as testList:       #withopen#
    for line in testList:
        for word in line.split():     # pour chaque mot des lignes       #split#
            if word.endswith('t'):       #endswith#
#           if word[-1:] == "t":   # idem
                print word
testList.close()

# resultat :
# attempt
# get

#fin#
#%_1074

haut

Lire une ligne si elle contient une majuscule (avec capitalize) #part1283


#!/usr/bin/python

#@_1283y_capture_majuscule!!splitlines_capitalize
#deb#

# BUT = Lire des lignes contenant une majuscule

IN="7-capitalize-in.txt"
OUT="7-capitalize-out.txt"

# IN :
# in @@@ il a 20 ans
# in @@@ Elle va bien
# in @@@ il va Avoir 40 ans
# in @@@ ELLE VA BIEN

f2 = open(OUT , "w")
mylist = open(IN).read().splitlines() 
#print mylist

for i in mylist:
    if "@@@" in i:
        m1 = i.split("@@@")[1]
        if not m1.capitalize() in i:          #capitalize#
            print i
            f2.write(i + "\n")     
f2.close()

# resultat :
# in @@@ Elle va bien
# in @@@ il va Avoir 40 ans
# in @@@ ELLE VA BIEN

#fin#
#%_1283

haut

Changer la premiere lettre en majuscule (avec upper) #part1277


#!/usr/bin/python

#@_1277y_premiere_lettre_majuscule!!split_upper_join_read_splitlines
#deb#

IN="6-in.txt"

OUT1="6-out1.txt"
OUT2="6-out2.txt"

# IN :
# she plans to attempt the record again in June
# his aim is to get rich quickly
# Depending on the depth of the water
# All of these effects are well known

########################

# traitement des majuscules

with open(IN , 'r') as f42:       #withopen#
    f43 = open (OUT1 , 'w')
    for line42 in f42:

        m42 = line42.split(" ")[0]        #split
#        m43 = line42.split(" ")[1:]    
        m43 = line42.split(" ")[1]    

#        print m23.strip() + " " + m22.strip()    
#        m22b = m22 + "\n"
#        m43b = m43.strip()
#        m43b = str(m43)

# premiere lettre en majuscule
#        print m42[0].upper()         #upper

# premier mot sans la premiere lettre
#       print m42[1:]

# premier mot avec premiere lettre en majuscule
        w6 = m42[0].upper() + m42[1:]            #upper     
#        print w6

# a partir du deuxieme mot jusqu a la fin de la ligne
        w7 = line42.split(" ")[1:]
        w8 = ' '.join(w7)            #join
#        print w8

# concatener premier mot et le reste de la ligne        
#        print w6 , w8.strip()

        f43.write(w6 + " " + w8)
f43.close()

print "ok toutes les premieres lettres sont en majuscule, resultat ds : " , OUT1
print "-----------------------"

############################

# trier

fd4 = open (OUT1 , 'r')
fw5 = open (OUT2 , 'w')

t4 = fd4.read().splitlines()

d4 = sorted(t4)     # (t , reverse=True)     # tri en ordre inverse        #sort#

#print '\n'.join(d4)
fw5.write('\n'.join(d4))
fw5.close()

print "ok fichier trie , resultat ds : " , OUT2
print "-----------------------"

###########

# resultat :
# All of these effects are well known
# Depending on the depth of the water
# His aim is to get rich quickly
# She plans to attempt the record again in June

#fin#
#%_1277

haut

Lire premiere et derniere ligne #part1075


#!/usr/bin/python

#@_1075y_lire_premiere_et_derniere_ligne!!readlines_join_tableau
#deb#

# in.txt :
# line1
# line2
# line3
# line4

# exemple 1  (utilisation d'1 tableau) :
f1 = open("in.txt",'r')
fw1 = open ("out1.txt" , "w")
lines = f1.readlines()      #openreadlines#
if lines:
    first_line = lines[:1]
    last_line = lines[-1]

    print first_line
    print last_line
    new1 = ''.join(first_line)
    new2 = ''.join(last_line)
    fw1.write(new1)
    fw1.write(new2)
fw1.close()

# exemple 2 :  (utilisation d'1 tableau) :
with open("in.txt") as f2:        #withopen#
    fw2 = open ("out2.txt" , "w")
    lines = f2.readlines()       #withopenreadlines#
    first_row = lines[0].strip()
    last_row = lines[-1]

    print first_row
    print last_row
    fw2.write(first_row + "\n")
    fw2.write(last_row)
fw2.close()

# resultat :
# line1
# line4

#fin#
#%_1075

haut

Lire 2 lignes apres chaque apparition d'un marqueur (grep -A) #part1076


#!/usr/bin/python

#@_1076y_lire_2_lignes_apres_un_marqueur!!readlines_range_len_tableau_grep-A
#deb#

# BUT : lire 2 lignes apres chaque apparition du marqueur : @

PATTERN="@"

IN="1-in.txt"
OUT1="1-out.txt"

# IN :
# @ verbe1
# Line1a
# Line2a
# Line3a
# Line4a
# Line5a
# @ verbe2
# Line1b
# Line2b
# Line3b
# Line4b

# utilisation d'1 tableau - idem que :  grep -A

fw = open(OUT1 , 'w')
f1 = open(IN).readlines()       #openreadlines#
for i in range(len(f1)):      #rangelen#
    if PATTERN in f1[i]:
        print f1[i].strip()
        print f1[i+1].strip()
        print f1[i+2].strip()

        fw.write(f1[i])
        fw.write(f1[i+1])
        fw.write(f1[i+2])

fw.close()

# resultat :
# @ verbe1
# Line1a
# Line2a
# @ verbe2
# Line1b
# Line2b

#fin#
#%_1076

haut

Lire une ligne (moins bon) [var crochet] #part1077


#!/usr/bin/python

#@_1077y_lire_1_ligne_d_1_fichier!!sum_readlines_len_forinopen
#deb#

IN="in2.txt"

# IN :
# data1 line1
# data2 line2
# data3 line3


num_lines1 = sum(1 for line in open (IN))       #sumforinopen#
print "nombre de lignes du fichier : " , num_lines1    # 3

fd = open(IN).readlines()       #openreadlines#
num_lines2 = int(len(fd))       #len#
print "nombre de lignes du fichier : " , num_lines2    # 3

# lire 1 ligne

var=[        #var=[#
line.strip()
for line in open (IN)        #forinopen#
] #finduvar#
print var         # ['data1 line1', 'data2 line2', 'data3 line3']

print var[1]

# resultat :
# nombre de lignes du fichier :  3

# ['data1 line1', 'data2 line2', 'data3 line3']
# data2 line2

#fin#
#%_1077

haut

Lire la ligne 3 en partant de la fin #part1078


#!/usr/bin/python

#@_1078y_lire_la_ligne_3_en_partant_de_la_fin!!read_splitlines_tableau
#deb#

# in.txt :
# line 1 data1
# line 2 data2
# line 3 data3
# line 4 data4

# utilisation d'1 tableau

infile = open ("in.txt" , 'r')
fw = open ("out.txt" , "w")
my_list = infile.read().splitlines()      #openreadsplitlines#

print my_list[-3]
fw.write(my_list[-3])
fw.close()

# resultat :
# line 2 data2

#fin#
#%_1078

haut

Lire les N premieres lignes d'un fichier (exemple 1 court) #part1079


#!/usr/bin/python

#@_1079y_lire_N_premieres_lignes_exemple1!!range_next
#deb#

# in.txt :
# line1 / user
# line2 / 25
# line3 / data
# line4 / 100
# line5 / root

N=3
f1 = open("in.txt" , 'r')      #open#
f2 = open ("out.txt" , 'w')
for i in range(N):      #range#
    line = f1.next().strip()       #next#
    print line
    f2.write(line + "\n")
f1.close()
f2.close()

# resultat :
# line1 !! user
# line2 !! 25
# line3 !! data

#fin#
#%_1079

haut

Lire les N premieres lignes d'un fichier (exemple 2 long) #part1080


#!/usr/bin/python

#@_1080y_lire_N_premieres_lignes_exemple2!!next_range_replace
#deb#

# in.txt :
# line1 / user
# line2 / 25
# line3 / data
# line4 / 100
# line5 / root

N=3
with open ("in.txt" , 'r') as f1:       #withopen#
    f2 = open ("out.txt" , 'w')
    head = [next(f1) for x in xrange(N)]      #next#      #range#
    for i in head:
        i = i.replace("/" , "!!")       #replace#
        print i.strip()
        f2.write(i)

f1.close()
f2.close()

# resultat :
# line1 !! user
# line2 !! 25
# line3 !! data

#fin#
#%_1080

haut

Lire les N premieres lignes de plusieurs fichiers #part1081


#!/usr/bin/python

#@_1081y_capture_N_premieres_lignes_inplusieursfichiers!!forinlistdir_range_next
#deb#

import os

OUT1="4-out1.txt"

PATH="/Python27/tests-2018-06"

N=2

# 4-in1.txt
# line1 data1
# line2 data1
# line3 data1
# line4 data1

# 4-in2.txt
# line1 data2
# line2 data2
# line3 data2
# line4 data2


fw = open(OUT1 , 'w')
newlist = [i for i in os.listdir(PATH) if i[0:4] == "4-in"]       #forinlistdir#

for file in newlist:
    f1 = open(file , 'r')        #open#

    for i in range(N):                 #range#
        line1 = f1.next().strip()         #next#
        print line1
        fw.write(line1 + "\n")
fw.close()

# resultat :
# line1 data1
# line2 data1
# line1 data2
# line2 data2

#fin#
#%_1081

haut

Lire un fichier en sens inverse : de la fin au debut #part1082


#!/usr/bin/python

#@_1082y_lire_fichier_de_la_fin_au_debut!!reversed_readlines
#deb#

IN="1-in.txt"
OUT="1-reverse.txt"

# IN
# ligne 1
#   ligne 2
#     ligne 3
#       ligne 4


fw1 = open(OUT , 'w')
for line in reversed(open(IN).readlines()):      #openreadlines#
    print line.rstrip()
    w = line.rstrip()
    fw1.write(w + "\n")
fw1.close()

# resultat :
#       ligne 4
#     ligne 3
#   ligne 2
# ligne 1

#fin#
#%_1082

haut

Lire un fichier avec WITH OPEN #part1083


#!/usr/bin/python

#@_1083y_lire_fichier_avec_with_open!!startswith_sub_replace_split
#deb#

import os
import re

# DEPART :
# verb abandon @@  verb %%
# verb abandon @@   (transitive)
# verb abandon @@  to forsake completely; desert; leave behind
# % abstain @@  to choose to refrain -data to remove-
# % abstain @@  he abstained from alcohol 
# % abstain @@  to refrain from voting -to delete from text-


f16 = open("out.txt" , 'w')
with open("in.txt" , "r") as f15:       #withopen#
    for line15 in f15:

        if line15.startswith("%"):     #startswith#
            line15 = re.sub('data to remove' , '' , line15)      #sub#
#            line15 = re.sub('from.*?ing' , '' , line15)
            line15 = line15.replace("" , "") \       #replace#
            .replace("to delete from text" , "")

            w1 = line15.split(" ")[1].strip()       #split#
            w2 = line15.split("@@")[1].strip()
            new = w1 + " @@ " + w2
            print new
            f16.write(new + "\n")

        else:
            print line15.strip()
            f16.write(line15)

f15.close()
f16.close()

# resultat :
# verb abandon @@  verb %%
# verb abandon @@   (transitive)
# verb abandon @@  to forsake completely; desert; leave behind
# abstain @@ to choose to refrain --
# abstain @@ he abstained from alcohol
# abstain @@ to refrain from voting --

#fin#
#%_1083

haut

Ecrire dans un fichier un nombre avec str #part1276


#!/usr/bin/python

#@_1276y_ecrire_dans_fichier!!str
#deb#

IN = "test1.txt"
OUT = "test1-out.txt"

# IN 
# line1 data1
# line2 data2
# line3 data3

# compter les lignes d'un fichier
num_lines1 = sum(1 for line in open (IN))     #sumforinopen#    # count
print "nombre de lignes de OLD : " , num_lines1    # 3

##############

# ecrire  le nombre de lignes dans un fichier    en  "append" 

f1 = open (OUT , 'a')        # on peut reecrire dans le meme fichier  IN

str1 = str(num_lines1)
w1 = "nombre de lignes : " + str1
f1.write(w1 + "\n----------------------------------------\n")
f1.close()

# resultat : 
# nombre de lignes : 3
# ----------------------------------------

#fin#
#%_1276

haut

Selectionner des lignes contenant 3 espaces avec SEARCH #part1084


#!/usr/bin/python

#@_1084y_capture_lignes_contenant_3_espaces!!search
#deb#

import re

START="in.txt"
OUT="out.txt"

# START
# @ arise
#  a doubt arose in his mind   un doute est apparu dans son esprit
#  if the need arises   en cas de besoin

with open (START , 'r') as fr:      #withopen#
    fd = open (OUT , 'w')
    for line in fr:
        m = re.search('\w+(   )\w+', line)      #search#
        if m:
            print line.strip()
            fd.write(line)

fr.close()
fd.close()

# resultat
#  a doubt arose in his mind   un doute est apparu dans son esprit
#  if the need arises   en cas de besoin

#fin#
#%_1084

haut

Selectionner des portions de lignes delimitees par un separateur avec SEARCH - regex #part1085


#!/usr/bin/python

#@_1085y_capture_portions_de_lignes_avec_separateur_exemple 1!!search_group_regex
#deb#

import re

START="in.txt"
OUT="out.txt"

# START
# he doesn't see his drinking as a problem   il ne se considere pas comme un alcoolique
# All things considered ,  he's quite nice   Tout compte fait ,  il est aimable.
# ( believe )     croire ,    considerer
# 3.       ( bear in mind  points ,  facts )   prendre en consideration
# I consider it money well spent   je considere que c'est un bon investissement

regex = [".*consider.*"]       #regex#

with open (START, 'r') as road:       #withopen#
    fd = open (OUT, 'w')
    for ligne in road:
        m = re.search('.*(?<=   )', ligne)      #search#
        if m:
            code = m.group(0)
            for pattern in regex:
                n = re.search(pattern, code)
                if n:
                    code2 = n.group(0)
                    print code2
                    fd.write(code2 + "\n")
road.close()
fd.close()

# resultat
# All things considered ,  he's quite nice
# I consider it money well spent

#fin#
#%_1085

haut

Selectionner des portions de lignes delimitees par un separateur avec SPLIT #part1086


#!/usr/bin/python

#@_1086y_capture_portions_de_lignes_avec_separateur_exemple2!!split
#deb#

START="in.txt"
OUTUK="out.txt"

# START
# his excuses are wearing a bit thin   ses excuses ne prennent plus
# his jokes are wearing a bit thin   ses plaisanteries ne sont plus droles
# as the year wore to its close   comme l'annee tirait a sa fin

with open (START, 'r') as road1:       #withopen#
    fd1 = open (OUTUK, 'w')
    for ligne1 in road1:
        m = ligne1.split('   ')[0]       #split#
        print m.strip()
        fd1.write(m + "\n")

fd1.close()
road1.close()

# resultat
# his excuses are wearing a bit thin
# his jokes are wearing a bit thin
# as the year wore to its close

#fin#
#%_1086

haut

Marquer nom du fichier devant les lignes (in-out multi fichiers) #part1087


#!/usr/bin/python

#@_1087y_marquer_nom_fichier_devant_lignes_inoutplusieursfichiers!!forinlistdirif_remove_split_chdir
#deb#

import os
import glob
import time

END1="noun"
END2="adj"

PATH="/Python27/tests-2018-02"

TIME=1

# v-abide.txt :
# data1
# verb
# data2
# noun
# data3

# v-accept.txt :
# data1b
# verb
# data2b
# adj
# data3b


# deleter anciens verbes
for currentFile in glob.glob('*'):
    e5 = "w-";
    if currentFile.startswith(e5):
        os.remove(currentFile)

print " ---> ok anciens fichiers deletes"
time.sleep(TIME)

###################


newlist = [i for i in os.listdir(PATH) if i[0:3] == "v-a"]         #forinlistdir#
#print newlist

for id18 in newlist:
    id18=id18.strip()

    id18b=id18.split("-")[1].split(".")[0]       #split#
    print ""
    for line18 in open(id18 , 'r'):       #open#

        if not END1 in line18 and not END2 in line18:

            f19 = open("w-" + id18 , "a")     # "a" pour accumulation
            print id18b , " " , line18.strip()
            f19.write(id18b + " " + line18)

f19.close()

# resultat :
# w-v-abide.txt :
# abide data1
# abide verb
# abide data2
# abide data3

# w-v-accept.txt :
# accept data1b
# accept verb
# accept data2b
# accept data3b

#fin#
#%_1087

haut

Marquer nom du fichier devant les lignes (in-out multi fichiers) #part1088


#!/usr/bin/python

#@_1088y_marquer_nom_fichier_devant_lignes_inoutplusieursfichiers!!forinlistdir_split
#deb#

import os
import re

PATH="/nfs/u/tests-2018-02"

#####################

# v-abide.txt :
# to accept or submit to; suffer
# to comply (with)
# to withstand or sustain; endure

# v-account.txt :
# money deposited at a bank
# they transferred their publicity account to a new agent
# to consider or reckon


newlist = [i for i in os.listdir(PATH) if i[0:2] == "v-"]      #forinlistdir#
#print newlist

for file in newlist:
    file = file.strip()
    fileb = file.split("-")[1].split(".")[0]       #split#
    f2 = open("u-" + fileb + ".txt" , "w")

    f1 = open(file , 'r')     #open#

    for line1 in f1:
        line1 = line1.strip()

#        if not "pattern" in line1:

        print fileb , " @@ " , line1
        road1 = fileb , " @@ " , line1
        road2 = ''.join(road1)
        f2.write(road2 + "\n")
f2.close()

# resultat :
# u-abide.txt :
# abide  @@  to accept or submit to; suffer
# abide  @@  to comply (with)
# abide  @@  to withstand or sustain; endure

# u-account.txt :
# account  @@  money deposited at a bank
# account  @@  they transferred their publicity account to a new agent
# account  @@  to consider or reckon

#fin#
#%_1088

haut

Marquer nom du fichier devant les lignes (in-out multi fichiers) #part1089


#!/usr/bin/python

#@_1089y_marquer_nom_du_fichier_devant_lignes_inoutplusieursfichiers!!forinlistdir_readlines_split
#deb#

import os
import re

PATH="/nfs/u/tests-2018-02"

# c-abandon.txt :
# pos @@UK@@ verb %%
# def @@UK@@ to forsake completely; desert; leave behind
# def @@UK@@ to surrender (oneself) to emotion without restraint

# c-allow.txt :
# pos @@UK@@ verb %%
# def @@UK@@ the largest quantity or amount allowed

newlist = [i for i in os.listdir(PATH) if i[0:2] == "c-"]      #forinlistdir#
#print newlist

for id25 in newlist:
    id25=id25.strip()
    id25b=id25.split("-")[1].split(".")[0]       #split#

    f26 = open("d-" + id25b + ".txt" , "w")
    f27 = open(id25).readlines()       #openreadlines#

    for line27 in f27:
        line27 = line27.strip()

        if "pos" in line27:
            inter1 = line27.split("@@UK@@ ")[1].split("%%")[0]
            inter2 = line27.split("@@UK@@ ")[1]

            print inter1 , id25b , " @@ " , inter2
            f26.write(inter1 + id25b + " @@ " + inter2 + "\n")
        if not "pos" in line27:
            inter2 = line27.split("@@UK@@ ")[1]

            print inter1 , id25b , " @@ " , inter2
            f26.write(inter1 + id25b + " @@ " + inter2 + "\n")
f26.close()

# resultat :

# d-abandon.txt :
# verb abandon @@  verb %%
# verb abandon @@  to forsake completely; desert; leave behind
# verb abandon @@  to surrender (oneself) to emotion without restraint

# d-allow.txt :
# verb allow @@ verb %%
# verb allow @@ the largest quantity or amount allowed

#fin#
#%_1089

haut

Marquer un pattern devant les lignes variant selon les lignes (in-out mono fichier) #part1090


#!/usr/bin/python

#@_1090y_marquer_pattern_devant_chaque_ligne!!split_endswith
#deb#

# BUT : coller devant toutes les lignes un pattern dont sa fin est "ing"

IN="in.txt"

# IN :
# Working in front of the blast furnace ,  I was exposed to scorching heat every day. - chaleur accablante - fournaise
# The worker objected to having to work late since he wouldn't get paid for the extra hours.

f = open (IN ,'r')         #open#
fw = open("out.txt" , 'w')

for lines in f:
    word=lines.split()         #split#        # splitlines()   # pour compter lignes
    for g in word:

        if g.endswith("ing"):       #endswith#
            print g , " %%% " , lines.strip()
            road = g + " %%% " + lines
            fw.write(road)
fw.close()

# resultat :
# Working  %%%  Working in front of the blast furnace ,  I was exposed to scorching heat every day. - chaleur accablante - fournaise
# scorching  %%%  Working in front of the blast furnace ,  I was exposed to scorching heat every day. - chaleur accablante - fournaise
# having  %%%  The worker objected to having to work late since he wouldn't get paid for the extra hours.

#fin#
#%_1090

haut

Marquer un titre et le repeter s'il ne change pas avec RANGE LEN (in-out mono fichier) #part1091


#!/usr/bin/python

#@_1091y_marquer_titre_devant_ligne_et_le_repeter_range_len!!readlines_range_len_split_break_tableau
#deb#

# ajouter 2 lignes a la fin du fichier:

open ("in2.txt" , "w").write ( open ("in.txt").read() + "end\nend\n")

# in2.txt :
# Receiver 1 : titre1
# data1
# data2
# Receiver 2 : titre2
# data3
# data4
# end
# end

# utilisation d'1 tableau

fd30 = open ("in2.txt" , 'r').readlines()     #openreadlines#
fd31 = open ("out.txt" , "w")

for i in range(len(fd30)):      #rangelen#

    if "Receiver" in fd30[i].split(":")[0]:       #split#
        w = fd30[i].strip()
        print w
        fd31.write(w + "\n")

    if not "Receiver" in fd30[i+1]:
        fd30[i+1] = fd30[i+1].strip()
0        print w , fd30[i+1]
        fd31.write(w + " " + fd30[i+1] + "\n")
    if "end" in fd30[i]:
        break        #break#
fd31.close()

# resultat :
# Receiver 1 : titre1
# Receiver 1 : titre1 data1
# Receiver 1 : titre1 data2
# Receiver 2 : titre2
# Receiver 2 : titre2 data3
# Receiver 2 : titre2 data4
# Receiver 2 : titre2 end
# Receiver 2 : titre2 end

#fin#
#%_1091

haut

Marquer un titre et le repeter s'il ne change pas (in-out mono fichier) #part1092


#!/usr/bin/python

#@_1092y_marquer_pattern_col1_et_repeter_devant_lignes!!readlines_startswith
#deb#

# START :
# newy-109-accomplish- | transitive verb
# 1) accomplir , executer
# 2) realiser
# 3) effectuer
# 4) venir a bout de , mener a bonne fin
# newy-57-accord- | transitive verb
# 1) accorder
# 2) reserver

fd14 = open (START, 'r').readlines()       #openreadlines#
fd15 = open (RESULT15 , "w")

for line14 in fd14:
    if line14.startswith("newy"):    #startswith#
        inter = line14
        inter = inter.strip()
        print "----------"
#        fd15.write("-----------------\n")

    if not line14.startswith("newy"):
        print inter , "|" , line14.strip()
        fd15.write(inter + " | " + line14)

fd15.close()

# resultat :
# newy-109-accomplish- | transitive verb | 1) accomplir , executer
# newy-109-accomplish- | transitive verb | 2) realiser
# newy-109-accomplish- | transitive verb | 3) effectuer
# newy-109-accomplish- | transitive verb | 4) venir a bout de , mener a bonne fin
# newy-57-accord- | transitive verb | 1) accorder
# newy-57-accord- | transitive verb | 2) reserver

#fin#
#%_1092

haut

Marquer un titre et le repeter s'il ne change pas + trait separ WHILE1 - FLAG (in-out mono fichier) #part1093


#!/usr/bin/python

#@_1093y_marquer_titre_et_le_repeter_sur_lignes_suivantes_trait_separation!!readline_while_break_startswith_join_flag_rstrip
#deb#

IN1="in1.txt"
IN2="in2.txt"
OUT="out.txt"

PATTERN="Receiver"

# START = in1.txt :
# Receiver 1 : titre1
# data1
# data2
# Receiver 2 : titre2
# data3
# data4


# ajouter 1 ligne vide en ligne 1

open (IN2 , "w").write ( " \n" + open (IN1).read())     #openread#

#######################

f1 = open (IN2 , 'r')
f2 = open (OUT , "w")
line = f1.readline()       #openreadline#

while 1:       #while#
    line = f1.readline()
    line = line.rstrip()

    if not line:
        break        #break#

    if line.startswith(PATTERN):     #startswith#
        w = line
        flag=0      #flag#

        flag=1
#        z = line                               # sans separation  - schema de la ligne a ecrire
        m = "--------------------\n" , line      # avec separation - schema de la ligne a ecrire
        z = ''.join(m)                           # avec separation

    if not line.startswith(PATTERN):
        y = w , line              # schema de la ligne a ecrire  (en tuple)
        z = ' '.join(y)

    if flag:
        print z
        f2.write(z + "\n")
f2.close()

# resultat :
# --------------------
# Receiver 1 : titre1
# Receiver 1 : titre1 data1
# Receiver 1 : titre1 data2
# --------------------
# Receiver 2 : titre2
# Receiver 2 : titre2 data3
# Receiver 2 : titre2 data4

#fin#
#%_1093

haut

Marquer un texte unique devant les lignes MAP LAMBDA #part1094


#!/usr/bin/python

#@_1094y_ajouter_texte_devant_toutes_lignes!!map_lambda_readlines
#deb#

import os
import re

# in.txt :
# line1/data1
# line2/span/data2
# line3/hello/data3


f1 = open("in.txt" , 'r')
lines = map(lambda l : '/Python27/user/' + l , f1.readlines())      #openreadlines#      #map#     #lambda#
for i in lines:
    print i.strip()
f1.close()

f1 = open("out.txt" , 'w')
map(lambda l : f1.write(l) , lines)
f1.close()

# resultat :
# /Python27/user/line1/data1
# /Python27/user/line2/span/data2
# /Python27/user/line3/hello/data3

#fin#
#%_1094

haut

Supprimer la ligne 1 dans un fichier #part1095


#!/usr/bin/python

#@_1095y_deleter_ligne_1_ds_1_fichier!!read_splitlines_tableau
#deb#

# in.txt :
# line1 data1
# line2 @ span @ data2
# line3 @ hello data3

fw = open ("out.txt" , "w")

data = open("in.txt" , 'r').read().splitlines()        #openreadsplitlines#
datanew = data[1:]            # ligne 1 supprimee (idem que "shift")

for i in datanew:
    print i
    fw.write(i + "\n")
fw.close()

# resultat :
# line2 @ span @ data2
# line3 @ hello data3

#fin#
#%_1095

haut

Supprimer la derniere ligne de plusieurs fichiers #part1096


#!/usr/bin/python

#@_1096y_deleter_derniere_ligne_de_inoutplusieursfichiers!!forinlistdir_slip_read_splitlines_join
#deb#

import os
import glob

#OUT="1-without-last-line-out-multi.txt"

PATH="/nfs/u/tests-2018-03"

TIME=1

# file1.txt :
# line1 datafile1
# line2 datafile1

# file2.txt
# line1 datafile2
# line2 datafile2

######

# utilisation d'1 tableau

newlist = [i for i in os.listdir(PATH) if i[0:4] == "file"]      #forinlistdir#
#print newlist

for file in newlist:
    file = file.strip()
    road = file.split(".")[0]       #split#

    fw = open ("y" + road + ".txt" , "w")
    f1 = open(file , 'r')

    line1 = f1.read().splitlines()      #openreadsplitlines#
    w = line1[:-1]         # retirer derniere ligne
    w2 = ''.join(w)
    print w2
    fw.write(w2 + "\n")
fw.close()

# resultat :
# yfile1.txt
# line1 datafile1

# yfile2.txt
# line1 datafile2

#fin#
#%_1096

haut

Lire et stopper a un pattern dans plusieurs fichiers BREAK #part1097


#!/usr/bin/python

#@_1097y_lire_et_stopper_a_1_pattern_inoutplusieursfichiers!!forinlistdirif_split_break
#deb#

import os

PATTERNSTOP="@@@"

PATH="/Python27/tests-2018-02"

# x-file1.txt
# Data1a
# Data2a
# @@@
# Data3a
# Data4a

# x-file2.txt
# Data1b
# Data2b
# @@@
# Data3b
# Data4b


newlist = [i for i in os.listdir(PATH) if i[0:6] == "x-file"]         #forinlistdir#
#print newlist

for id15 in newlist:
    id15=id15.strip()
    id15b=id15.split("-")[1].split(".")[0]       #split#

    f16 = open("w-" + id15b + ".txt" , "w")

    f17 = open(id15 , "r")       #open#

    for line17 in f17:
        line17 = line17.strip()

        if PATTERNSTOP in line17:
            break        #break#
        else:
#            print line17
            f16.write(line17 + "\n")
f16.close()

print " ---> ok creation des fichiers : w-"


# resultat :
# w-file1.txt
# Data1a
# Data2a

# w-file2.txt
# Data1b
# Data2b

#fin#
#%_1097

haut

Supprimer un blanc au debut ou fin de ligne - supprimer un retour chariot #part1098


#!/usr/bin/python

#@_1098y_deleter_blanc_au_debut_ou_fin_de_ligne_deleter_retour_chariot!!readlines_sorted_set_sub_sed_system
#deb#

# retirer le blanc devant toutes les lignes
# 1/3_ methode courte avec sed BASH : (OUT2 et OUT3 sont a definir)

os.system("sed 's/^ \{1\}//g' " + OUT2 + " > " + OUT3)

# 2/3_ autre methode

f1 = open(OUT1 , 'r')
f2 = open(OUT2 , 'w')
for line1 in f1.readlines():     #openreadlines#
    line1 = line1[:-2]        # oubien [:-1] si necessaire pour retirer le '\r'
    print line1
    f2.write(line1 + "\n")
f2.close()

# 3/3_ methode longue :

basket = open (CONCAT3).readlines()
f6 = open (CONCAT4 , 'w')
for f in sorted(set(basket)):       #set#        #sort#
#    if not f.startswith("---"):     #startswith#

    f = re.sub(r"(^\s)" , "" , f )   # blanc au debut de ligne     #sub#
#    f = re.sub(r"(\s$)" ,"" , f)    # blanc en fin de ligne
    f6.write(f)
f6.close()

###############

# retirer le \r (retour chariot d'un fichier)

os.system("sed 's/\r//g' " + OUT1 + " > " + OUT2)

#fin#
#%_1098

haut

Supprimer des lignes avec STARTSWITH #part1099


#!/usr/bin/python

#@_1099y_deleter_lignes_avec_startswith!!readlines_startswith
#deb#

# in.txt :
# realiser
# pattern1 accomplir , executer
# pattern2 venir a bout de , mener a bonne fin

f1 = open ("in.txt" , 'r')
lines = f1.readlines()       #openreadlines#
f1.close()

f1 = open ("out.txt" , 'w')        # on pourrait reecrire dans le meme fichier  : in.txt

for i in lines:
    if not i.startswith("pattern1") and not i.startswith("pattern2"):     #startswith#
        print i
        f1.write(i)
f1.close()

# resultat :
# realiser

#fin#
#%_1099

haut

Supprimer des groupes de mots avec SUB [var crochet] #part1100


#!/usr/bin/python

#@_1100y_deleter_groupes_de_mots_inplusieursfichiers!!forinlistdirif_forinopen_sub_sort
#deb#

import os
import re

PATHHOME="/Python27/tests-2017-10"

# uyz-1.txt
# Missfire SP 2233 , 4365
# SP Time error 5ms data1
# autofire SP 4512 , 5227
# disabled field shot 2325
# ------------- SP

# uyz-2.txt
# disabled data1 SP 4431
# autofire Tmg error 7ms SP3379
# disabled SP 2325 SP 4512 , 5227
# missfire SP 2233 , 4365


newlist = [i for i in os.listdir(PATHHOME) if i[0:3] == "uyz"]      #forinlistdir#
#print newlist

newlist2 = sorted(newlist)        #sort#
fz1 = open("out.txt" , 'w')

for file1 in newlist2:
    var=[       #var=[#
    line1
    for line1 in open(file1)      #forinopen#
    if "SP" in line1 or "shot" in line1 or "autofire" in line1
    ] #finduvar#
    w1b = ''.join(var)

    w1c = re.sub('Time.*?ms|Tmg.*?ms|dis.*?ed|f.*?d' , '' , w1b)     #sub#

    print w1c
    fz1.write(w1c)

fz1.close()


# resultat :
# Missfire SP 2233 , 4365
# SP  data1
# autofire SP 4512 , 5227
#   shot 2325
# ------------- SP
#  data1 SP 4431
# autofire  SP3379
#  SP 2325 SP 4512 , 5227
# missfire SP 2233 , 4365

#fin#
#%_1100

haut

Supprimer une colonne avec SPLIT - MATCH - SUB #part1280


#!/usr/bin/python

#@_1280y_supprimer_colonnes_en_marquant_colonnes_avec_signes!!format_split_match_sub
#deb#

import re

IN="6-in1.txt"

OUT1="6-out1.txt"
OUT2="6-out2.txt"
OUT3="6-out3.txt"

# IN : 
# 1 data01 end of line 1a
# 2 data02 end of line 2a
# 3 data03 end of line 3a
# 4 data04 end of line 4a
# 5 data05 end of line 5a
# 6 data06 end of line 6a
# 10 data10 end of line 10a
# 11 data11 end of line 11a

############################

# 1/2 :  methode  1  :  methode la plus rapide pour supprimer la colonne  1

with open(IN, 'r') as before1:      #withopen#
    town1 = open (OUT1 , "w")
    for line1 in before1:        
        w1 = line1.split()[1:]         # supprimer la premiere colonne
#        w1 = line1.split()[:-1]               # supprimer la derniere colonne

        w2 = ' '.join(w1)
        print w2
        town1.write("{0}\n".format(w2))        #format#

before1.close()
town1.close()

# resultat :
# data01 end of line 1a
# data02 end of line 2a
# data03 end of line 3a
# data04 end of line 4a
# data05 end of line 5a
# data06 end of line 6a
# data10 end of line 10a
# data11 end of line 11a

print " "
print  "###############################"
print " "

#########################

# 2/2 :  methode  2

# coller un signe   %%   a la colonne  1  pour avoir :   1%%

with open(IN, 'r') as before2:      #withopen#
    town2 = open (OUT2 , "w")
    for line2 in before2:
        if re.match('^[1-9]', line2):                  #match#
            syntaxe = ("1%%" + line2.strip())
            print syntaxe
            town2.write("{0}\n".format(syntaxe))         #format#

before2.close()
town2.close()

# resultat :
# 1%%1 data01 end of line 1a
# 1%%2 data02 end of line 2a
# 1%%3 data03 end of line 3a
# 1%%4 data04 end of line 4a
# 1%%5 data05 end of line 5a
# 1%%6 data06 end of line 6a
# 1%%10 data10 end of line 10a
# 1%%11 data11 end of line 11a

print " "
print  "###############################"
print " "

# supprimer la colonne 1 :

f1 = open(OUT2 , 'r')
fw = open(OUT3 , "w")

for line in f1:
    line = re.sub(r'(.*) (.*) (.*) (.*) (.*) (.*)' , r'\2 \3 \4 \5 \6' , line)       #sub#
    print line.strip()
    fw.write(line)
fw.close()

# resultat :
# data01 end of line 1a
# data02 end of line 2a
# data03 end of line 3a
# data04 end of line 4a
# data05 end of line 5a
# data06 end of line 6a
# data10 end of line 10a
# data11 end of line 11a

#fin#
#%_1280

haut

Supprimer une colonne avec SUB #part1101


#!/usr/bin/python

#@_1101y_supprimer_une_colonne_avec_sub!!sub
#deb#

import re

IN="1-in1.txt"
OUT="1-out1.txt"

# BUT = supprimer la colonne 3

# IN
# 341m9lt  0002  341m9lt  0003
# 389m9ky  0391  389m9ky  0392
# 100m9lr  0104  100m9lr  0105


f1 = open(IN , 'r')
fw = open(OUT , "w")

for line in f1:
    line = re.sub(r'(.*) (.*) (.*) (.*)' , r'\1 \2 \4' , line)       #sub#
    print line.strip()
    fw.write(line)

fw.close()

# resultat :
# 341m9lt  0002  0003
# 389m9ky  0391  0392
# 100m9lr  0104  0105

#fin#
#%_1101

haut

Capturer la premiere et derniere colonne FINDALL #part1102


#!/usr/bin/python

#@_1102y_capture_premiere_derniere_colonne!!findall_join
#deb#

import re

IN="2-in4b.txt"
OUT="2-out4b.txt"

PATTERN="#"         # pattern recherche


# IN :
# line001 @@@ occur : 1 # occur : 2 # occur : 3 # occur : 4 # occur : 5 # occur : 6 #
# line002 @@@ occur : 1 # occur : 2 # occur : 3 #
# line003 @@@ occur : 1 # occur : 2 # occur : 3 # occur : 4 #

f36 = open(OUT , 'w')
f35 = open(IN , 'r')
for line35 in f35:
    line35 = line35.strip()

    z1 = ' '.join(re.findall(r'\b\w+\b', line35)[:1])        #  1ere colonne       #find#
    z2 = ' '.join(re.findall(r'\b\w+\b', line35)[-1:])       # derniere colonne sans signe #

#    z2 = ' '.join(re.findall(r'\b\w+\b', line35)[-2:])        # resultat :  occur 6  (sans le #)
#    z2 = ' '.join(re.findall('\S+', line35)[-4:])        # resultat :  occur : 6 #

    print z1 , " : nb occurrences de : " , PATTERN , " : " , z2
    w = z1 , " : nb occurrences de : " , PATTERN , " : " , z2
    w2 = ''.join(w)
    f36.write(w2 + "\n")

# resultat :
# line001  : nb occurrences de :  #  :  6
# line002  : nb occurrences de :  #  :  3
# line003  : nb occurrences de :  #  :  4

#fin#
#%_1102

haut

Des mots contenus dans un fichier vont supprimer des lignes dans un autre fichier #part1270


#!/usr/bin/python

#@_1270y_deleter_lignes_si_des_mots_sont_dans_1_fichier!!read_splitlines_any_inforin
#deb#

import re

# BUT : des mots contenus dans DELETE vont deleter des lignes dans IN
# voir script semblable dans la partie IF ANY :
# deleter_lignes_si_des_mots_sont_dans_1_fichier

# les lignes doivent avoir un blanc a la fin
IN = "6a-in-2colonnes.txt"       # fichier a 2 colonnes

DELETE = "6b-delete-mots.txt"     # a ete fait a partir de IN (fichier a 2 colonnes)

OUT1 = "6c-inter.txt"       # a ete fait a partir de DELETE (fichier a 1 colonne)

OUT2 = "6d-result.txt"

# IN    :       #  fichier a 2 colonnes avec un blanc au bout des lignes
# 988 , 
# 916 de 
# 797 est 
# 781 la 
# 132 etre 
# 081 meme 
# 080 au 
# 076 fait 
# 076 amitie 
# 070 desir 
# 069 peut 


# DELETE :      #  fichier a 2 colonnes avec un blanc au bout des lignes
# 988 , 
# 916 de 
# 797 est 
# 781 la 
# 081 meme 
# 080 au 
# 076 fait 
# 069 peut 

###########################

# retirer la colonne 1

f1 = open(DELETE , 'r')        
fw1 = open(OUT1 , 'w')         # OUT1 = "6c-inter.txt"
for line1 in f1:

    #w1 = re.sub('\(.*?\)|\..*? ' , ' ' , line1).replace("," , "")    # action : retirer les parentheses    
    #w2 = "\t".join(w1.split()[:-1])               # retirer la derniere colonne

    w3 = "\t".join(line1.split()[1:])        # retirer la premiere colonne

    fw1.write(" " + w3 + " \n")        # ajouter blanc avant et apres la colonne
fw1.close()

#########################

# retirer les lignes de IN si elles sont dans OUT1 :

bad_words = open (OUT1).read().splitlines()       # mots avec blanc avant et apres
 
with open(IN) as oldfile:              
    newfile = open (OUT2 , 'w')
    for line in oldfile:
        if not any(bad_word in line for bad_word in bad_words):       
            print line.strip()
            newfile.write(line)

oldfile.close()
newfile.close()

# resultat : OUT2
# 132 etre 
# 076 amitie 
# 070 desir 

#fin#
#%_1270

haut

Supprimer la premiere et derniere colonne et portions de textes SUB #part1103


#!/usr/bin/python

#@_1103y_supprimer_premiere_derniere_colonne_et_parentheses!!sub_replace_split_join
#deb#

import os
import re

IN="1-in.txt"

OUT1="2-out1.txt"
OUT2="2-out2.txt"


# IN :
# Vienna at021va 6,331 6,349(+18) 6,358(+8) 6,357(0) 6,357(0) 6,516(+158) 6,545(+28)  214
# Vienna at019va 274,582 281,061(+6,479) 281,539(+477) 281,539(+0) 281,539 285,882(+4,342) 288,727(+2,845)  14,145
# Vienna at018va 663,052 663,035(-16) 665,059(+2,024) 665,059(+0) 665,059 665,061(+1) 665,061(+0)  2,009
# Vienna at007va 9.65 9.65 9.65 9.65 9.65 9.65 9.65  0

######

# retirer les parentheses , retirer les ',' , effacer les decimales apres '.'
# retirer la col1 et la derniere colonne

f1 = open(IN , 'r')        #open#
fw1 = open(OUT1 , 'w')
for line1 in f1:

    w1 = re.sub('\(.*?\)|\..*? ' , ' ' , line1).replace("," , "")    # action : retirer les parentheses      #sub#       #replace#

    w2 = "\t".join(w1.split()[:-1])         #split#      # retirer la derniere colonne

    w3 = "\t".join(w2.split()[1:])        # retirer la premiere colonne
    print w3

    fw1.write(w3 + "\n")
fw1.close()

#################

# difference entre  col3 et col9

print "----------"
f2 = open(OUT1 , 'r')
fw2 = open(OUT2 , 'w')

for line2 in f2:
    col0 = line2.split()[0]
    col1 = line2.split()[1]
    col7 = line2.split()[-1:]
    col7b = ''.join(col7)

    z = int(col7b) - int(col1)
    z2 = col0 , str(z)
    z3 = ' '.join(z2)
    print z3
    fw2.write(z3 + "\n")
fw2.close()

# resultat :
# at021va       6331    6349    6358    6357    6357    6516    6545
# at019va       274582  281061  281539  281539  281539  285882  288727
# at018va       663052  663035  665059  665059  665059  665061  665061
# at007va       9       9       9       9       9       9       9
# ------------
# at021va 214
# at019va 14145
# at018va 2009
# at007va 0

#fin#
#%_1103

haut

Arrondir une colonne a la decimale inferieure ou superieure #part1104


#!/usr/bin/python

#@_1104y_arrondir_1_colonne_a_la_decimale_inf_ou_sup_operation!!split_float_round_join
#deb#

IN="in.txt"
OUT="out.txt"

# BUT : arrondir a la decimale  inferieure ou superieure (operation)

# IN :
# 1001 125.3
# 1002 167.8
# 1003 293.5
# 1004 327.2
# 1005 159.6


f1 = open(IN , 'r')        #open#
fw1 = open(OUT , 'w')
for line1 in f1:
    col1 = line1.split()[0]       #split#
    col2 = line1.split()[1]
    col2 = float(col2)
    col2b = round(col2)
#    print col2b                    # decimale  .0 conservee
    print('%g' %col2b)       # suppression de la decimale  .0

    col2c = ('%g' %col2b)
#    print col1 , col2c
    w = col1 , col2c
    wb = ' '.join(w)
    fw1.write(wb + "\n")
fw1.close()

# resultat de out.txt
# 1001 125
# 1002 168
# 1003 294
# 1004 327
# 1005 160

#fin#
#%_1104

haut

IF ANY - SANS VAR=[ - fichier2:readsplitlines + fichier1:with open + if any @ Capturer des lignes #part1105


#!/usr/bin/python

#@_1105y_capture_ligne_si_pattern_contenu_dans_1_fichier!!read_splitlines_any
#deb#

# 1-ing.txt :
# working as
# working for

# 1-start.txt :
# The protesters went to Washington to march against increased taxes.
# I'm working as a teaching assistant to support myself as a graduate student in order to get an advanced degree.
# Let us mark the occasion by opening a bottle of champagne.
# An advantage of working for the business is that they provide a pension scheme for their employees.
# Wear this oxygen mask till the pressure returns to normal.


# capturer dans  1-start.txt  les patterns contenus dans cette liste : good_words
good_words = open ("1-ing.txt").read().splitlines()       #openreadsplitlines#

with open("1-start.txt") as oldfile:       #withopen#
    newfile = open ("1-out.txt" , 'w')
    for line in oldfile:
#        print line.strip()

        if any(good_word in line for good_word in good_words):     #ifany#
            print line.strip()
            newfile.write(line)
oldfile.close()
newfile.close()

# resultat :
# I'm working as a teaching assistant to support myself as a graduate student in order to get an advanced degree.
# An advantage of working for the business is that they provide a pension scheme for their employees.

#fin#
#%_1105

haut

IF ANY - SANS VAR=[ - FIC2:readsplitlines + FIC1:readlines @ Colorier plusieurs mots dans un texte #part1107


#!/usr/bin/python

#@_1107y_colorier_des_mots_cles!!rawinput_join_read_splitlines_readlines_any_search_format_group_pass
#deb#

from __future__ import print_function
import fixpath
from colorama import init, Fore, Back, Style

init()

import os
import sys
import re
import time


SITE="1-0-RESULT-OF9BA2-CONCAT-only-SITE.txt"

# SITE (de la forme : exemple de lignes :)
# part9999  line data

# liste des mots cles
keysinfile="1-1-liste-ORIGINALE-CLE.txt"

TIME = 1

#############################

# keysinfile :
# cle1
# cle2
# cle3

part = raw_input('taper un numero de  part....   : ')      #rawinput#
print (part)
part2 = "part" , part
part2b = ''.join(part2)
print (part2b)
print ()

###########################

good_words = open (keysinfile).read().splitlines()        #openreadsplitlines#

# de la forme :  part9999  fp = open(IN).read().splitlines()
fs = open(SITE , 'r').readlines()      #openreadlines#
for s in fs:

    if any(good_word in s for good_word in good_words):  # en premier      #ifany#
        for good_word in good_words:                     # en deuxieme

            m = re.search(("(.*)" + good_word + "(.*)") , s)      #search#

            if m and part2b in s:

                m1 = "{0}".format(m.group(1))
                m2 = "{0}".format(m.group(2))

                print (Style.NORMAL + m1
                    + Back.RED + Style.BRIGHT + good_word
                    + Style.NORMAL
                    + Back.RESET + m2
                    , end='')
                print ()

            if not m and part2b in s:
                pass      #pass#

    if not any(good_word in s for good_word in good_words) and part2b in s:
        print (s.strip())

#fin#
#%_1107

haut

IF ANY - SANS VAR=[ - FIC2:readsplitlines + FIC1:withopen + if any @ Supprimer des lignes si des mots sont dans un autre fichier #part1108


#!/usr/bin/python

#@_1108y_deleter_lignes_si_des_mots_sont_dans_1_fichier!!read_splitlines_any_inforin
#deb#

IN = "in.txt"
OUT = "out.txt"
DELETE = "z-delete.txt"

# BUT : des patterns dans une liste vont deleter des lignes les contenant
# voir aussi script semblable dans la partie : LA COLONNE

# data1 = c2 9c
# data2 = c3 a9
# data3 = c3 ae
# data4 = c3 a7
# data5 = c3 a0

# DELETE :
# data1 =
# data2 =

bad_words = open (DELETE).read().splitlines()        #openreadsplitlines#

with open(IN) as oldfile:       #withopen#
    newfile = open (OUT , 'w')
    for line in oldfile:
        if not any(bad_word in line for bad_word in bad_words):       #ifany#
            print line.strip()
            newfile.write(line)

oldfile.close()
newfile.close()

# resultat :
# data3 = c3 ae
# data4 = c3 a7
# data5 = c3 a0

#fin#
#%_1108

haut

REGEX + FIC1:for + if any @ Lire un fichier ligne par ligne #part1109


#!/usr/bin/python

#@_1109y_xargs_chercher_pattern_ds_1_fichier_avec ANY!!rawinput_split_endswith_any_inforin_join_len_readlines_continue_regex_xargs
#deb#

import os
import re

# fichier entree : IN   provient d'1 scan fait avant :

# 091956 /nfs/u/0-0-04-WR-FRdef/1-0-WR-FR-8-21.txt 85071
# 076392 /nfs/u/tests-2017-10/result-with-out-yield.txt 84229
# ^^col1    ^^col2                                      ^^col3


IN="1-0-result.txt"       # <--- fichier entree

NEW1="1-file1.txt"
NEW2="1-file2.txt"

LIMITMIN="000010"      # limite du nombre de lignes
LIMITMAX="001000"

PATTERN = raw_input("entrer un mot cle : ")      #rawinput#

print PATTERN

regex = ["old" , "OLD"]       #regex#

EXTENSION=".py"

############

# habillage : capture colonnes 1 et 2

fw1 = open(NEW1 , 'w')
f1 = open(IN , 'r')        #open#

for line1 in f1:
    col0 = line1.split()[0]       #split#
    col1 = line1.split()[1]

    if col0 > LIMITMIN and col0 < LIMITMAX:
        z = col1
        z2 = ''.join(z)
        fw1.write(z2 + "\n")

fw1.close()

##############

# idem que xargs
# capture du PATTERN dans les lignes des fichiers
# utilisation de ANY pour selectionner des fichiers

f3 = open(NEW2 , 'w')
f4 = open(NEW1 , 'r')
for file4 in f4:
    file4 = file4.strip()
    if file4.endswith(EXTENSION) and not any(i in file4 for i in regex):    #ifany#   #endswith#

        f5 = open(file4 , 'r')
        for line5 in f5:
            if PATTERN in line5:
                z = file4 , " @@@ " , line5
                z2 = ''.join(z)
                print z2.strip()
                f3.write(z2)
            if not PATTERN in line5:
                continue        #continue#
f3.close()

##########################

# nb lignes de NEW2

print "----------"
nblines = len(open(NEW2 , 'r').readlines())      #openreadlines#       #len#
print "pattern = " , PATTERN , " ;  nb lignes du fichier : " , NEW2 , " : " , nblines

#fin#
#%_1109

haut

IF ANY - SANS VAR=[ - pattern + fichier1:readlines + IFANY @ Tester l'existence d'un pattern dans un fichier #part1110


#!/usr/bin/python

#@_1110y_tester_existence_d'1_mot_dans_1_fichier!!readlines_any_inforin
#deb#

# in.txt
# to make an attempt at doing something OR to do something
# we made our first attempt in January
# she made every attempt to put him at ease

pattern = "make"

fd = open ("in.txt", 'r').readlines()        #openreadlines#

if any([pattern in s for s in fd]):        #ifany#
    print "ok : le mot : " , pattern , " : existe dans le fichier"
else:
    print "non pas de mot : " , pattern , " dans le fichier"

# resultat :
# ok : le mot :  make  : existe dans le fichier

#fin#
#%_1110

haut

IF ANY - SANS VAR=[ - PATTERN + FOR:readlines + if any @ Tester l'existence d'un mot dans plusieurs fichiers #part1111


#!/usr/bin/python

#@_1111y_tester_existence_d'1_mot_dans_plusieurs_fichiers!!readlines_any
#deb#

import os

pattern="#toto#"

PATH="/Python27/0-0-05-SCAN-SEARCH-XARGS"
#####

newlist = [i for i in os.listdir(PATH) if i[0:6] == "s-part"]       #forinlistdir#

for file in newlist:
    fd = open (file , 'r').readlines()          #openreadlines#

    if any([pattern in s for s in fd]):          #ifany#
        print "ok : le mot : " , pattern , " : existe dans le fichier : " , file
    else:
        print "non pas de mot : " , pattern , " dans le fichier : " , file

# resultat :
# ok : le mot :  #toto#  : existe dans le fichier :  s-part965.txt
# non pas de mot :  #toto#  dans le fichier :  s-part966.txt
# ok : le mot :  #toto#  : existe dans le fichier :  s-part967.txt

#fin#
#%_1111

haut

IF ANY - AVEC VAR=[ - FIC2:readsplitlines + IFANY @ Supprimer des lignes selon un deuxieme fichier [var crochet] #part1112


#!/usr/bin/python

#@_1112y_deleter_lignes_inoutplusieursfichiers_selon_1_fichier!!read_splitlines_listdir_sort_forinopen_any_inforin_join
#deb#

import os

PATHHOME="/Python27/tests-2017-10"

BADWORDS="1-delete.txt"

# y-001.txt
# data 1
# data 2
# 100
# 101

# y-002.txt
# New data 1
# New data 2
# 200
# 201

# BADWORDS :
# data

bad_words = open(BADWORDS).read().splitlines()      #openreadsplitlines#

newlist = [i for i in os.listdir(PATHHOME) if i[0:2] == "y-"]      #forinlistdir#
#print newlist

newlist2 = sorted(newlist)        #sort#

for file1 in newlist2:
    f2 = open("z" + file1 , 'w')
    var=[        #var=[#
    line1
    for line1 in open(file1)      #forinopen#
    if not any(bad_word in line1 for bad_word in bad_words)      #ifany#
    ] #finduvar#

    print ''.join(var)
    w = ''.join(var)
    f2.write(w)

f2.close()

# resultats :
# zy-001.txt :
# 100
# 101

# zy-002.txt :
# 200
# 201

#fin#
#%_1112

haut

IF ANY - AVEC VAR=[ - FIC2:readsplitlines + fpath IFANY @ XARGS : Rechercher une syntaxe dans des sous repertoires [var crochet] #part1113


#!/usr/bin/python

#@_1113y_xargs_capture_syntaxe_dans_fichiers_directories_sub-directories!!rawinput_read_splitlines_walk_len_readlines_any_inforin_join_count_xargs
#deb#

import os
import re

PATH="/Python27/2017-09"

# idem que  xargs

# verifier le contenu de ce fichier : (par defaut fichier vide)
BADWORDS="1-bad_words.txt"      #contenant des patterns a supprimer dans les lignes

GOODWORDS="1-0-good_words.txt"     # contenant des patterns a capturer

OUT="1-0-result-choix-nb-lines.txt"

PATTERNFPATH = raw_input("1/4_Rechercher un pattern ds les fpath (noms des fichiers) - (exemple : py) : ")      #rawinput#
NOTPATTERNFPATH = raw_input("2/4_Retirer un pattern ds les fpath ('blanc' si rien a retirer) : ")

# selection d'1 seul pattern qu'on veut dans les lignes
PATTERNLINE = raw_input("3/4_Rechercher un pattern ds les lignes ('blanc' si rien a retirer) : ")

NBLINESFILES = raw_input("4/4_indiquer limite sup nb lines des fichiers (1 chiffre - pas de blanc) : ")
new = int(NBLINESFILES)

print "mot recherche ds les fpath: " , PATTERNFPATH
print "mot retire des fpath : " , NOTPATTERNFPATH
print "mot recherche dans les lignes: " , PATTERNLINE
print "limite nb lines in files : " , new

print "----------"
print ""

fz1 = open(OUT , 'w')

bad_words = open(BADWORDS).read().splitlines()        #openreadsplitlines#
good_words = open(GOODWORDS).read().splitlines()

count=1

for dname, dirs, file1 in os.walk(PATH):       #walk#
    for fname in file1:
        fpath = os.path.join(dname, fname)

# traitement des fpath
        m1 = len(open(fpath , 'r').readlines())        #openreadlines#       #len#

        if m1 < new and PATTERNFPATH in fpath and not NOTPATTERNFPATH in fpath:

# traitement des lignes
            var=[            #var=[#
            line1
            for line1 in open(fpath)       #forinopen#
            if PATTERNLINE in line1          # selection d'1 seul pattern (manuel - blanc si rien)
            if not any(bad_word in line1 for bad_word in bad_words)      #ifany#
#            if any(good_word in line1 for good_word in good_words)
            ] #finduvar#

            print "@@@@### " , m1 , " @@@@ " , fpath  , " @@@@ " , count , " @@@@\n" , ''.join(var)
            p1 = "@@@@### " , str(m1) , " @@@@ " , fpath  , " @@@@ " , str(count) , " @@@@\n" , ''.join(var)
            p2 = ''.join(p1)
            fz1.write(p2)
            count+=1

fz1.close()

# resultat :
# 1/4_Rechercher un pattern ds les fpath (noms des fichiers) : py
# 2/4_Retirer un pattern ds les fpath (noms des fichiers) : color
# 3/4_Rechercher un pattern ds les lignes : split
# 4/4_indiquer limite sup nb lines des fichiers : 100

# @@@@###  82  @@@@  /Python27/2017-09\0-1-922-OK.py  @@@@  1  @@@@
#         w = line.split()[3]          #split#        # variable memorisee

# @@@@###  72  @@@@  /Python27/2017-09\0-1-974-OK.py  @@@@  2  @@@@

# @@@@###  84  @@@@  /Python27/2017-09\0-1-carcasse-1.py  @@@@  3  @@@@
#         w = line.split()[3]           # variable memorisee
#         line2 = line.split()[3] , line         # modifier la syntaxe ligne contenant le pattern

#fin#
#%_1113

haut

IF ANY - AVEC VAR=[ - FIC2:readsplitlines + IFANY @ Tester l'existence d'un mot dans un fichier [var crochet] #part1114


#!/usr/bin/python

#@_1114y_tester_existence_d'1_mot_dans_1_fichier!!read_splitlines_any_inforin
#deb#

# in.txt
# to make an attempt at doing something OR to do something
# we made our first attempt in January
# she made every attempt to put him at ease


# 1-good-words.txt :
# make

good_words = open("1-good-words.txt").read().splitlines()       #openreadsplitlines#

var=[        #var=[#
line1
for line1 in open("in.txt")        #forinopen#
if any(good_word in line1 for good_word in good_words)       #ifany#
] #finduvar#

w1b = ''.join(var)
print w1b

# resultat :
# to make an attempt at doing something OR to do something

#fin#
#%_1114

haut

IF ANY - AVEC VAR=[ - FIC2:readsplitlines + IFANY @ Capturer des patterns (ecrits ds 1 fichier a part) dans plusieurs fichiers [var crochet] #part1115


#!/usr/bin/python

#@_1115y_capture_dans_plusieurs_fichiers_de_plusieurs_patterns_stockes_dans_1_fichier!!read_splitlines_forinlistdir
#deb#

import os

GOODWORDS="1-good_words.txt"

VERIF1="2-verif-start-end1.txt"
VERIF2="2-verif-start-end2.txt"

PATTERN1="#op#"
PATTERN2="#closed#"

# HOME
PATH="/Python27/tests-2018-05d"

TIME=1

# 1-good_words.txt
# #op#
# #closed#


# a-part1.txt
# part901 @@%@@ #op#
# part901 @@%@@
# part901 @@%@@ import time
# part901 @@%@@ import glob
# part901 @@%@@ import os
# part901 @@%@@
# part901 @@%@@ LIMIT=30
# part901 @@%@@ #closed#
# part901 @@%@@
# part901 @@%@@ # 901


# a-part2.txt
# part903 @@%@@ #op# #op#
# part903 @@%@@
# part903 @@%@@ for currentFile in glob.glob('*'):
# part903 @@%@@     e1 = "z-";
# part903 @@%@@     if currentFile.startswith(e1):
# part903 @@%@@         os.remove(currentFile)
# part903 @@%@@ #closed#
# part903 @@%@@
# part903 @@%@@ # 903


# 1/2_ capture des LIGNES contenant les patterns   ---> liste de LIGNES

f41 = open(VERIF1 , 'w')
good_words = open(GOODWORDS).read().splitlines()       #openreadsplitlines#

newlist40 = [k for k in os.listdir(PATH) if k[0:6] == "a-part"]      #forinlistdir#
for new40 in newlist40:


    var=[        #var=[#
    line1
    for line1 in open(new40)        #forinopen#
    if any(good_word in line1 for good_word in good_words)       #ifany#
    ] #finduvar#

    w1 = ''.join(var)
    print w1                # print les lignes

#    w2 = new40 , w1
#    w2b = ''.join(w2)
    f41.write(w1 + "\n")
f41.close()

print "--------------------"
#################################

# 2/2_ capture des PATTERNS directement    ---> liste de PATTERNS

count1=1
count2=1
f42 = open (VERIF1 ,'r')
f43 = open (VERIF2 ,'w')

for line42 in f42:
    line42 = line42.strip()
    w = line42.split()
    for z in w:
	if any(good_word in z for good_word in good_words):
            print line42.split("@@%@@")[0] , z

# resultat :
# part901 @@%@@ #op#
# part901 @@%@@ #closed#

# part903 @@%@@ #op# #op#
# part903 @@%@@ #closed#

# --------------------
# part901  #op#
# part901  #closed#
# part903  #op#
# part903  #op#
# part903  #closed#

#fin#
#%_1115

haut

IF ANY - AVEC VAR=[ - IFANY REGEX SPLIT @ Rechercher un pattern [var crochet] #part1116


#!/usr/bin/python

#@_1116y_xargs_chercher_pattern_dans_fichier_de_path!!rawinput_forinlistdirif_split_forinopenif_endswith_any_regex_join_xargs
#deb#

import os

# script concu pour traiter des path de fichiers  (pas des fichiers avec data concatenes) :
# exemple :    /Python27/tests-2018-06/0-3019.py

# DEPART : provient d'1 scan deja fait dans 1 repertoire :
IN1="1-in1.txt"        # <--- DEPART

# out
OUT="1-out.txt"

LIMITMIN="020"      # nb lignes des fichiers
LIMITMAX="065"

# RAWINPUT reel :
RAWINPUT = raw_input("entrer un mot cle : ")      # ici : join       #rawinput#

print RAWINPUT         # ici : join

regex = ["905" , "921"]      # exclusion        #regex#

EXTENSION=".txt"

PATHHOME="/Python27/tests-2018-06"

# IN1 :
# 015 /Python27/tests-2018-06/1-file.py
# 038 /Python27/tests-2018-06/1-919.py
# 039 /Python27/tests-2018-06/1-905.txt
# 045 /Python27/tests-2018-06/1-920.txt
# 062 /Python27/tests-2018-06/1-909.txt
# 073 /Python27/tests-2018-06/1-921.txt

# contenu de : 1-920.txt :
# join

####################

# utilisation de xargs

f1 = open(IN1 , 'r')
f2 = open(OUT , 'w')

newlist = [i for i in os.listdir(PATHHOME) if i == IN1]      #forinlistdir#
#print newlist

for file in newlist:

    var=[         #var=[#
    line
    for line in open(file)           #forinopen#
    if line.split()[-1].endswith(EXTENSION)         #endswith#       #split#

    if line.split()[0] > LIMITMIN and line.split()[0] < LIMITMAX       # selection du nb lignes

    if not any(j in line.split()[0] for j in regex)      #ifany#
    ]
    #finduvar#
    print ""
    for k in var:        # ouverture des lignes
        k = k.strip()

        k2 = k.split()[1]
        k01 = k.split()[0] , k.split()[1]
        k01b = ' '.join(k01)

        f5 = open(k2 , 'r')      #open#
        for line5 in f5:
            line5 = line5.strip()
            if RAWINPUT in line5:
                print k01b , " .. " , line5
                k3 = k01b , " .. " , line5
                k4 = ''.join(k3)
                f2.write(k4 + "\n")
f2.close()

# resultat :

# 045 /Python27/tests-2018-06/1-920.txt  ..  join

#fin#
#%_1116

haut

Capture avec IF ANY + mono REGEX (raw_input) #part1117


#!/usr/bin/python

#@_1117y_capture_lignes_avec_ifany!!rawinput_split_ifany
#deb#

IN="1-exemple.txt"

# IN :
# don't forget to switch the lights off when you leave
# we'll carry on from where we left off
# we finished what was left of the cake

PATTERN = raw_input("entrer un mot : ")      #rawinput#

mot = [PATTERN]         #regex#
w = ' '.join(mot)
print " ---> mot recherche : " , w

count=1
for line in open(IN):       #forinopen#
    line = line.split()       #split#
#    print line    # ["don't", 'forget', 'to', 'switch', 'the', 'lights', 'off', 'when', 'you', 'leave']
#          de la meme forme que :    mot = [PATTERN]

    m = ' '.join(line)
#    print m       # don't forget to switch the lights off when you leave

#   's'   sera du meme type dans  "line"  et dans   "mot"
    if any(s in line for s in mot):       #ifany#
        print "ok le mot :  " , w , "  est ds la ligne : " , count , " : " , m
    count+=1

# resultat :
# entrer un mot : the
#  ---> mot recherche :  the
# ok le mot :   the   est ds la ligne :  1  :  don't forget to switch the lights off when you leave
# ok le mot :   the   est ds la ligne :  3  :  we finished what was left of the cake

#fin#
#%_1117

haut

Capture avec IF ANY + multi REGEX (rapide - en desordre) #part1118


#!/usr/bin/python

#@_1118y_capture_lignes_avec_ifany_regex!!regex_split_ifany
#deb#

# BUT : recherche rapide de mots avec IFANY mais dans le desordre

IN="1-exemple.txt"

# IN :
# don't forget to switch the lights off when you leave
# we'll carry on from where we left off
# we finished what was left of the cake


regex = ['when' , 'cake']         #regex#
w = ' '.join(regex)
print " ---> regex recherche : " , w

count=1
for line in open(IN):       #forinopen#
    line = line.split()       #split#
#    print line    # ["don't", 'forget', 'to', 'switch', 'the', 'lights', 'off', 'when', 'you', 'leave']
#          du meme type que :    regex = ['when' , 'cake']

    m = ' '.join(line)
#    print m       # don't forget to switch the lights off when you leave

#   's'   sera du meme type dans  'line'  et dans   'regex'
    if any(s in line for s in regex):       #ifany#
        print "ok regex est ds la ligne : " , count , " : " , m

    count+=1

# resultat :
#  ---> regex recherche :  when cake
# ok regex est ds la ligne :  1  :  don't forget to switch the lights off when you leave
# ok regex est ds la ligne :  3  :  we finished what was left of the cake

#fin#
#%_1118

haut

Capture avec IF IN + multi REGEX (numero de ligne precise) #part1119


#!/usr/bin/python

#@_1119y_capture_mots_avec_numero_ligne_regex!!regex_split
#deb#

IN="1-exemple.txt"
OUT="2-out.txt"

# IN :
# don't forget to switch the lights off when you leave
# we'll carry on from where we left off
# we finished what was left of the cake


regex = ['when' , 'cake']         #regex#
w = ' '.join(regex)
print " ---> regex recherche : " , w

count=1
fw = open(OUT , 'w')
for line in open(IN):       #forinopen#
    line = line.split()       #split#
    m = ' '.join(line)

    for i in regex:
        if i in line:
            print i , " : se trouve a la ligne : " , count , " : " , m
            k = str(count)
            k2 = i , " : se trouve a la ligne : " , k , " : " , m
            k3 = ''.join(k2)
            fw.write(k3 + "\n")
    count+=1
fw.close()

# resultat :
# ---> regex recherche :  when cake
# when  : se trouve a la ligne :  1  :  don't forget to switch the lights off when you leave
# cake  : se trouve a la ligne :  3  :  we finished what was left of the cake

#fin#
#%_1119

haut

Capture avec IF ANY + fichier2: read splitlines (out mono fichier) #part1120


#!/usr/bin/python

#@_1120y_grep_capture_pattern_si_contenu_dans_1_fichier_IF_ANY!!read_splitlines_any_inforin_grep
#deb#

# 7/7_ grep avec IF ANY

# in-goodword.txt :
# working as
# working for

# in7.txt :
# The protesters went to Washington to march against increased taxes.
# I'm working as a teaching assistant to support myself as a graduate student in order to get an advanced degree.
# Let us mark the occasion by opening a bottle of champagne.
# An advantage of working for the business is that they provide a pension scheme for their employees.
# Wear this oxygen mask till the pressure returns to normal.

good_words = open ("in-goodword.txt").read().splitlines()        #openreadsplitlines#

with open("in7.txt") as oldfile:        #withopen#
    newfile = open ("out7.txt" , 'w')
    for line in oldfile:
#        print line.strip()

        if any(good_word in line for good_word in good_words):      #ifany#
            print line.strip()
            newfile.write(line)
oldfile.close()
newfile.close()

# resultat :
# I'm working as a teaching assistant to support myself as a graduate student in order to get an advanced degree.
# An advantage of working for the business is that they provide a pension scheme for their employees.

#fin#
#%_1120

haut

Capture avec IF IN + fichier2 (out multi fichier) #part1121


#!/usr/bin/python

#@_1121y_grep_capture_pattern_si_contenu_ds_1_fichier!!read_splitlines
#deb#

import re
import glob
import os
import time

TIME=1

goodwords = open("1-all-patterns.txt").read().splitlines()      #openreadsplitlines#

# 1-all-patterns.txt :
# %pattern1%
# %pattern2%

# 1-start.txt :
# to make an attempt at doing something %pattern1% OR to do something
# we made our first attempt in January %pattern1%
# she made every attempt to put him at ease %pattern2%


###########

# deleter les anciens resultats

for currentFile in glob.glob('*'):
    e1 = "2-out-%";
    if currentFile.startswith(e1):
        os.remove(currentFile)

print " ---> ok anciens fichiers deletes"
time.sleep(TIME)

############

for goodword in goodwords:

    for line1 in open("1-start.txt"):         #forinopen#
        line1 = line1.strip()
        if goodword in line1:
            fw1 = open("2-out-" + goodword + ".txt" , 'a')
            print goodword , " : " , line1
            z = goodword , " : " , line1
            z2 = ''.join(z)
            fw1.write(z2 + "\n")
fw1.close()

# resultats :
# 2-out-%pattern1%.txt :
# %pattern1% : to make an attempt at doing something %pattern1% OR to do something
# %pattern1% : we made our first attempt in January %pattern1%

# 2-out-%pattern2%.txt :
# %pattern2% : she made every attempt to put him at ease %pattern2%

#fin#
#%_1121

haut

Capture avec [var crochet] incluant SEARCH #part1122


#!/usr/bin/python

#@_1122y_grep_avec_search!!forinlistdirif_search_join_grep
#deb#

import re
import os

PATH="/Python27/2017-10"

PATTERN="%%-"

# 1-in1.txt :
# %%-85 title1
# data1
# line3
# %%-62 title2
# data2

fz1 = open("out2.txt" , 'w')

newlist = [i for i in os.listdir(PATH) if i[0:3] == "in1"]      #forinlistdir#
#print newlist

for file1 in newlist:

    var=[       #var=[#
    line1
    for line1 in open(file1)            #forinopen#
    if re.search(PATTERN , line1)      #search#
    ] #finduvar#

    print ''.join(var)
    w1b = ''.join(var)     # adapter le '\n' avec le resultat
    fz1.write(w1b)
fz1.close()

# resultat :
# %%-85 title1
# %%-62 title2

#fin#
#%_1122

haut

Capture avec [var crochet] incluant MATCH #part1123


#!/usr/bin/python

#@_1123y_grep_avec_match!!forinif_match_read_splitlines_grep_join
#deb#

import re

IN="in2.txt"

# IN :
# 5
# 6
# 5
# 8
# 3
# 8
# 9
# 6


var=[i for i in open(IN) if not re.match("8" , i)]     #forinopen#       #var=[#      #match#
#finduvar#
print '\n'.join(var)     # rajouter le  \n

#resultat :
# 5
# 6
# 5
# 3
# 9
# 6

#fin#
#%_1123

haut

Capture avec [var crochet] incluant STARTSWITH + REGEX #part1124


#!/usr/bin/python

#@_1124y_grep_capture_patterns_multi_regex!!regex_forinopen_startswith_join
#deb#

# in6.txt :
# line1
# ----- data1
# python
# @ data2
# script
# !! data3
# end of test
# % text

regex1=("!" , "-")       #regex#
regex2=("@" , "%")

fz1 = open("out6.txt" , 'w')
var=[        #var=[#
line
for line in open("in6.txt")         #forinopen#
if line.startswith(regex1) or line.startswith(regex2)     #startswith#
] #finduvar#

print ''.join(var)
w1b = ''.join(var)
fz1.write(w1b)
fz1.close()

# resultat : zout.txt :
# ----- data1
# @ data2
# !! data3
# % text

#fin#
#%_1124

haut

Capture avec 'contenu +=' #part1125


#!/usr/bin/python

#@_1125y_grep_avec_accumulation!!cumul_grep
#deb#

# The protesters went to Washington to march against increased taxes.
# I'm working as a teaching assistant to support myself as a graduate student in order to get an advanced degree.
# Let us mark the occasion by opening a bottle of champagne.
# we really achieved something today   on a vraiment bien avance aujourd'hui
# we need to build on our achievements   il faut consolider nos succes


PATTERN = "achieve"     # pattern recherche
cumul = ""

f1 = open("in3.txt" , "r")       #open#
f2 = open("out3.txt" , 'w')

for line in f1:
    if PATTERN in line:
        cumul += line

print cumul       # caller cette ligne a colonne 0
f2.write(cumul)     # caller cette ligne a colonne 0

f1.close()
f2.close()

# resultat :
# we really achieved something today   on a vraiment bien avance aujourd'hui
#  we need to build on our achievements   il faut consolider nos succes

#fin#
#%_1125

haut

Capture avec SEARCH #part1126


#!/usr/bin/python

#@_1126y_grep_avec_search!!search_format_grep
#deb#

import re

# in4.txt :
# lang="fr" >
# content=text
# abandon - English-French WordReference.com
# description content=abandon - traduction anglais-francais
# Language content=fr

PATTERN = "abandon"     # pattern recherche

f2 = open("out4.txt" , 'w')
f1 = open("in4.txt" , 'r')       #open#
for line1 in f1:
#    if re.search("{0}".format(PATTERN) , line1):
    if re.search(PATTERN , line1):       #search#

        print line1.strip()
        f2.write(line1)
f2.close()

# resultat :
# abandon - English-French WordReference.com
# description content=abandon - traduction anglais-francais

#fin#
#%_1126

haut

Capture avec RANGE LEN (idem que 'grep -B' en Bash) #part1127


#!/usr/bin/python

#@_1127y_grep_avec_capture_ligne_n-1_si_pattern_ligne_n!!readlines_range_len_tableau
#deb#

# in5.txt :
# 6014 pattern
# 6014 : 200 5 1000 col4 col5 col6
# 6014 : data4 col2 col3 col4 col5
# 6015 pattern
# 6015 : info1a col2 col3 col4 col5
# 6015 : info2a col2 col3 col4 col5
# 6016 pattern
# 6016 : data7 124 5476 819 337
# 6016 : data9 22 713 4389 773
# 6017 pattern


fd30 = open ("in5.txt" , 'r').readlines()       #openreadlines#
fd31 = open ("out5.txt" , "w")

for i in range(len(fd30)):      #rangelen#

    if "pattern" in fd30[i]:
        print fd30[i-1].strip()
        fd31.write(fd30[i-1])
fd31.close()

# resultat :
# 6014 : data4 col2 col3 col4 col5
# 6015 : info2a col2 col3 col4 col5
# 6016 : data9 22 713 4389 773

#fin#
#%_1127

haut

Capture avec RANGE LEN une ligne avant un pattern #part1284


#!/usr/bin/python

#@_1284y_compter_lignes_capturer_avant_pattern!!while_readline_range_len
#deb#

# compter des lignes et capturer 1 ligne avant un pattern  (ici : ------)

LIST4="y-site4.txt"
LIST5="y-site5.txt"
LIST6="y-site6.txt"

# LIST4
# 0-
# 0-
# 0-
# --------------------------------------
# 00
# 00
# 00
# 00
# --------------------------------------
# 01
# 01
# 01
# 01
# --------------------------------------
# 02
# 02

#####################

# compter le nombre de lignes entre les separateurs  "---------"

count = 1
fyw = open(LIST5 , 'w')     #open#
f = open(LIST4 , 'r')
line = f.readline()
while line:                     #while#
#    print str(count) , line.strip()
    y = str(count)
    y2 = y + " " + line.strip()
    line = f.readline()

    if "---" in line.strip():
        count = -1                 # compteur remis a zero apres  "---"
    fyw.write(y2 + "\n")
    count +=1
fyw.close()

###########################

# capturer une ligne avant un pattern     (ici : ------)

data2 = open (LIST5).readlines()      #openreadlines#
fp = open (LIST6, "w")

for i in range(len(data2)):      #rangelen#
    if "0 -" in data2[i]:               # pattern de capture
        print data2[i-1].strip()
        z = data2[i-1].strip()
        fp.write(z + "\n")
fp.close()

# resultat :
# 3 0-
# 4 00
# 4 01
# 2 02

#fin#
#%_1284

haut

Technique de capture : readlines : tableau IF selectionne des colonnes #part1128


#!/usr/bin/python

#@_1128y_technique_de_capture_tableau_colonne_CONTINUE_ds_1_fichier!!continue_readlines_tableau
#deb#

# zin.txt :
# data1 line1
# @ data2 line2
# data3 line3
# @ data 4 line4


fd1 = open ("zin.txt" , 'r').readlines()      #openreadlines#
fd2 = open ("zout.txt" , "w")

for line1 in fd1:
    if line1[0] == "@":
        continue        #continue#
    print line1.strip()    # type(line1)   'str'
    fd2.write(line1)
fd2.close()

# resultat :
# data1 line1
# data3 line3

#fin#
#%_1128

haut

Technique de capture : readlines - tableau RANGE LEN selectionne des lignes (mono fichier) #part1129


#!/usr/bin/python

#@_1129y_technique_de_capture_tableau_ligne_dans_1_fichier_RANGE_LEN!!readlines_range_len_tableau
#deb#

import time

IN="start.txt"
OUT="out.txt"

PATTERN="@@"

TIME=1

# IN :
# she
# plans
# to
# attempt
# @@
# the
# record
# again
# in
# June


data = open (IN).readlines()       #openreadlines#
fp = open (OUT,"w")
for i in range(len(data)):      #rangelen#
    if PATTERN in data[i]:
        time.sleep(TIME)
        print data[i-2].strip()
        time.sleep(TIME)
        print data[i-1].strip()
        time.sleep(TIME)
        print data[i].strip()
        time.sleep(TIME)
        print data[i+1].strip()
        time.sleep(TIME)
        print data[i+2].strip()
        time.sleep(TIME)

        fp.write(data[i-2])
        fp.write(data[i-1])
        fp.write(data[i])
        fp.write(data[i+1])
        fp.write(data[i+2])
print "  --> resultat ds : " , OUT
fp.close()

# resultat :
# to
# attempt
# @@
# the
# record

#fin#
#%_1129

haut

Technique de capture : readlines - tableau RANGE LEN selectionne des lignes (multi fichier) #part1130


#!/usr/bin/python

#@_1130y_technique_de_capture_tableau_ligne_RANGE_LEN_inoutplusieursfichiers!!forinlistdir_split_readlines_range_len_replace_endswith_tableau
#deb#

import os

# a-file1.txt :
# text to delete
# accentuated
# accentuating
# end

# a-file2.txt :
# text to delete
# allowed
# allowing
# end


PATTERN1="ing"

PATH="/nfs/u/tests-2018-02"

###############

newlist = [i for i in os.listdir(PATH) if i[0:6] == "a-file"]      #forinlistdir#
#print newlist

for id25 in newlist:
    id25=id25.strip()
    id25b=id25.split("-")[1].split(".")[0]       #split#
#    print id25b

    f26 = open("b-" + id25b + ".txt" , "w")

    data = open (id25).readlines()      #openreadlines#
    for i in range(len(data)):      #rangelen#
        data[i] = data[i].strip()
#        data[i] = data[i].replace('text to delete' , '')   # inclure 1 replace ici       #replace#

        if data[i].endswith(PATTERN1):   # or data[i].endswith(PATTERN2):      #endswith#

            print data[i-1].strip()
            print data[i].strip()
            print data[i+1].strip()
            print ""
            f26.write(data[i-1] + "\n")
            f26.write(data[i] + "\n")
            f26.write(data[i+1] + "\n")

f26.close()

# resultat :

# b-file1.txt :
# accentuated
# accentuating
# end

# b-file2.txt :
# allowed
# allowing
# end

#fin#
#%_1130

haut

Technique de capture : readline - WHILE 1 ajoute titres devant lignes (multi fichier) #part1131


#!/usr/bin/python

#@_1131y_technique_capture_while1_modifier_ligne_inoutplusieursfichiers!!forinlistdirif_split_readline_while_break_join
#deb#

import os

PATH="/Python27/tests-2018-02"

# BUT : placer 1 titre devant les lignes ds plusieurs fichiers

# 1-serie@motcle1 :
# data part80  SEPAR  data1
# data part81  SEPAR  data2

# 1-serie@motcle2 :
# data part90  SEPAR  data3
# data part91  SEPAR  data4


newlist = [i for i in os.listdir(PATH) if i[0:8] == "1-serie@"]      #forinlistdir#
#print newlist

for file in newlist:
    cle = file.split("@")[1]       #split#

    f1 = open ("1-serie@" + cle , 'r')
    f2 = open ("2-serie@" + cle , 'w')
    line1 = f1.readline()         #openreadline#

    while 1:       #while#
        road = cle , line1.strip()  # placer titre devant lignes
        road2 = ' '.join(road)
        print road2

        if not line1:
            break        #break#
        line1 = f1.readline()

        f2.write(road2 + "\n")
f2.close()

# resultat :

# 2-serie@motcle1 :
# motcle1 data part80  SEPAR  data1
# motcle1 data part81  SEPAR  data2

# 2-serie@motcle2 :
# motcle2 data part90  SEPAR  data3
# motcle2 data part91  SEPAR  data4

#fin#
#%_1131

haut

Technique de capture : withopen - SPLIT selectionne des colonnes (mono fichier) #part1132


#!/usr/bin/python

#@_1132y_technique_de_capture_withopen_modifier_colonne_split_mono_fichier!!split_join
#deb#

# START
# col1 col2 data1 line1
# col1b col2b data2 line2


with open("start2.txt" , 'r') as f22:       #withopen#
    f23 = open ("out.txt" , 'w')
    for line22 in f22:

#        m22 = line22.split()[:2]     # 2 premieres colonnes       #split#
        m22 = line22.split()[2:]    # 2 dernieres colonnes

        print ' '.join(m22)
        m23 = ' '.join(m22)
        f23.write(m23 + "\n")
f23.close()

# resultat pour : split()[:2]
# col1 col2
# col1b col2b

# resultat pour : split()[2:]
# data1 line1
# data2 line2

#fin#
#%_1132

haut

Technique de capture : open lines - SPLIT coupe en mots (mono fichier) #part1133


#!/usr/bin/python

#@_1133y_technique_de_capture_split_word_dans_1_fichier!!split_endswith
#deb#

# BUT : decouper les lignes par mot

# IN :
# Working in front of the blast furnace ,  I was exposed to scorching heat every day. - chaleur accablante - fournaise
# The worker objected to having to work late since he wouldn't get paid for the extra hours.

IN="in.txt"

f = open (IN ,'r')      #open#
fw = open("out.txt" , 'w')

for lines in f:
    word = lines.split()         #split#         # decouper les lignes par mots
    for g in word:

        if g.endswith("ing"):        #endswith#
            print g , " %%% " , lines.strip()
            road = g + " %%% " + lines
            fw.write(road)
fw.close()

# resultat :
# Working  %%%  Working in front of the blast furnace ,  I was exposed to scorching heat every day. - chaleur accablante - fournaise
# scorching  %%%  Working in front of the blast furnace ,  I was exposed to scorching heat every day. - chaleur accablante - fournaise
# having  %%%  The worker objected to having to work late since he wouldn't get paid for the extra hours.

#fin#
#%_1133

haut

Technique de capture : open lines - CUMUL += lines (mono fichier) #part1134


#!/usr/bin/python

#@_1134y_technique_de_capture_accumulation_avec_modif_ds_1_fichier!!cumul
#deb#

# The protesters went to Washington to march against increased taxes.
# I'm working as a teaching assistant to support myself as a graduate student in order to get an advanced degree.
# Let us mark the occasion by opening a bottle of champagne.
# we really achieved something today   on a vraiment bien avance aujourd'hui
# we need to build on our achievements   il faut consolider nos succes


PATTERN = "achieve"     # pattern recherche
cumul = ""

f1 = open("in3.txt" , "r")       #open#
f2 = open("out3.txt" , 'w')

for line in f1:
    if PATTERN in line:
        cumul += line

print cumul       # caller cette ligne a colonne 0
f2.write(cumul)     # caller cette ligne a colonne 0
f2.close()

# resultat :
# we really achieved something today   on a vraiment bien avance aujourd'hui
#  we need to build on our achievements   il faut consolider nos succes

#fin#
#%_1134

haut

Technique de capture : read splitlines dans [var crochet] - WHILE boucle sur fichiers a ouvrir (out mono fichier) #part1135


#!/usr/bin/python

#@_1135y_technique_de_capture_while_pour_files_for_pour_lines!!while_forinopenreadsplitlines_join
#deb#

import os
import re


# in1.txt :
# data1 line1
# data1 line2

# in2.txt :
# data2 line1
# data2 line2

# in3.txt :
# data3 line1
# data3 line2

# ajouter +1 au nombre de fichiers (3+1)
COUNTFILES=4

fw = open("out.txt" , 'w')

i = 1
while i < COUNTFILES:       #while#
    var=[j for j in open("in" + str(i) + ".txt").read().splitlines()]       #forinopenreadsplitlines#       #var=[#
    #finduvar#
    w2b = '\n'.join(var)
    print w2b
    fw.write(w2b + "\n")
    i += 1
fw.close()

# resultat :
# data1 line1
# data1 line2
# data2 line1
# data2 line2
# data3 line1
# data3 line2

#fin#
#%_1135

haut

MIN et MAX des elements d'une liste : shortest - longest #part1136


#!/usr/bin/python

#@_1136y_min_max_shortest_longest_elements_d_1_liste!!len_min_max
#deb#

cars = ['honda' , 'ford' , 'kia' , 'toyota' , 'jaguar' , 'citroen' , 'peugeot']


lenList = [len(car) for car in cars]       #len#
for car in cars:
    if len(car) == min(lenList):
        print "Shortest: %s" % car

    if len(car) == max(lenList):
        print "Longest: %s" % car

# resultat :
# Shortest: kia
# Longest: citroen
# Longest: peugeot

#fin#
#%_1136

haut

Capturer une ligne et la modifier avec un tableau #part1137


#!/usr/bin/python

#@_1137y_capture_modifier_ligne_par_tableau!!replace_tableau_readlines_startswith
#deb#

import os

# BUT :  remplacer un pattern d'une ligne
#       utilisation d'1 tableau

IN="1-in.txt"
OUT="1-out.txt"

# IN
# %data1/login/larousse/file1.txt
# @data2/login/collins/file2.txt

f10 = open (OUT , 'w')
f9 = open (IN , 'r').readlines()        #openreadlines#

print ""
print "f9 moins 1 : " , f9[-1].strip()
print "f9 moins 2 : " , f9[-2].strip()

if f9[-1].startswith("@"):     #startswith#
    f9[-1] = f9[-1].replace('@' , 'stop ')       #replace#
    f10.writelines(f9)

print "----------"
print "verif du remplacement  de @ par stop : "
print ""
print "f9 moins 1 : " , f9[-1].strip()
print "f9 moins 2 : " , f9[-2].strip()

f10.close()

# resultat :
# f9 moins 1 :  @data2/login/collins/file2.txt
# f9 moins 2 :  %data1/login/larousse/file1.txt
# ----------------------
# verif du remplacement  de @ par stop :

# f9 moins 1 :  stop data2/login/collins/file2.txt
# f9 moins 2 :  %data1/login/larousse/file1.txt

#fin#
#%_1137

haut

Lire IN2 uniquement si la colonne 1 de IN2 est dans IN1 #part1282


#!/usr/bin/python

#@_1282y_selectionner des lignes avec 2 fichiers!!while 1_readlines_split_break
#deb#

import time

TIME=1

# BUT : lire IN2 uniquement si la colonne 1 de IN2 est dans IN1
# Supprimer les verbes parasites ( earn , stand , tan ... )

# boucle while 1 sur un fichier  IN2  et lire un autre fichier  IN1  qui controlera cette boucle while 1

IN1="7-essai-1.txt"
IN2="7-essai-2.txt"
OUT="7-out.txt"

# IN1 :

# hear
# instruct
# learn
# teach
# understand

################

# IN2 :

# hear @@@ entendre
# hear @@@ apprendre
# instruct @@@ instruire apprendre
# earn @@@ gagner
# learn @@@ apprendre
# teach @@@ enseigner
# teach @@@ apprendre
# stand @@@ supporter
# stand @@@ resister a
# tan @@@ hale  
# tan @@@ bronzage  
# understand @@@ concevoir
# understand @@@ apprendre

#################


f2 = open(IN2 , 'r')
f3 = open(OUT , 'w')

while 1:       #while#
    line2 = f2.readline()               #openreadline#
    z2 = line2.split(" @@@ ")[0]       #split#
    
    f1 = open(IN1 , 'r')
    for i1 in f1:
        i1b = i1.strip()       # <----- important
        if i1b in z2:

            print line2.strip() + " " + i1b
            inter = line2.strip() + " " + i1b
            time.sleep(TIME)
            f3.write(inter + "\n")

    if not line2:
        break        #break#
f3.close()

# resultat :
# hear @@@ entendre hear
# hear @@@ apprendre hear
# instruct @@@ instruire apprendre instruct
# learn @@@ apprendre learn
# teach @@@ enseigner teach
# teach @@@ apprendre teach
# understand @@@ concevoir understand
# understand @@@ apprendre understand

#fin#
#%_1282

haut

Lire plusieurs lignes avec WHILE [var crochet] #part1138


#!/usr/bin/python

#@_1138y_lire_lignes_d_1_fichier_avec_while!!sum_readlines_len_forinopen_while_count
#deb#

IN="in2.txt"

# IN :
# data1 line1
# data2 line2
# data3 line3


num_lines1 = sum(1 for line in open (IN))       #sumforinopen#
print "nombre de lignes du fichier : " , num_lines1    # 3

fd = open(IN).readlines()       #openreadlines#
num_lines2 = int(len(fd))       #len#
print "nombre de lignes du fichier : " , num_lines2    # 3

# lire plusieurs lignes avec WHILE

count = 0
while (count < num_lines1):       #while#
    var=[       #var=[#
    line.strip()
    for line in open (IN)      #forinopen#
    ] #finduvar#
    print var[count]
    count += 1

# resultat :
# nombre de lignes du fichier :  3
# nombre de lignes du fichier :  3

# data1 line1
# data2 line2
# data3 line3

#fin#
#%_1138

haut

Lire un fichier ligne par ligne avec WHILE1 #part1139


#!/usr/bin/python

#@_1139y_while_1_lit_fichier_ligne_par_ligne!!while_readline_replace_break
#deb#

# in2.txt :
# data1 line1
# data2 line2
# data3 line3
# road1 line4
# road2 line5
# end end


fd = open("in2.txt" , 'r')
fw = open("1-out2.txt" , 'w')

while 1:       #while#
    line = fd.readline()      #openreadline#
    if not "road" in line:

        line = line.replace("data1" , "new")       #replace#
        print line.strip()
        fw.write(line)
    if not line:
        break        #break#
fw.close()

# resultat :
# new line1
# data2 line2
# data3 line3
# end end

#fin#
#%_1139

haut

Lire un fichier ligne par ligne avec WHILE et ANY #part1140


#!/usr/bin/python

#@_1140y_convertir_ASCII_en_HEXA!!read_splitlines_readline_count_while_encode_hex_sub_inforin_join_any_break
#deb#

import binascii
import base64
import re
import time

TIME=0

# @ @local: %%
# @ @local/share: %% -rw------- 1
# @ @local/share: %%
# @ @local/share/Trash: %% -rw------- 1
# @ @local/share/Trash: %%
# @ @local/share/Trash/files: %% -rw-rw-r-- 1
# ici ligne de caracteres speciaux (impossible a ecrire en html)

# 1-liste-hexa.txt :
# 7e
# b1
# f4

carac = open ("1-liste-hexa.txt").read().splitlines()       #openreadsplitlines#

count = 1
fd1 = open("data.txt" , "r")
fd2 = open("out.txt" , "w")

line = fd1.readline()     #openreadline#

while line:       #while#

    m = line.encode("hex").strip()
    m2 = re.sub('([0-9,a-z][0-9,a-z])', r'\1 ', m)      #sub#
    print str(count) , " - " , m2
    m3 = str(count) , " - " , m2
    m4 = ''.join(m3)

    if any(word in m2 for word in carac):       #ifany#
        fd2.write(m4 + "\n")         # ecriture seulement de la ligne de caracteres speciaux
        break        #break#
    line = fd1.readline()

    fd2.write(m4 + "\n")     # ecriture sans la ligne de caracteres speciaux
    count = count + 1
    time.sleep(TIME)

fd2.close()

# resultat :
# 1  -  40 20 40 6c 6f 63 61 6c 3a 20 25 25 20 0a
# 2  -  40 20 40 6c 6f 63 61 6c 2f 73 68 61 72 65 3a 20 25 25 20 2d 72 77 2d 2d 2d 2d 2d 2d 2d 20 31 20 0a
# 3  -  40 20 40 6c 6f 63 61 6c 2f 73 68 61 72 65 3a 20 25 25 20 0a
# 4  -  40 20 40 6c 6f 63 61 6c 2f 73 68 61 72 65 2f 54 72 61 73 68 3a 20 25 25 20 2d 72 77 2d 2d 2d 2d 2d 2d 2d 20 31 20 0a
# 5  -  40 20 40 6c 6f 63 61 6c 2f 73 68 61 72 65 2f 54 72 61 73 68 3a 20 25 25 20 0a
# 6  -  40 20 40 6c 6f 63 61 6c 2f 73 68 61 72 65 2f 54 72 61 73 68 2f 66 69 6c 65 73 3a 20 25 25 0a
# 7  -  7e 06 30 b1 f4 13 e4 de ea 8a 8e 8c 08 b6 74 a2 61 8e e9 37 36 3a ee 9b 6f f3 91 de 52 6c 05 77 5a 6f ec 08 57 92

#fin#
#%_1140

haut

Assembler plusieurs fichiers avec WHILE 1 #part1141


#!/usr/bin/python

#@_1141y_assembler_inplusieursfichiers_while_1!!forlistdirif_while_readline_break_concat
#deb#

import os

PATH="/Python27/2018-01"

# 3-data1.txt :
# line1 data1
# line2 data1

# 3-data2.txt :
# line1 data2
# line2 data2

# 3-data3.txt :
# line1 data3
# line2 data3


newlist = [ word for word in os.listdir(PATH) if word[:2]=='3-' ]   # (= concatenation)      #forinlistdir#
#print newlist        # ['3-data1.txt', '3-data2.txt', '3-data3.txt']

fw2 = open("out2.txt" , 'w')

for file in newlist:
    fd = open(file , 'r')     #open#
    while 1:       #while#
        line = fd.readline()   # lire lignes de tous les fichiers avec WHILE

        print line.strip()
        fw2.write(line)
        if not line:
            break        #break#
fw2.close()

# resultat :
# line1 data1
# line2 data1
# line1 data2
# line2 data2
# line1 data3
# line2 data3

#fin#
#%_1141

haut

Convertir en HEXA et ASCII #part1281


#!/usr/bin/env python

#@_1281y_encoder_en_hexa_changer_caracteres_encoder_en_ascii!!listdir_readsplitlines_oschdir_encode_replace_join_chr_int
#deb#

import os
import re
import glob
import time

TIME=1

# BUT : convertir des lignes  ascii  en  hexa, changer des caracteres hexa et convertir en ascii

##########################
##########################

# activer les actions qui conviennent :         <-------------------

# 1_ supprimer les contenus des dossiers      OUBIEN :
# 2_ supprimer des dossiers      OUBIEN :
# 3_ creer des dossiers 

###########

# 1_ supprimer les contenus des dossiers automatiquement :
# PATH0="C:/Users/toto/Desktop/dir"
# for f1 in os.listdir(PATH0):
#     os.remove(os.path.join(PATH0, f1))

# oubien :
 
# 2_ supprimer des dossiers automatiquement :
# os.rmdir("C:/Users/toto/Desktop/dir)

# oubien :

# 3_ creer des dossiers automatiquement :
# os.mkdir("C:/Users/toto/Desktop/dir)

###########

#  Oubien : option manuelle : creer avant manuellement les dossiers  :  site1-a-depart
#                                                                       site2-a-arrivee-ascii
#                                                                       site3-a-arrivee-ascii

#  A FAIRE : changer la lettre :
LETTRE="a"      # changer la lettre                <---------------- a faire

PATH1="C:/Users/toto/dico-cambridge/site1-" + LETTRE + "-depart"

PATH2="C:/Users/toto/dico-cambridge/site2-" + LETTRE + "-arrivee-hexa"

PATH3="C:/Users/toto/dico-cambridge/site3-" + LETTRE + "-arrivee-ascii"

############

#  attention : ne pas oublier : ecrire les caracteres hexa a changer   :   e   a  .....    <--------------  a faire
HEXATOCHANGE="1-liste-hexa-to-change.txt"

# HEXATOCHANGE :
# c3 a9                             #     e   accent aigu    <--------------- sera change en  :  65      ( e )
# c3 a8                             #     e   accent grave  <--------------- sera change en  :  65      ( e )
# c3 a0                             #     a   accent grave  <--------------- sera change en  :  61      ( a )
# e2 80 99                       #     apostrophe         <--------------- sera change en  :  20     (    )

################

# dans le repertoire     site1-a-depart    ( mis dans  PATH1 )    se trouvent par exemple 3 fichiers :

# o-abstain.txt :   (fichier 1)
# s abstenir (de)                             # apostrophe
# He abstained from  alcohol  

# o-allude.txt :     (fichier 2)
# to formal 
# to mention  
# faire allusion (a)           # a accent grave

# o-araise.txt :       (fichier 3)
# to begin to exist  
# survenir
# se presenter                                 # e accent aigu
# Questions have arisen. 
# Des questions se sont posees.          # e accent aigu
# Problems will arise. 
# Des problemes surviendront.           # e accent grave

carac = open (HEXATOCHANGE).read().splitlines()       #openreadsplitlines#

##########################################
# PARTIE  1  :  convertir ascii en hexa  (en corrigeant des caracteres hexa : accents , apostrophes ...)
##########################################

# deleter anciens fichiers  hexa :
os.chdir(PATH2)

for currentFile in glob.glob('*'):
    e1 = "hexa-";
    if currentFile.startswith(e1):
        os.remove(currentFile)
print " ---> ok anciens fichiers  hexa  deletes"
time.sleep(TIME)

######################

# deleter anciens fichiers  ascii :
os.chdir(PATH3)

for currentFile in glob.glob('*'):
    e2 = "ascii-";
    if currentFile.startswith(e2):
        os.remove(currentFile)

print " ---> ok anciens fichiers  ascii  deletes"
time.sleep(TIME)

##############################

newlist = [i for i in os.listdir(PATH1) if i[0:2] == "o-"]         #forinlistdir#

for id18 in newlist:
    id18=id18.strip()
    id18b=id18.split("o-")[1].split(".")[0]       #split#

    os.chdir(PATH1)
    fr = open(id18 , "r")    

    for line18 in fr:       
#        conversion en hexa :
	m = line18.encode("hex").strip()
#        separer les caracteres par un blanc et les regrouper 2 par 2 :
 	m2 = re.sub('([0-9,a-z][0-9,a-z])', r'\1 ', m)             #sub#

	if any(word in m2 for word in carac):           #ifany#

	    w2 = m2.replace('c3 a9' , '65').replace('c3 a8' , '65')\
	    .replace('c3 a0' , '61').replace('e2 80 99' , '20')\
	    .replace('c3 aa' , '65').replace('c3 a2' , '61').strip() 

	    os.chdir(PATH2)
	    f19 =open("hexa-" + id18b + ".txt" , "a") 
	    f19.write(w2 + "\n")         

	if not any(word in m2 for word in carac):       

	    w2 = m2.replace('c3 a9' , '65').replace('c3 a8' , '65')\
	    .replace('c3 a0' , '61').replace('e2 80 99' , '20')\
	    .replace('c3 aa' , '65').replace('c3 a2' , '61').strip()     

	    os.chdir(PATH2)
	    f19 = open("hexa-" + id18b + ".txt" , "a")
	    f19.write(w2 + "\n")         

f19.close()

print "ok conversion ascii to hexa " 
time.sleep(TIME)

################################
# PARTIE  2  :  convertir hexa en ascii
################################

newlist = [i for i in os.listdir(PATH2) if i[0:4] == "hexa"]         #forinlistdir#

for id20 in newlist:
    id20=id20.strip()

    id20b=id20.split("hexa-")[1].split(".")[0]       #split#

    os.chdir(PATH2)
    fr = open(id20 , "r")    

    for line20 in fr:       
#     conversion  hexa en ascii :
        w = ''.join(chr(int(i, 16)) for i in line20.split())

	os.chdir(PATH3)
	f21 =open("ascii-" + id20b + ".txt" , "a") 
	f21.write(w)         
f21.close()

print "ok conversion hexa to ascii " 
time.sleep(TIME)

# resultat : 

# ascii-abstain.txt    :   fichier 1 :
# s abstenir (de)
# He abstained from  alcohol  

# ascii-allude.txt    :    fichier 2 :
# to formal 
# to mention  
# faire allusion (a)

# ascii-araise.txt     :    fichier 3 :
# to begin to exist  
# survenir
# se presenter
# Questions have arisen. 
# Des questions se sont posees.
# Problems will arise. 
# Des problemes surviendront.

#fin#
#%_1281

haut

Capturer un pattern entre 2 dieses avec SEARCH #part1142


#!/usr/bin/python

#@_1142y_chercher_mots_entre_2_dieses!!search_group_uniq_set_add
#deb#

import re

 # BUT : chercher tous les mots entre 2  dieses

PATH="/Python27/0-0-05-SCAN-SEARCH-XARGS"

# ENTREE
STAT5="1-0-RESULT-codesource-part-devant-30-04-2018.txt"

# SORTIE
OUT1="1-entrediese1.txt"
OUT2="1-entrediese2.txt"


f2 = open(OUT1 , 'w')
f1 = open(STAT5 , 'r')      #open#
for line1 in f1:
    m = re.search("#.*#" , line1)      #search#
    if m:
        code = m.group(0)
        if not " " in code and not "##" in code:
	    print code
            f2.write(code + "\n")
f2.close()

#######################

# unique

uniq = set()      #set#       #uniq#

f3 = open(OUT2, "w")
for line2 in open(OUT1 , "r"):
    if line2 not in uniq:

        uniq.add(line2)
        print line2.strip()
        f3.write(line2)
f3.close()

# resultat :
# #pattern1#
# #pattern2#
# #pattern3#
# #pattern4#

#fin#
#%_1142

haut

Capturer un pattern avec SEARCH #part1143


#!/usr/bin/python

#@_1143y_capture_pattern_avec_search!!search
#deb#

import os
import re

IN="in.txt"
OUT="out.txt"

# IN :
# part925 @@%@@     new = [line2 for line2 in open(file2)]
# part925 @@%@@
# part925 @@%@@ #    print ''.join(new)
# part925 @@%@@
# part925 @@%@@     w2b = ''.join(new)
# part941 @@%@@ for file1 in newlist2:
# part941 @@%@@     new = [
# part941 @@%@@     line1
# part941 @@%@@     for line1 in open(file1)
# part941 @@%@@     ]


# 1/2_ capturer les  w  oubien  var :

with open (IN , 'r') as road1:      #withopen#
    fd = open (OUT, 'w')
    for ligne1 in road1:

        m1 = re.search(("(.*)" + "w = \[") , ligne1) or re.search(("(.*)" + "w1 = \[") , ligne1) \       #search#
        or re.search(("(.*)" + "w2 = \[") , ligne1) \
        or re.search(("(.*)" + "w=\[") , ligne1) or re.search(("(.*)" + "w1=\[") , ligne1) \
        or re.search(("(.*)" + "w2=\[") , ligne1) \
        or re.search(("(.*)" + "w3 = \[") , ligne1) or re.search(("(.*)" + "w3=\[") , ligne1)

        if m1:
            code1 = m1.group(0)
            print code1
            fd.write(code1 + "\n")
fd.write("---------------------\n")
road1.close()

####
print "----------"

# 2/2_ capturer les tabulations

with open (IN , 'r') as road2:
    fd = open (OUT , 'a')
    for ligne2 in road2:

#        m2 = re.search("\w+" + " @@%@@ " + "\t" + "\w+" , ligne2)
        m2 = re.search("(.*)" + "\t" + "(.*)" , ligne2)
        if m2:
            code2 = m2.group(0)
            print code2
            fd.write(code2 + "\n")
road2.close()
fd.close()

# resultat :
# part925 @@%@@     new = [
# part941 @@%@@     new = [

#fin#
#%_1143

haut

Marquer des lignes si patterns uniques dans une colonne avec RANGE LEN #part1273


#!/usr/bin/python

#@_1273y_marquer_lignes_si_uniques!!read_readlines_range_len_split_break
#deb#

IN="5-site.txt"

INTER1="z-inter1.txt"
OUT ="z-out.txt"

# But : marquer les lignes si les chiffres de la colonne 1 sont uniques

# IN :
# 433 N
# 433 O
# 444 N
# 445 N
# 459 O
# 468 O
# 470 N
# 470 O
# 487 O
# 488 N
# 488 O
# 490 N

#####################

# ajouter des fins de ligne a IN

open(INTER1 , "w").write(open(IN).read() + "end end\n" + "end end\n" + "end end\n")    #read#

print "ok ajout de  end  a la fin de IN , resultat ds :" , INTER1
print "################\n"

################################

# marquer les lignes si les chiffres de la colonne 1 sont uniques

fd12 = open (INTER1, 'r').readlines()      #openreadlines#
fd13 = open (OUT, 'w')
for i in range(len(fd12)):      #rangelen#

    element4 = fd12[i-1].split()[0]       #split#
    element5 = fd12[i-1].split()[1]

    element0 = fd12[i].split()[0]       #split#
    element1 = fd12[i].split()[1]

    element2 = fd12[i+1].split()[0]       #split#
    element3 = fd12[i+1].split()[1]

    if (element2 == element0):
        print element0 , element1 , element2 , element3 , "ok\n--------"
        w1 = element0 + " " + element1 + " " + element2 + " " + element3 + " " + "ok\n--------"
        w1b = ''.join(w1)
        fd13.write(w1b + "\n")

    if (element2 != element0) and (element0 != element4):
        print element0 , element1 , " @@\n"
        w2 = element0 + " " + element1 + " " + " @@\n"
        w2b = ''.join(w2)
        fd13.write(w2b + "\n")

    if element3 == "end":
        break        #break#

fd13.close()

# resultat :
# 433 N 433 O ok
# --------
# 444 N  @@

# 445 N  @@

# 459 O  @@

# 468 O  @@

# 470 N 470 O ok
# --------
# 487 O  @@

# 488 N 488 O ok
# --------
# 490 N  @@

#fin#
#%_1273

haut

Capturer des patterns avec RANGE LEN #part1144


#!/usr/bin/python

#@_1144y_capture_pattern_avec_range_len!!read_splitlines_range_len_split_isdigit
#deb#

# in.txt :
# line1 user
# line2 25
# line3 data
# line4 100

#####################

mylist1 = open ("in.txt").read().splitlines()       #openreadsplitlines#

fw1 = open("out1.txt" , 'w')
print mylist1               #           ['line1 user', 'line2 25', 'line3 data', 'line4 100']
print len(mylist1)       # 4       #len#

# ['line1 user\n', 'line2 25\n', 'line3 data\n', 'line4 100\n']   avec  :  'open ("in.txt").readlines()'     #openreadlines#


for i in range(len(mylist1)):      #rangelen#
    if mylist1[i].split()[1].isdigit():       #split#      #isdigit#
        print mylist1[i]
        w1 = mylist1[i]
        fw1.write(w1 + "\n")
fw1.close()

# resultat :
# line2 25
# line4 100

#fin#
#%_1144

haut

Marquer un titre et le repeter s'il ne change pas avec RANGE LEN - mono fichier #part1145


#!/usr/bin/python

#@_1145y_marquer_titre_devant_lignes_et_repeter_si_pas_changement!!read_splitlines_range_len_join_break
#deb#

IN1="1-in1.txt"
IN2="1-in2.txt"
OUT="out.txt"

# in1.txt
# @ new:
# -rw-------   1 data users    51157
# -rw-r--r--   1 data users      189
# @ Local/Modes:
# -rw-rw-rw-  1 data users 1607
# -rw-rw-rw-  1 data users  357
# @ java/fonts:
# -rw------- 1 data users 34937
# -rw------- 1 data users 38241


# 1/2_ ajouter 2 lignes a la fin du fichier:

open (IN2 , "w").write ( open (IN1).read() + "end@end\n")      #openread#

############

# 2/2_ marquer 1 titre devant chaque ligne

mylist = open (IN2).read().splitlines()       #openreadsplitlines#
fw = open(OUT , 'w')

print "      --> nb de lignes de IN2 : " , len(mylist)       #len#

for i in range(len(mylist)):      #rangelen#
    if "@ " in mylist[i]:
        w = mylist[i]

    if not "@ " in mylist[i]:
        print w , "%%%" , mylist[i]
        w2 = w + "%%%" + mylist[i]
        w3 = ''.join(w2)
        fw.write(w2 + "\n")

    if "end@end" in mylist[i]:
        break        #break#
fw.close()

# resultat :
#       --> nb de lignes de IN2 :  10
# @ new: %%% -rw-------   1 data users    51157
# @ new: %%% -rw-r--r--   1 data users      189
# @ Local/Modes: %%% -rw-rw-rw-  1 data users 1607
# @ Local/Modes: %%% -rw-rw-rw-  1 data users  357
# @ java/fonts: %%% -rw------- 1 data users 34937
# @ java/fonts: %%% -rw------- 1 data users 38241
# @ java/fonts: %%% end@end

#fin#
#%_1145

haut

PASTE de 2 fichiers #part1146


#!/usr/bin/python

#@_1146y_coller_2_files_side_by_side_idem_que_paste!!rstrip_readline
#deb#

INTER1="1-pastefile1.txt"
INTER2="1-pastefile2.txt"
INTER3="1-pastfile-result.txt"

# INTER1 :
# A1 A2 A3
# A4 A5 A6
# A7 A8 A9

# INTER2 :
# B1 B2 B3
# B4 B5 B6
# B7 B8 B9


# merge 2 files side by side

comp1 = open (INTER1 , "r")      #open#
comp2 = open (INTER2 , "r")
f5 = open (INTER3 , "w")

for line in comp1:

    wcomp = line.rstrip() + " " + comp2.readline().strip()      #openreadline#
    print wcomp
    f5.write(wcomp + "\n")

comp1.close()
comp2.close()
f5.close()

# resultat
# A1 A2 A3 B1 B2 B3
# A4 A5 A6 B4 B5 B6
# A7 A8 A9 B7 B8 B9

#fin#
#%_1146

haut

PASTE de plusieurs fichiers #part1147


#!/usr/bin/python

#@_1147y_coller_4_files_side_by_side_idem_que_paste!!rstrip_readline_paste
#deb#

# merge 4 files side by side

# in1.txt :
# line1 data1
# line2 data1

# in2.txt :
# line1 data2
# line2 data2

# in3.txt :
# line1 data3
# line2 data3

# in4.txt :
# line1 data4
# line2 data4


INTER1="in1.txt"
INTER2="in2.txt"
INTER3="in3.txt"
INTER4="in4.txt"

OUT="1-out.txt"

comp1 = open (INTER1 , "r")
comp2 = open (INTER2 , "r")
comp3 = open (INTER3 , "r")
comp4 = open (INTER4 , "r")

f5 = open (OUT , "w")

for line in comp1:

    wcomp = line.rstrip() + " " + comp2.readline().strip() \
    + " " + comp3.readline().strip() + " " + comp4.readline().strip()
    print wcomp
    f5.write(wcomp + "\n")

comp1.close()
comp2.close()
comp3.close()
comp4.close()
f5.close()

# resultat :
# line1 data1 line1 data2 line1 data3 line1 data4
# line2 data1 line2 data2 line2 data3 line2 data4

#fin#
#%_1147

haut

PASTE de 2 listes : ZIP (old) #part1148


#!/usr/bin/python

#@_1148y_inserer_trait_separation_ds_1_fichier_si_colonne_1_change_old!!read_splitlines_join_zip_split_break
#deb#

IN="1-separe-trait2.txt"
PASTE="1-separe-trait-paste.txt"
RESULT="1-separe-trait-result.txt"

# IN :        # contient plusieurs colonnes

# accepter     %     accept     %     transitive verb @@
# accepter     %     agree     %     transitive verb @@
# accommoder     %     accommodate     %     transitive verb @@
# accompagner     %     attend     %     transitive verb @@
# accomplir     %     accomplish     %     transitive verb @@
# accomplir     %     achieve     %     transitive verb @@
# end @@
# end @@


# shift de la ligne 1 + PASTE (affichage cote a cote)
#      + insertion d'1 trait de separation

f1 = open (RESULT ,'w')

data = open(IN , 'r').read().splitlines()       #openreadsplitlines#
datanew = data[1:]                  # ligne 1 supprimee

w = zip(data , datanew)             # creation du PASTE
for j in w:
    road = ' '.join(j)

    line1 = road.split("@@")[0]       #split#
    line2 = road.split("@@")[1]

    col1line1 = line1.split()[0]
    col1line2 = line2.split()[0]

    if col1line1 == col1line2:
        print line1
        f1.write(line1 + "\n")

    if col1line1 != col1line2:
        print line1 , "\n--------------"
        road = line1 , "\n--------------"
        road2 = ''.join(road)
        f1.write(road2 + "\n")

    if "end" in road:
        break        #break#

f1.close()

# resultat :

# accepter     %     accept     %     transitive verb
# accepter     %     agree     %     transitive verb
# --------------
# accommoder     %     accommodate     %     transitive verb
# --------------
# accompagner     %     attend     %     transitive verb
# --------------
# accomplir     %     accomplish     %     transitive verb
# accomplir     %     achieve     %     transitive verb
# --------------
# end

#fin#
#%_1148

haut

Ajouter des colonnes avec RANGE LEN #part1149


#!/usr/bin/python

#@_1149y_ajouter_colonnes!!readlines_range_len_tableau
#deb#

# data1.txt
# S    97-2520                4968  163148   449213  6749438          239
# S    97-2520                4969  163159   449178  6749506          239
# S    97-2520                4970  163210   449250  6749472          239
# S    SF1724                  717  152056   387433  6659358          117
# S    SF1724                  718  152106   387484  6659343          117
# S    SF1724                 1870  184159   408916  6678563          117
# S    SF1724                 1871  184212   408906  6678617          117
# S    SF1732                  721  215615   387399  6659538          133
# S    SF1732                  722  215626   387448  6659525          133
# S    SF1732                 1865  014230   408636  6678680          134
# S    SF1732                 1866  014242   408688  6678669          134
# S    SF1740                  721  075653   387267  6659683          133
# S    SF1740                  722  075703   387316  6659671          133
# S    SF1740                  723  075714   387305  6659719          133
# S    SF1740                  724  075723   387354  6659706          133


# marquer la colonne de travail par 1 separateur : @

data1 = open ("data1.txt").readlines()        #openreadlines#
fp = open ("1-nav1.txt","w")
for i in range(len(data1)):      #rangelen#

    new1 = data1[i].split()[0]       #split#
    new2 = data1[i].split()[1]
    new3 = data1[i].split()[2]
    new4 = data1[i].split()[3]
    new5 = data1[i].split()[4]
    new6 = data1[i].split()[5]
    new7 = data1[i].split()[6]

    m =  new1 + " @ " + new2 + " @ " + new3 + " " + new4 + " " + new5 + " " + new6 + " " + new7
    fp.write(m + "\n")

# resultat :
# S @ 97-2520 @ 4968 163148
# S @ 97-2520 @ 4969 163159
# S @ 97-2520 @ 4970 163210
# S @ SF1724 @ 717 152056
# S @ SF1724 @ 718 152106
# S @ SF1724 @ 1870 184159
# S @ SF1724 @ 1871 184212
# S @ SF1732 @ 721 215615
# S @ SF1732 @ 722 215626
# S @ SF1732 @ 1865 014230
# S @ SF1732 @ 1866 014242
# S @ SF1740 @ 721 075653
# S @ SF1740 @ 722 075703
# S @ SF1740 @ 723 075714
# S @ SF1740 @ 724 075723

#fin#
#%_1149

haut

Capturer des lignes si les patterns d'une colonne changent RANGE LEN #part1150


#!/usr/bin/python

#@_1150y_capturer_lignes_si_patterns_d_1_colonne_changent!!readlines_range_len_split_join_tableau
#deb#

LISTE="1-file.txt"

# LISTE :
# 239
# 239
# 239
# 117
# 117
# 117
# 117
# 133
# 133
# 134
# 134
# 133
# 133
# 133

# utilisation d'1 tableau

data2 = open (LISTE).readlines()      #openreadlines#
fp = open ("out.txt","w")

for i in range(len(data2)):      #rangelen#
    new1 = data2[i].split()[0]       #split#
    new2 = data2[i+1].split()[0]

    new1b = int(new1)
    new2b = int(new2)

    if new1b != new2b:
        print "lignes " , i+1 , " - " , i+2 , "  :  " , new1 , " --> " , new2
        new3 = "ligne " , str(i+1) , " - " , str(i+2) , " : " , new1 , " --> " , new2
        new4 = ''.join(new3)

        fp.write(new4 + "\n")
fp.close()

# resultat :
# lignes  3  -  4   :   239  -->  117
# lignes  7  -  8   :   117  -->  133
# lignes  9  -  10   :   133  -->  134
# lignes  11  -  12   :   134  -->  133

#fin#
#%_1150

haut

Compter les occurrences de chaque mot d'un fichier [var crochet] #part1151


#!/usr/bin/python

#@_1151y_count_occurrences_de_chaque_mot_d_1_fichier!!forinopenforin_split_sort_set_count_cumul_join_map_splitlines_reverse_True
#deb#

IN="in.txt"

OUT1="out1-occur.txt"
OUT2="out2-occur.txt"
OUT3="out3-occur.txt"

# IN :
# Tout est bien qui finit bien
# La terre est ronde
# La rue est pleine de monde
# Le monde change vite


var=[i for line in open (IN , "r") for i in line.split()]      #forinopen#        #var=[#       #split#
#finduvar#

f2 = open (OUT1 , "w")

for word in sorted(set(var)):       #set#        #sort#
    w2 = var.count(word) , ":" ,  word
    w3 = ' '.join(map(str,w2))       #map#
    f2.write(w3 + "\n")
f2.close()

##############

# ajouter des digits '0' devant des chiffres

with open(OUT1 , "r") as fww:      #withopen#
    fd4b = open (OUT2 , "w")
    for line3b in fww:
        b = line3b.split(" : ")[0]       #split#
        c = line3b.split(" : ")[1].strip()
        if len(b) == 1:       #len#
#            print "00" + ''.join(b) + " " + c
            fd4b.write("00" + ''.join(b) + " " + c + "\n")

        if len(b) == 2:
#            print "0" + ''.join(b) + " " + c
            fd4b.write("0" + ''.join(b) + " " + c + "\n")

        if len(b) == 3:
#            print "" + ''.join(b) + " " + c
            fd4b.write("" + ''.join(b) + " " + c + "\n")
fww.close()
fd4b.close()

######################

# trier

f3 = open (OUT2 , 'r')
f4 = open (OUT3 , 'w')

t = f3.read().splitlines()       #openreadsplitlines#

d = sorted(t , reverse=True)     # tri en ordre inverse

print '\n'.join(d)
f4.write('\n'.join(d))
f4.close()

# resultat :
# 003 est
# 002 monde
# 002 bien
# 002 La
# 001 vite
# 001 terre
# ......

#fin#
#%_1151

haut

Compter les occurrences de chaque mot d'un fichier (count) #part1152


#!/usr/bin/python

#@_1152y_count_occurrences_de_chaque_mot_d'1_fichier!!read_split_sort_set_cumul_count_occur_join_format_uniq
#deb#

IN="in.txt"
OUT1="out1-occur.txt"
OUT2="out2-occur.txt"
OUT3="out3-occur.txt"

# IN :
# Tout est bien qui finit bien
# La terre est ronde
# La rue est pleine de monde
# Le monde change vite

words = open (IN , "r").read().split()        #openreadsplit#
uniqWords = sorted(set(words))      #set#       #uniq#        #sort#
fd2 = open (OUT1 , 'w')
for word in uniqWords:

    memor1 = words.count(word)
    memor2 = str(memor1)
    memor3 = memor2 , ":" , word
    memor4 = ' '.join(memor3)

    fd2.write("{0}\n".format(memor4))
fd2.close()

############################

# ajouter des digits '0' devant des chiffres

with open(OUT1 , "r") as fww:      #withopen#
    fd4b = open (OUT2 , "w")
    for line3b in fww:
        b = line3b.split(" : ")[0]       #split#
        c = line3b.split(" : ")[1].strip()
        if len(b) == 1:       #len#
#            print "00" + ''.join(b) + " " + c
            fd4b.write("00" + ''.join(b) + " " + c + "\n")

        if len(b) == 2:
#            print "0" + ''.join(b) + " " + c
            fd4b.write("0" + ''.join(b) + " " + c + "\n")

        if len(b) == 3:
#            print "" + ''.join(b) + " " + c
            fd4b.write("" + ''.join(b) + " " + c + "\n")
fww.close()
fd4b.close()

######################

# trier

f3 = open (OUT2 , 'r')
f4 = open (OUT3 , 'w')

t = f3.read().splitlines()       #openreadsplitlines#

d = sorted(t , reverse=True)     # tri en ordre inverse

print '\n'.join(d)
f4.write('\n'.join(d))
f4.close()

# resultat :
# 003 est
# 002 monde
# 002 bien
# 002 La
# 001 vite
# 001 terre
# ......

#fin#
#%_1152

haut

Compter les occurrences de chaque mot d'un fichier (uniq_words) - tri avec [var crochet] #part1153


#!/usr/bin/python

#@_1153y_count_occurrences_de_chaque_mot_d_1_fichier!!read_split_uniq_cumul_occur_join_forinopen_key_reverse_sort_True_lambda
#deb#

IN="in.txt"

OUT1="z-out1.txt"
OUT2="z-out2.txt"
OUT3="z-out3.txt"

# IN :
# Tout est bien qui finit bien
# La terre est ronde
# La rue est pleine de monde
# Le monde change vite


#####################

word_list = open (IN , "r").read().split()       #openreadsplit#
fw1 = open (OUT1 , "w")

uniq_words = []       #uniq#

for word in word_list:
    if word not in uniq_words:
        uniq_words += [word]

for word in uniq_words:

    m1 = str(word_list.count(word)) , ":" , word
    m2 = ' '.join(m1)
    fw1.write(m2 + "\n")
fw1.close()

#####################

# ajouter des digits '0' devant des chiffres

with open(OUT1 , "r") as fww:      #withopen#
    fd4b = open (OUT2 , "w")
    for line3b in fww:
        b = line3b.split(" : ")[0]       #split#
        c = line3b.split(" : ")[1].strip()
        if len(b) == 1:       #len#
#            print "00" + ''.join(b) + " " + c
            fd4b.write("00" + ''.join(b) + " " + c + "\n")

        if len(b) == 2:
#            print "0" + ''.join(b) + " " + c
            fd4b.write("0" + ''.join(b) + " " + c + "\n")

        if len(b) == 3:
#            print "" + ''.join(b) + " " + c
            fd4b.write("" + ''.join(b) + " " + c + "\n")
fww.close()
fd4b.close()

######################

# trier

var=[ligne.strip() for ligne in open (OUT2)]      #forinopen#        #var=[#
#finduvar#

fw2 = open (OUT3 , 'w')

var.sort(key = lambda ligne: int(ligne.split(' ')[0]), reverse=True)       #split#        #sort#     #lambda#

for ligne in var:
    print ligne
    fw2.write(ligne + "\n")
fw2.close()

# resultat :
# 003 est
# 002 monde
# 002 bien
# 002 La
# 001 vite
# 001 terre
# ......

#fin#
#%_1153

haut

Compter les occurrences de chaque mot d'un fichier - dict (cumul+=1) #part1154


#!/usr/bin/python

#@_1154y_count_occurrences_de_chaque_mot_d'1_fichier!!read_dict_split_key_val_iteritems_sort_False_count_cumul_reverse_lambda
#deb#

IN="in.txt"
OUT1="1-out1.txt"
OUT2="1-out2.txt"
OUT3="1-out3.txt"

# IN :
# Tout est bien qui finit bien
# La terre est ronde
# La rue est pleine de monde
# Le monde change vite


fw1 = open(OUT1 , 'w')
fd = open(IN , 'r')
t = fd.read()       #openread#

cumul = dict()          #dic#
words = t.split()       #split#
# print words , type(words)     # tout le fichier en 1 'list'

for word in words:

    if word in cumul:
        cumul[word] += 1
    else:
        cumul[word] = 1

st = ""
for key, val in cumul.iteritems():
    st = st + str(val) + " : " + key + "\n"

# print st , type(st)        # tout le fichier en 1 'str'
fw1.write(st)
fw1.close()

############################

# ajouter des digits '0' devant des chiffres

with open(OUT1 , "r") as fww:      #withopen#
    fd4b = open (OUT2 , "w")
    for line3b in fww:
        b = line3b.split(" : ")[0]       #split#
        c = line3b.split(" : ")[1].strip()
        if len(b) == 1:       #len#
#            print "00" + ''.join(b) + " " + c
            fd4b.write("00" + ''.join(b) + " " + c + "\n")

        if len(b) == 2:
#            print "0" + ''.join(b) + " " + c
            fd4b.write("0" + ''.join(b) + " " + c + "\n")

        if len(b) == 3:
#            print "" + ''.join(b) + " " + c
            fd4b.write("" + ''.join(b) + " " + c + "\n")
fww.close()
fd4b.close()

#######################

# trier

f3 = open (OUT2 , 'r')
f4 = open (OUT3 , 'w')

t = f3.read().splitlines()       #openreadsplitlines#

d = sorted(t , reverse=True)     # tri en ordre inverse

print '\n'.join(d)
f4.write('\n'.join(d))
f4.close()

# resultat :
# 003 est
# 002 monde
# 002 bien
# 002 La
# 001 vite
# 001 terre
# ......

#fin#
#%_1154

haut

Numeroter les lignes qui suivent un meme titre RANGE LEN FLAG #part1155


#!/usr/bin/python

#@_1155y_count_numeroter_lignes_qui_suivent_1_meme_titre_et_remise_a_zero!!readlines_range_len_startswith_flag_count
#deb#

RESULT13="1-in2.txt"
RESULT14="1-out2.txt"

# RESULT13 :
# newy-109-accomplish- | transitive verb
# ) accomplir , executer
# ) realiser
# ) effectuer
# ) venir a bout de , mener a bonne fin
# newy-57-accord- | transitive verb
# ) accorder
# ) reserver

fd13 = open (RESULT13).readlines()       #openreadlines#
fd14 = open (RESULT14 , "w")

for i in range(len(fd13)):      #rangelen#

    if fd13[i].startswith(")"):     #startswith#
        flag = 1      #flag#
#        print count , fd13[i].strip()
        fd14.write(str(count) + fd13[i])

    if fd13[i].startswith("newy"):
        count = 1
        flag = 0
#        print fd13[i].strip()
        fd14.write(fd13[i])
    if flag:
        count = count + 1

fd14.close()

# resultat :
# newy-109-accomplish- | transitive verb
# 1) accomplir , executer
# 2) realiser
# 3) effectuer
# 4) venir a bout de , mener a bonne fin
# newy-57-accord- | transitive verb
# 1) accorder
# 2) reserver

#fin#
#%_1155

haut

Numeroter les lignes qui suivent un meme titre + separer par 1 trait (methode longue : PASTE ZIP) #part1156


#!/usr/bin/python

#@_1156y_occurrence_numeros_lignes_avec_remise_a_zero!!read_splitlines_zip_join_split_break_count
#deb#

IN="1-count-numero.txt"
OUT="1-count-numero-out.txt"

# IN : doit avoir @ en dernier caractere
#     + 1 ligne "start  :  start @"
#     + 2 lignes "end  :  end @"

# start  :  start @
# 6014  :  200 5 1000 col4 col5 col6 @
# 6014  :  201 5 900 col4 col5 col6 @
# 6014  :  202 5 850 col4 col5 col6 @
# 6014  :  203 5 830 col4 col5 col6 @
# 6014  :  204 5 810 col4 col5 col6 @
# 6014  :  data3 col2 col3 col4 col5 @
# 6014  :  data4 col2 col3 col4 col5 @
# 6015  :  info1a col2 col3 col4 col5 @
# 6015  :  info2a col2 col3 col4 col5 @
# 6016  :  data7 124 5476 819 337 @
# 6016  :  data8 173 2416 379 344 @
# 6016  :  data9 22 713 4389 773 @
# end  :  end @
# end  :  end @

f2 = open (OUT , "w")

data = open(IN , 'r').read().splitlines()       #openreadsplitlines#
datanew = data[1:]            # ligne 1 supprimee (idem que "shift")

count=0
w = zip(data , datanew)       # creation du PASTE

for j in w:
    road = ' '.join(j)

    line1 = road.split("@")[0]       #split#
    line2 = road.split("@")[1]

    col1line1 = line1.split()[0]
    col1line2 = line2.split()[0]

    if col1line1 == col1line2:
        print count , line1
        newa = str(count)
        newb = newa , " " , line1
        new2 = ''.join(newb)
        f2.write(new2 + "\n")

    if col1line1 != col1line2:
        print count , line1 , "\n--------------"
        newe = str(count)
        newf = newe , " " , line1 , "\n--------------"
        new4 = ''.join(newf)
        f2.write(new4 + "\n")

        count=0          # remise a zero pour le pattern suivant

    if "end" in road:
        break        #break#

    count=count+1

f2.close()

# resultat :
# 0 start  :  start
# --------------
# 1 6014  :  200 5 1000 col4 col5 col6
# 2 6014  :  201 5 900 col4 col5 col6
# 3 6014  :  202 5 850 col4 col5 col6
# 4 6014  :  203 5 830 col4 col5 col6
# 5 6014  :  204 5 810 col4 col5 col6
# 6 6014  :  data3 col2 col3 col4 col5
# 7 6014  :  data4 col2 col3 col4 col5
# --------------
# 1 6015  :  info1a col2 col3 col4 col5
# 2 6015  :  info2a col2 col3 col4 col5
# --------------
# 1 6016  :  data7 124 5476 819 337
# 2 6016  :  data8 173 2416 379 344
# 3 6016  :  data9 22 713 4389 773
# --------------

#fin#
#%_1156

haut

Modifier la derniere occurrence d'un pattern d'une ligne SUB #part1157


#!/usr/bin/python

#@_1157y_modifier_derniere_occurrence_d'1_pattern!!sub
#deb#

import re

IN="1-in.txt"
OUT="1-out.txt"

# IN :
# %data1/login/larousse/file1.txt
# @data2/login/collins/file2.txt


f1 = open(IN , 'r')      #open#
fw = open(OUT , "w")

for line in f1:
    line = re.sub(r'(.*)/' , r'\1 @ ' , line)     #sub#
    print line.strip()
    fw.write(line)

fw.close()

# resultat
# %data1/login/larousse @ file1.txt
# @data2/login/collins @ file2.txt

#fin#
#%_1157

haut

Compter le nombre de mots pour chaque ligne (ecrit en fin de ligne) LEN FINDALL #part1158


#!/usr/bin/python

#@_1158y_count_cumul_nombre_mots_par_ligne!!read_splitlines_len_split_findall_join_count_cumul
#deb#

import re

# in.txt :
# line612@ generates generating generated
# line613@ gesticulates gesticulating gesticulated
# line614@ getting   gets getting got  gets getting got  gets getting got
# line615@
# line616@ giving   gives giving gave  gives giving gave  gives giving gave

f2 = open("out1.txt" , "w")
f2B = open("out2.txt" , "w")
mylist = open("in.txt").read().splitlines()       #openreadsplitlines#
for i in mylist:
#    print i

    count = len(re.findall(r'\w+' , i))      #find#       #len#

    print i , count      # ecrire en fin de ligne le nb de mot de chaque ligne
    v1 = ''.join(i)
    v2 = str(count)
    v3 = v1 + "  " + v2
    v4 = ''.join(v3)
    f2B.write(v4 + "\n")     # ecrire en fin de ligne le nb de mot de chaque ligne

    m = i.split()[:1]   # premiere colonne       #split#
    m2 = ''.join(m)
    m3 = str(count)
    m4 = m3 + "  " + m2
    m5 = ''.join(m4)
    f2.write(m5 + "\n")

f2.close()
f2B.close()

# resultat :
# line612@ generates generating generated 4
# line613@ gesticulates gesticulating gesticulated 4
# line614@ getting   gets getting got  gets getting got  gets getting got 11
# line615@  1
# line616@ giving   gives giving gave  gives giving gave  gives giving gave 11

#fin#
#%_1158

haut

Compter le nombre de plusieurs mots ou d'expressions dans un fichier #part1274


#!/usr/bin/python

#@_1274y_compter_occurences_de_plusieurs_expressions_dans_fichier_avec_countoutplusieursfichiers!!readline_while 1_break_cumul
#deb#

# le script ne detectera qu une seule occurence d'un mot par ligne

import os
import glob

IN1 = "z-in1.txt"
IN2 = "z-in2.txt"
OUT = "z-out-light.txt"

# Mettre ici les mots ou expressions dont on veut connaitre les occurrences
MOTS="z-alpha.txt"

# MOTS :    (exemple de contenu de  :  z-alpha.txt)
# necessite
# habitude
# volonte
# acte de volonte

PATHHOME = "\Users\jean\Desktop\philo6\saint augustin\contre deuxieme rep pelagien livre4"

# IN1 :
# Il y a entre la volonte et la necessite une opposition telle 
# qu'elles s'excluent et se detruisent mutuellement .
# et a ce sujet tu nous reproches  d affirmer que la seconde
# est nee de la premiere et de soutenir que la necessite 
# est un des fruits de la volonte  . 
# Tu vois cependant que la necessite de l habitude 
# est un fruit manifeste de la volonte .
# Si , comme tu le pretends , la necessite detruit la volonte , 
# n est-ce pas cependant en se multipliant que la volonte 
# a engendre la necessite de l habitude ? 
# Comment donc as-tu ose dire que la volonte et la necessite 
# ne sauraient exister en meme temps , 
# puisque tu les vois resider ensemble lorsqu elles sont 
# d'accord comme lorsqu'elles sont en lutte ?
# Par la meme , le peche ne saurait exister sans un acte de la
# volonte , parce qu il ne peut etre commis sans un acte de la volonte ; 
# et d autre part , le peche peut exister sans un acte de la volonte , 
# parce qu apres avoir ete commis volontairement , il
# subsiste independamment de tout acte de volonte ; 
# et l on se trouve  ainsi soumis a une necessite 
# contraire a la volonte , par suite d un acte de volonte 
# exempt de toute necessite . 
# Suivant vous , la necessite a laquelle est assujetti celui qui dit : 
# Je ne fais pas ce que je veux  , 
# n est pas autre chose que la necessite ou le joug de l habitude .

###########################

# deleter anciens resultats

for currentFile in glob.glob('*'):
    e1 = "zzz-";
    if currentFile.startswith(e1):
        os.remove(currentFile)

for currentFile in glob.glob('*'):
    e2 = "z-out-light";
    if currentFile.startswith(e2):
        os.remove(currentFile)

##########################

# ajouter 1 ligne vide en debut de fichier

open (IN2 , "w").write (" \n" + open (IN1).read())      #openread#

##############################

# creation d'autant de fichiers que de mots dans MOTS 
# il y a ajout des occurrences sans les compter
# le script ne detectera qu une seule occurence d un mot par ligne

f1 = open(MOTS , 'r')       #open#
for mot in f1:
    count = 1
    mot = mot.strip()
    
    f2 = open(IN2 , 'r')
    line2 = f2.readline()      #openreadline#

    while 1:                        #while#
        line2 = f2.readline()
        line2 = line2.rstrip()

        if not line2:
            break        #break#

        if mot in line2:            
            o = open("zzz-" + mot + ".txt" , 'a')
            print >> o , mot , count
            count = count+1
o.close()

##############################

# nb des occurrences de chaque mot recherche = nb lignes de plusieurs fichiers

newlist = [i for i in os.listdir(PATHHOME) if i[0:3] == "zzz"]      #forinlistdir#

for file in newlist:
    file=file.strip()
    word = file.split("zzz-")[1].split(".txt")[0]
    f24 = open(OUT , "a")

    cumul=0
    f23 = open(file , 'r')      #open#
    for lines in f23:
        w = lines.splitlines()
        cumul=cumul+len(w)       #len#

    print word , " : nb occurrence du mot : " , str(cumul)

    road = word , " : nb occurrence du mot : " , str(cumul)
    road2 = ' '.join(road)
    f24.write(road2 + "\n")
f24.close()

# resultat :
# acte de volonte  : nb occurrence du mot :  2
# habitude  : nb occurrence du mot :  3
# necessite  : nb occurrence du mot :  10
# volonte  : nb occurrence du mot :  10

#fin#
#%_1274

haut

Compter cumuler les occurrences d'un pattern pour chaque ligne SPLIT COUNT - exemple 1/3 #part1159


#!/usr/bin/python

#@_1159y_count_cumul_1_pattern_pour_chaque_ligne!!split_count_join
#deb#

IN="in2.txt"
OUT="out.txt"

PATTERN="-"

# IN:
# abide  @@@  tolerate -  last -  stay
# accept  @@@  receive -  take on -  say yes to -  acknowledge -  stand -  welcome
# access  @@@  admission -  entrance -  attack -  acquire

count = 1
with open(IN , 'r') as road1:      #withopen#
    fd = open (OUT , "w")
    for line in road1:
        big = line.split (' ')         #split#    # separation par mot
        element = big.count(PATTERN)
        element2 = str(element)
        if element2 != 0:
            print "line " , count , " : " , element2 , " occurrences du pattern : " , PATTERN
            count2 = str(count)
            w = "line " + count2 + " : " + element2 + " occurrences du pattern : " + PATTERN
            w2 = ''.join(w)
#            fd.write(element2 + "\n")
            fd.write(w2 + "\n")
            count = count + 1

fd.close()

# resultat :
# line  1  :  2  occurrences du pattern :  -
# line  2  :  5  occurrences du pattern :  -
# line  3  :  3  occurrences du pattern :  -

#fin#
#%_1159

haut

Compter cumuler les occurrences d'un pattern pour chaque ligne avec LEN SPLIT - exemple 2/3 #part1160


#!/usr/bin/python

#@_1160y_count_cumul_occurrences_d_1_pattern_pour_chaque_ligne!!len_split_join_count_cumul
#deb#

# 1-in.txt
# abide  @@@  tolerate -  last -  stay
# accept  @@@  receive -  take on -  say yes to -  acknowledge -  stand -  welcome
# access  @@@  admission -  entrance -  attack -  acquire


PATTERN="-"

count = 1
with open ("1-in.txt" , 'r') as f2:       #withopen#
    f3 = open ("out.txt" , 'w')
    for ligne2 in f2:

        w = len(ligne2.split(PATTERN)) -1       #len#       #split#

        print "ligne :" , count , ": nb occurrences du pattern :" , PATTERN , ": " , w

        wb = str(w)

        countb = str(count)
        countc = ''.join(countb)

        result1 = "ligne : " , countc , " : nb occurrences du pattern : " , PATTERN , " : " , wb
        result2 = ''.join(result1)
        f3.write(result2 + "\n")

        count+=1

# resultat :
# ligne : 1 : nb occurrences du pattern : - :  2
# ligne : 2 : nb occurrences du pattern : - :  5
# ligne : 3 : nb occurrences du pattern : - :  3

#fin#
#%_1160

haut

Compter cumuler les occurrences d'un pattern pour chaque ligne et totaliser - exemple 3/3 #part1161


#!/usr/bin/python

#@_1161y_count_cumul_occurrences_pattern_par_ligne!!rawinput_len_split_join_cumul_read_tableau
#deb#

# z.txt :
# data line 1
# line 2
# data line 3

RAW = raw_input("chercher un mot ds les z- : ")    # ici : data      #rawinput#


# habiller le fichier

with open("z.txt" , 'r') as road1:     #withopen#
    fw = open("tz.txt" , 'w')
    for line1 in road1:
        w = len(line1.split(RAW)) -1       #len#       #split#
#        print w
        line1 = line1.strip()
        if RAW in line1:
            print "z.txt" , " % " , RAW , " @%@ " , w , " @%@ " , line1
            wb = str(w)
            element = "z.txt" , " % " , RAW , " @%@ " , wb , " @%@"
            element2 = ''.join(element)
            fw.write(element2 + "\n")

road1.close()
fw.close()

################

# ouvrir le tz et cumuler les occurrences

cumul = 0
fw2 = open("stz.txt" , 'w')
for line5 in open("tz.txt"):
    line5 = line5.split("@%@")[1]
    cumul += int(line5)
#    print cumul
    cumul2 = str(cumul)
    fw2.write(cumul2 + "\n")
fw2.close()

###################

# capture de la derniere ligne du cumul

# exemple 1 :
f6 = open("stz.txt" , 'r')
line6 = f6.readlines()       #openreadlines#
if line6:
    last = line6[-1]
#    print last.strip()

# exemple 2 :
f6 = open("stz.txt" , 'r')
f7 = open("mstz.txt" , 'w')
mylist = f6.read().splitlines()       #openreadsplitlines#
print mylist[-1]
f7.write(mylist[-1])

# resultat :
# chercher un mot ds les z- : data
# z.txt  %  data  @%@  1  @%@  data line 1
# z.txt  %  data  @%@  1  @%@  data line 3
# 2

#fin#
#%_1161

haut

Compter cumuler les occurrences des patterns en colonne 1 RANGE LEN #part1162


#!/usr/bin/python

#@_1162y_count_cumul_occurrences_patterns_colonne_1!!readlines_range_len_split_join_break
#deb#

import time

IN1="2-start1.txt"
IN2="2-start2.txt"
IN3="2-start3.txt"

TIME=2

# IN1
# 6014 : 200 5 1000
# 6014 : 201 5 900
# 6014 : 202 5 850
# 6014 : 203 5 830
# 6014 : 204 5 810
# 6014 : data3 col2
# 6014 : data4 col2
# 6015 : info1a col2
# 6015 : info2a col2
# 6016 : data7 124
# 6016 : data8 173
# 6016 : data9 22

#############

# ajouter 1 ligne a la fin

open(IN2 , 'w').write(open(IN1).read() + "9999 : 9999\n")       #openread#

###########################

# occurrences des patterns (tries) de la colonne 1

count=1
f2 = open(IN3 , 'w')
f1 = open(IN2 , 'r').readlines()       #openreadlines#

for i in range(len(f1)):      #rangelen#
    w1 = f1[i].split()[0]       #split#
    w2 = f1[i+1].split()[0]

    if w2 != w1:
        print "last line of pattern : " , f1[i].split()[0] , " : " , f1[i].strip()
        print "          ---> pattern : " , f1[i].split()[0] , " : nb occurrences : " , count
        count2 = str(count)
        road1 = "pattern : " , f1[i].split()[0] , " : nb occurrences : " , count2
        road2 = ' '.join(road1)
        print "----------"
        count = 0
        f2.write(road2 + "\n")
        time.sleep(TIME)

    if "9999" in f1[i+1].split()[0]:
        break        #break#
    count += 1

# resultat :
# last line of pattern :  6014  :  6014 : data4 col2
#           ---> pattern :  6014  : nb occurrences :  7
# ---------------
# last line of pattern :  6015  :  6015 : info2a col2
#           ---> pattern :  6015  : nb occurrences :  2
# ---------------
# last line of pattern :  6016  :  6016 : data9 22
#           ---> pattern :  6016  : nb occurrences :  3
# ---------------

#fin#
#%_1162

haut

Compter cumuler les occurrences d'un pattern d'un fichier et ecrire ce chiffre en fin de fichier #part1163


#!/usr/bin/python

#@_1163y_compter_cumuler_les_occurrences_d_1_pattern_et_ecrire_ce_chiffre_en_fin_de_fichier!!cumul
#deb#

IN="in.txt"

###########
# exemple de ligne du depart :
# @ write file - % A lookup file operation

cumul = 0
pattern = "@"

teams = open (IN, 'r')      #open#
for line in teams:
    if pattern in line:
        cumul += 1
        print line.strip() , " @!! " , cumul-1   # compteur commence a zero
    else:
        print line.strip()

teams.close()

# resultat : nombre d'occurrences du pattern @ :
# @ write file - % A lookup file operation  @!!  245

#fin#
#%_1163

haut

Repeter une ligne selon un nombre mis dans une colonne #part1164


#!/usr/bin/python

#@_1164y_occurrence_la_colonne_1_indique_le_nombre_de_repetitions_d_1_ligne!!split_range
#deb#

# in.txt
# 3 animal line1
# 2 car line2
# 3 airplane line3

with open ("in.txt" , 'r') as fww:        #withopen#
    fd1 = open ("out.txt" , 'w')
    for ligne1 in fww:

        m1 = ligne1.split()[0].strip()       #split#
        m2 = ligne1.split()[1].strip()
        m3 = ligne1.split()[2].strip()

        m = m2 + " " + m3
        mc = str(m)

        m1b=int(m1)
        for i in range(m1b):     #range#
            print m1 , m

            fd1.write(m1 + " " + mc + "\n")

fww.close()
fd1.close()

# out.txt
# 3 animal line1
# 3 animal line1
# 3 animal line1
# 2 car line2
# 2 car line2
# 3 airplane line3
# 3 airplane line3
# 3 airplane line3

#fin#
#%_1164

haut

Capturer le mot le plus long d'un fichier SPLIT LEN #part1165


#!/usr/bin/python

#@_1165y_capture_mot_le_plus_long_dans_fichier!!read_split_len
#deb#

# on met tout le fichier dans une 'str'

f1 = open("1-0-data-larousse-8-21-converti.txt" , 'r')
texte = f1.read()        #openread#

mot_long = ""

mots = texte.split()         #split#            # on construit la liste des mots.
for mot in mots:
    if len(mot) > len(mot_long):       #len#
        mot_long = mot

print mot_long     # le mot le plus long du fichier

#fin#
#%_1165

haut

Marquer toutes les positions d'un pattern choisi - exemple 1 [var crochet] #part1166


#!/usr/bin/python

#@_1166y_marquer_position_pattern!!readlines_len_xrange_forinif_find_start_finditer_count
#deb#

import re

# BUT : ecrire la position du pattern sur chaque ligne

# 1-in.txt
# abide  @@@  tolerate -  last -  stay
# accept  @@@  receive -  take on -  say yes to -  acknowledge -  stand -  welcome
# access  @@@  admission -  entrance -  attack -  acquire

PATTERN="-"

########################

# nombre de lignes du fichier

f1 = open("in.txt" , 'r').readlines()        #openreadlines#
linecount = len(f1)       #len#
print "---> nb de lignes du fichier : " , linecount
print ""

count = 1
with open ("in.txt" , 'r') as f1:      #withopen#
    for ligne1 in f1:
        var=[n for n in xrange(len(ligne1)) if ligne1.find(PATTERN , n) == n]        #var=[#      #find#      #rangelen#
        #finduvar#
        print "line : " , count , "/ ", linecount , " : position du pattern sur la ligne : " , var
        count +=1
f1.close()
print ""

# resultat :
# ---> nb de lignes du fichier :  3

# line :  1 /  3  : position du pattern sur la ligne :  [21, 29]
# line :  2 /  3  : position du pattern sur la ligne :  [21, 32, 46, 61, 70]
# line :  3 /  3  : position du pattern sur la ligne :  [23, 35, 45]

#fin#
#%_1166

haut

Marquer toutes les positions d'un pattern choisi - exemple 2 [var crochet] #part1167


#!/usr/bin/python

#@_1167y_marquer_position_pattern!!readlines_len_xrange_forinif_find_start_finditer_count
#deb#

import re

# BUT : ecrire la position du pattern sur chaque ligne

# 1-in.txt
# abide  @@@  tolerate -  last -  stay
# accept  @@@  receive -  take on -  say yes to -  acknowledge -  stand -  welcome
# access  @@@  admission -  entrance -  attack -  acquire

PATTERN="-"

with open ("in.txt" , 'r') as f2:        #withopen#
    for ligne2 in f2:
        var=[m.start() for m in re.finditer(PATTERN , ligne2)]         #var=[#      #find#
        #finduvar#
        print "position du pattern sur la ligne :" , var

f2.close()

# resultat :
# position du pattern sur la ligne : [21, 29]
# position du pattern sur la ligne : [21, 32, 46, 61, 70]
# position du pattern sur la ligne : [23, 35, 45]

#fin#
#%_1167

haut

Ajouter des digits 0000 si un champ est vide #part1168


#!/usr/bin/python

#@_1168y_ajouter_0_si_champ_est_vide!!withopen_split_len
#deb#

# BUT : ajouter 0000 s'il n'y a qu'1 champ

IN="2-in1.txt"
OUT="2-out.txt"

# IN :
# 328m9lc 0070
# 100m9lj
# 352m9mb 0048


with open(IN , "r") as f:         #withopen#
    fw = open (OUT , "w")
    for line in f:
        line = line.strip()
        b = line.split()       #split#
        if len(b) <= 1:       #len#
            print line , "0000"
            z = line , " 0000"
            z2 = ''.join(z)
            fw.write(z2 + "\n")

        if len(b) > 1:
            print line
            fw.write(line + "\n")
f.close()
fw.close()

# resultat :
# 328m9lc 0070
# 100m9lj 0000
# 352m9mb 0048

#fin#
#%_1168

haut

Selectionner le nombre de champs qu'on choisit pour chaque ligne #part1169


#!/usr/bin/python

#@_1169y_count_nb_champs_et_choix_pour_selectionner_lignes!!len_split
#deb#

IN="1-data1.txt"
OUT="1-out.txt"

#IN :
# line1 data1
# line2
# line3 data1


fw = open(OUT , 'w')
f1 = open(IN , 'r')
for line1 in f1:
#    print len(line1.split())
    if len(line1.split()) == 2:     #len#       #split#
        print line1.strip()
        fw.write(line1)
fw.close()

# resultat :
# line1 data1
# line3 data1

#fin#
#%_1169

haut

Compter le nombre de champs de chaque ligne et regrouper les lignes #part1170


#!/usr/bin/python

#@_1170y_count_nombre_champs_de_chaque_lignes_et_regrouper_lignes!!len_split_forinopen_count_join_uniq
#deb#

IN="in2.txt"
OUT="2-out.txt"

# IN :
# Vienna at021va 6,358(+8) 6,357(0) 6,357(0) 6,516(+158) 6,545(+28)  214
# Vienna at019va 274,582 281,061(+6,479) 281,539(+477) 281,539(+0) 281,539 285,882(+4,342) 288,727(+2,845)  14,145
# Vienna at018va 663,052 663,035(-16) 665,059(+2,024) 665,059(+0) 665,059 665,061(+1) 665,061(+0)  2,009
# Vienna at007va 9.65 9.65 9.65 9.65 9.65 9.65 0

######

# compter le nombre de champs de chaque ligne et regrouper les lignes ayant meme nb de champs

unique_nbchamp = []       #uniq#

fw3 = open(OUT , 'w')      #open#
result = [len(line.split()) for line in open(IN)]       #len#       #split#
print result     # en liste

# regrouper les lignes ayant meme nombre de champs
for nbchamp in result:
    if nbchamp not in unique_nbchamp:
        unique_nbchamp += [nbchamp]

for nbchamp in unique_nbchamp:
    m1 = int(result.count(nbchamp))
    m1b = str(m1)
    m2 = str(nbchamp)

    print m1b , " lignes contenant : " , m2 , " champs"
    m1c = m1b , " lignes contenant : " , m2 , " champs"
    m1d = ''.join(m1c)
    fw3.write(m1d + "\n")
fw3.close()

# resultat :
# [8, 10, 10, 9]
# 1  lignes contenant :  8  champs
# 2  lignes contenant :  10  champs
# 1  lignes contenant :  9  champs

#fin#
#%_1170

haut

Compter le nombre de champs de chaque ligne avec LEN LINE - SPLIT #part1171


#!/usr/bin/python

#@_1171y_count_nombre_champs_de_chaque_lignes_et_regrouper!!len_split_count_join_readline_while
#deb#

import time

IN="in2.txt"
OUT="out.txt"

TIME=1

# IN :
# Vienna at021va 6,358(+8) 6,357(0) 6,357(0) 6,516(+158) 6,545(+28)  214
# Vienna at019va 274,582 281,061(+6,479) 281,539(+477) 281,539(+0) 281,539 285,882(+4,342) 288,727(+2,845)  14,145
# Vienna at018va 663,052 663,035(-16) 665,059(+2,024) 665,059(+0) 665,059 665,061(+1) 665,061(+0)  2,009
# Vienna at007va 9.65 9.65 9.65 9.65 9.65 9.65 0

######

fw = open(OUT , 'w')
f3 = open(IN , 'r')
line3 = f3.readline()     #openreadline#

count = 1
while line3:       #while#
    print "ligne : " , count , " : nb de champs = " , len(line3.split())       #len#       #split#
    w = "ligne : " , count , " : nb de champs = " , len(line3.split())
    c1 = str(count)
    c2 = str(len(line3.split()))
    w2 = "ligne : " , c1 , " : nb de champs = " , c2
    w3 = ''.join(w2)
    fw.write(w3 + "\n")
    line3 = f3.readline()
    time.sleep(TIME)
    count += 1
fw.close()

# resultat :
# ligne :  1  : nb de champs =  8
# ligne :  2  : nb de champs =  10
# ligne :  3  : nb de champs =  10
# ligne :  4  : nb de champs =  9

#fin#
#%_1171

haut

Supprimer le dernier champ de chaque ligne #part1172


#!/usr/bin/python

#@_1172y_deleter_dernier_champ_de_chaque_ligne!!read_splitlines_rsplit
#deb#

# in.txt : pas d'espace apres le dernier mot
# 1) accomplir , executer
# 2) realiser
# 3) venir a bout de , mener a bonne fin

mylist = open ("in.txt").read().splitlines()      #openreadsplitlines#
fw3 = open ("out.txt" , "w")
for detail in mylist:
    detail = detail.rsplit(" " , 1)[0]       #split#
    fw3.write(detail + "\n")
fw3.close()

# resultat :
# 1) accomplir ,
# 2)
# 3) venir a bout de , mener a bonne

#fin#
#%_1172

haut

Marquer les lignes selon le nombre de champs - LEN #part1173


#!/usr/bin/python

#@_1173y_marquer_lignes_selon_nombre_de_champs!!split_len_join
#deb#

IN="1-in-marquer-champs.txt"

# IN
# line1 data1 data2
# line2 data3 data4
# line3
# line4

with open(IN , "r") as f:         #withopen#
    fw = open ("out.txt" , "w")
    for line in f:
        b = line.split()       #split#
        if len(b) <= 1:       #len#
            print ' '.join(b)
            fw.write(' '.join(b) + "\n")

        if len(b) > 1:
            w = ' '.join(b) , "end"
            w2 = ' '.join(w)
            print w2
            fw.write(' '.join(w) + "\n")

f.close()
fw.close()

# resultat :
# line1 data1 data2 end
# line2 data3 data4 end
# line3
# line4

#fin#
#%_1173

haut

Definir le nombre de champs pour chaque ligne (WHILE LEN) #part1174


#!/usr/bin/python

#@_1174y_maitriser_nombre_de_champs_pour_chaque_ligne!!split_while_len_append_join_count
#deb#

# in.txt
# abide  @@@  tolerate -  last -  stay
# accept  @@@  receive -  take on -  say yes to -  acknowledge -  stand -  welcome
# access  @@@  admission -  entrance -  attack -  acquire

# nombre d'occurrences :
n = 4
sep = "-"

with open ("in.txt" , 'r') as f1:       #withopen#
    f2 = open ("out.txt" , 'w')
    for ligne1 in f1:

        n_split_groups = []       #split#
        groups = ligne1.split(sep)

        while len(groups):          #while#    #len#
            n_split_groups.append(sep.join(groups[:n]))       #append#
            groups = groups[n:]

        w = (n_split_groups)[0]
        w2 = ''.join((n_split_groups)[0].strip())

        print (n_split_groups)[0].strip()
        f2.write(w2 + "\n")

f1.close()
f2.close()

# resultat
# abide  @@@  tolerate -  last -  stay
# accept  @@@  receive -  take on -  say yes to -  acknowledge
# access  @@@  admission -  entrance -  attack -  acquire

#fin#
#%_1174

haut

Supprimer les lignes doubles SET ADD (exemple 1/3) #part1175


#!/usr/bin/python

#@_1175y_deleter_lignes_doubles_set_add!!set_add_uniq
#deb#

# supprime les lignes doubles (ou triples)

# in.txt :
# line1
# data1
# data1
# data1
# line4
# data2
# data2
# line6


uniq = set()       #set#       #uniq#

fw = open("out-sans-doubles.txt", "w")

for line in open("in.txt", "r"):       #open#
    if line not in uniq:

        uniq.add(line)
        print line.strip()
        fw.write(line)
fw.close()

# resultat : (out-sans-doubles.txt)
# line1
# data1
# line4
# data2
# line6

#fin#
#%_1175

haut

Supprimer les lignes doubles UNIQ APPEND (exemple 2/3) #part1176


#!/usr/bin/python

#@_1176y_deleter_lignes_doubles_uniq_append!!read_splitlines_sort_set_forinifin_append_join_uniq
#deb#

# in.txt
# line818@making
# line818@makes
# line818@making
# line818@made
# line818@makes
# line818@making
# line818@made
# line818@makes
# line819@-
# line819@-
# line819@-


mylist2 = open ("in.txt").read().splitlines()        #openreadsplitlines#
fw = open("out.txt" , 'w')

# intercaler un tri
# mylist3 = sorted(list(set(mylist2)))      #set#        #sort#

uniq = []       #uniq#
[uniq.append(item) for item in mylist2 if item not in uniq]       #append#

str1 = '\n'.join(str(e) for e in uniq)

print str1
fw.write(str1)
fw.close()

# resultat
# line818@making
# line818@makes
# line818@made
# line819@-

#fin#
#%_1176

haut

Supprimer les lignes doubles RANGE LEN (exemple 3/3) #part1177


#!/usr/bin/python

#@_1177y_deleter_lignes_doubles_range_len!!readlines_range_len_break_tableau
#deb#

IN="in.txt"
OUT="out.txt"

# IN :
# vy-1150012.txt @ 6114 @ 1150012 @ 49 @ 1000
# vy-1150012.txt @ 6114 @ 1150012 @ 49 @ 1155
# vy-1150012.txt @ 6115 @ 1150012 @ 49 @ 1321
# vy-1150012.txt @ 6116 @ 1150012 @ 49 @ 1441
# vy-1150012.txt @ 6116 @ 1150012 @ 49 @ 1441
# ---- --- --- ---- --- ----- ----------
# vy-1150054.txt @ 6186 @ 1150054 @ 77 @ 1441
# vy-1150054.txt @ 6186 @ 1150054 @ 77 @ 1286
# vy-1150054.txt @ 6187 @ 1150054 @ 77 @ 1120
# vy-1150054.txt @ 6188 @ 1150054 @ 77 @ 1000
# vy-1150054.txt @ 6188 @ 1150054 @ 77 @ 1000
# ---- --- --- ---- --- ----- ----------
# end
# end

# utilisation d'1 tableau

fd32 = open (IN , 'r').readlines()       #openreadlines#
fd33 = open (OUT , "w")

for j in range(len(fd32)):      #rangelen#

    if fd32[j+1] == fd32[j]:
        fd32[j] == ""

    if fd32[j+1] != fd32[j]:
        print fd32[j].strip()
        fd33.write(fd32[j])

    if "end" in fd32[j+1]:
        break        #break#

fd33.close()

# resultat :

# vy-1150012.txt @ 6114 @ 1150012 @ 49 @ 1000
# vy-1150012.txt @ 6114 @ 1150012 @ 49 @ 1155
# vy-1150012.txt @ 6115 @ 1150012 @ 49 @ 1321
# vy-1150012.txt @ 6116 @ 1150012 @ 49 @ 1441
# ---- --- --- ---- --- ----- ----------
# vy-1150054.txt @ 6186 @ 1150054 @ 77 @ 1441
# vy-1150054.txt @ 6186 @ 1150054 @ 77 @ 1286
# vy-1150054.txt @ 6187 @ 1150054 @ 77 @ 1120
# vy-1150054.txt @ 6188 @ 1150054 @ 77 @ 1000
# ---- --- --- ---- --- ----- ----------

#fin#
#%_1177

haut

Occurrences uniques des lignes et renommer les sorties - WHILE LEN #part1178


#!/usr/bin/python

#@_1178y_occurrences_uniques_inoutplusieursfichiers!!forinlistdirif_read_splitlines_while_len_break_tableau
#deb#

import os

PATH="/Python27/tests-2018-04"

# in1.txt :
# data part3238
# data part3238
# data part3240
# data part3240
# data part3078
# data part3078

# in2.txt :
# data part973
# data part973
# data part985
# data part985
# data part997
# data part997


# ajouter 1 ligne a la fin de plusieurs fichiers

newlist1 = [i for i in os.listdir(PATH) if i[0:2] == "in"]      #forinlistdir#
#print newlist

for file1 in newlist1:

    open("z-" + file1 , 'w').write(open(file1).read() + "end\nend\n")      #openread#

################

# occurrences uniques des lignes ds plusieurs fichiers et renommer les sorties

newlist2 = [i for i in os.listdir(PATH) if i[0:4] == "z-in"]
#print newlist2

for file2 in newlist2:

    f1 = open(file2).read().splitlines()       #openreadsplitlines#
    f2 = open("y" + file2 , 'w')

    k=1
    while k < len(f1):        #while#     #len#

        item3 = f1[k]
        item4 = f1[k+1]

        if item4 != item3:
#            print item3
            f2.write(item3 + "\n")

        if "end" in item3:
            break        #break#
        k=k+1
f2.close()

# resultats :

# yz-in1.txt :
# data part3238
# data part3240
# data part3078

# yz-in2.txt :
# data part973
# data part985
# data part997

#fin#
#%_1178

haut

Occurrences uniques dans une colonne WHILE - POP #part1179


#!/usr/bin/python

#@_1179y_occurrences_uniques_dans_1_colonne!!readlines_while_len_pop_occur_uniq_tableau
#deb#

IN="in.txt"
OUT="out.txt"

# IN :
# abide
# abide
# abide
# accept
# accept
# access
# access
# access

# idem que uniq = deleter les mots doubles       #uniq#

a = open (IN).readlines()      #openreadlines#
b = open (OUT , 'w')

k=1
while k < len(a):         #while#     #len#
    if a[k] in a[k+1:]:
        a.pop(k)      #pop#
    else:
        print a[k].strip()
        road = a[k]
        b.write(road)
        k=k+1
b.close()

# resultat :
# abide
# accept
# access

#fin#
#%_1179

haut

Occurrences uniques dans une colonne avec WHILE - PASS et tableau #part1180


#!/usr/bin/python

#@_1180y_occurences_uniques_dans_1_colonne!!write_read_splitlines_while_len_tableau_split_pass_break_occur_uniq
#deb#

IN1="in1.txt"
IN2="in2.txt"
IN3="in3.txt"

# IN1 :
# earn  @@ to gain or be paid (money or other payment) in return for work or service
# earn  @@ (transitive)
# earn  @@ to acquire, merit, or deserve through behaviour or action
# edit  @@  (transitive)
# edit  @@ to prepare (text) for publication by checking and improving its accuracy, clarity, etc
# edit  @@ to be in charge of (a publication)
# elaborate  @@ planned or executed with care and exactness; detailed
# elaborate  @@ marked by complexity, ornateness, or detail


# ajouter des lignes au debut et a la fin du fichier (a cause des messages d'erreur)

open(IN2 , "w").write("@startstart @@\n" + open(IN1).read() + "@endend @@\n@endend @@\n")       #openread#

# break ---> quite la boucle en sautant 'else'
# continue ---> remonte au debut de la boucle
# pass ---> ne fait rien

# script : utilisation d'1 tableau
           idem que uniq = deleter les mots doubles       #uniq#

f30 = open (IN2).read().splitlines()       #openreadsplitlines#
f31 = open (IN3 , 'w')

k=1
while k < len(f30):         #while#     #len#
#    print k
    item1 = f30[k].split("@@")[0]       #split#
    item2 = f30[k+1].split("@@")[0]
#    print item1 , item2
    if item2 == item1:
        pass            # do nothing      #pass#
    if item2 != item1:
        print item1
        f31.write(item1 + "\n")
    if "endend" in item1:
        break        #break#
    k=k+1
f31.close()

# resultat :
# earn
# edit
# elaborate

#fin#
#%_1180

haut

Capturer les lignes doubles (ou triples) RANGE LEN #part1181


#!/usr/bin/python

#@_1181y_capture_lignes_doubles!!readlines_range_len_split_break
#deb#

# BUT : capturer les lignes doubles (et meme triples)

IN1="1-in1.txt"
IN2="1-in2.txt"
IN3="1-in3.txt"

# IN1 :
# part906  @_906
# part910  open (ROAD, 'r')
# part910  @_910
# part913  @_913
# part907  open("z.txt" , 'r')
# part907  @_907
# part908      open("2a-in-" + str(i) + ".txt" , 'r')
# part908      open("3a-out-" + str(i) + ".txt" , "r")
# part908  @_908
# part911  @_911


# ajouter lignes a la fin

open(IN2 , 'w').write(open(IN1).read() + "end\nend\n")         #openread#

########################

# capturer les lignes doubles

f1 = open (IN2).readlines()        #openreadlines#
f2 = open (IN3 , "w")

for i in range(len(f1)):      #rangelen#
    new1 = f1[i].split()[0]       #split#
    new2 = f1[i+1].split()[0]

    if new1 == new2:
        print new1
        f2.write(new1 + "\n")

    if "end" in new2:
        break        #break#
f2.close()

# resultat :
# part910
# part907
# part908
# part908     # ligne triple

#fin#
#%_1181

haut

Detecter les lignes doubles RANGE LEN #part1182


#!/usr/bin/python

#@_1182y_capturer_des_lignes_doubles!!read_splitlines_range_len_pass_join_startswith_break_listdir_path_getsize
#deb#

import os

IN1="in1.txt"
IN2="in2.txt"
IN3="in3.txt"
IN4="in4.txt"

PATH="/Python27/tests-2018-04"

# IN1 :
# 43
# 45
# 40
# 47
# 45


# trier  en conservant les doubles

f1 = open (IN1).read().splitlines()        #openreadsplitlines#
f2 = open(IN2 , 'w')

d = sorted(f1)        #sort#

print '\n'.join(d)
f2.write('\n'.join(d))
f2.close()

###################
################### detecter les lignes doubles

# ajouter lignes a la fin du fichier

open(IN3 , 'w').write(open(IN2).read() + "\nend\nend\n")       #openread#

######

# si ligne n+1 == n   ---> il y a 1 ligne double

f3 = open(IN3 , 'r').readlines()        #openreadlines#
f4 = open(IN4 , 'w')

for i in range(len(f3)):      #rangelen#
    elem1 = f3[i]
    elem2 = f3[i+1]
    if elem2 != elem1:
        pass      #pass#

    if elem2 == elem1:
        print "  ---> attention il y a 1 double : " , elem2
        w = "  ---> attention il y a 1 double : " , elem2
        w2 = ''.join(w)
        f4.write(w2)

    if elem2.startswith("end"):     #startswith#
        break        #break#
f4.close()

#######################

# tester si IN4 est vide

road = os.listdir(PATH)
for filey in road:
    if IN4 in filey:
        taille = os.path.getsize(filey)       #getsize#
        if taille == 0:
            print "     ---> ok il n'y a pas de ligne double"

# resultats :
# exemple 1 :
# 40
# 43
# 45
# 45
# 47
#   ---> attention il y a 1 double :  45

# exemple 2 :
# 40
# 43
# 45
# 47
#   ---> ok il n y a pas de ligne double

#fin#
#%_1182

haut

Deleter des elements d'une liste POP #part1183


#!/usr/bin/python

#@_1183y_deleter_elements_d'1_liste!!range_pop
#deb#

a = range(10)      #range#
print a

# resultat
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

###############

# supprimer dernier element d'1 liste

b = [1,14,25,31,47,55]
b = b[:-1]
print b

# resultat
# [1, 14, 25, 31, 47]

###############

# supprimer 1 element d'1 liste

print b.pop(3)      # 31      #pop#
print b           # [1, 14, 25, 47]

#fin#
#%_1183

haut

Trier un fichier en conservant les doubles avec SORTED #part1184


#!/usr/bin/python

#@_1184y_trier_fichier_avec_sorted!!read_splitlines_sort_reverse_True_join
#deb#

# in.txt
# 1087 data99
# 1178 data11
# 1005 data05


fd = open ("in.txt" , 'r')
fw = open ("out.txt" , 'w')

t = fd.read().splitlines()         #openreadsplitlines#

d = sorted(t)     # (t , reverse=True)     # tri en ordre inverse        #sort#

print '\n'.join(d)
fw.write('\n'.join(d))
fw.close()

# resultat :
# 1005 data05
# 1087 data99
# 1178 data11

#fin#
#%_1184

haut

Trier un fichier en deletant les doubles SORTED SET #part1185


#!/usr/bin/python

#@_1185y_trier_fichier_sorted_set!!readlines_sorted_set_replace
#deb#

# line0815   %   magnifies -magnifying -magnified --
# line0869   %   notices -noticing -noticed --
# line0868   %   notes -noting -noted --


f33 = open ("in.txt").readlines()       #openreadlines#
f34 = open("out.txt" , 'w')

for line33 in sorted(set(f33)):      #set#        #sort#
        line33 = line33.replace("line" , "").replace("-" , "")       #replace#
        print line33.strip()
        f34.write(line33)

f34.close()

# resultat :
# 0815   %   magnifies magnifying magnified
# 0868   %   notes noting noted
# 0869   %   notices noticing noticed

#fin#
#%_1185

haut

Trier un fichier en choisissant la colonne du tri SORT KEY #part1186


#!/usr/bin/python

#@_1186y_trier_fichier_avec_choix_colonne_du_tri!!forinopen_sort_key_split_lambda_reverse_True
#deb#

# fichier in.txt :
# 2779  ,  a
# 2  ,  a.m.
# 1  ,  aback
# 1  ,  ability
# 12  ,  able
# 1  ,  aboard
# 6  ,  abortion
# 326  ,  about
# 13  ,  above

newlist = [ligne.strip() for ligne in open ("in.txt")]          #forinopen#
#print newlist

fd = open ("out.txt", 'w')

newlist.sort(key = lambda ligne: int(ligne.split(',')[0]), reverse=True)       #split#        #sort#     #lambda#

for ligne in newlist:
    print ligne
    fd.write(ligne + "\n")
fd.close()

# resultat :
# 2779  ,  a
# 326  ,  about
# 13  ,  above
# 12  ,  able
# 6  ,  abortion
# 2  ,  a.m.
# 1  ,  aback
# 1  ,  ability
# 1  ,  aboard

#fin#
#%_1186

haut

Trier un fichier en conservant les doubles SORT KEY #part1187


#!/usr/bin/python

#@_1187y_trier_sur_1_colonne!!forinlistdir_sort_key_lambda_reverse_False
#deb#

import os
import re


PATH="/nfs/u/tests-2018-02"

newlist = [i for i in os.listdir(PATH) if i[0:2] == "c-"]      #forinlistdir#
#print newlist

fw = open ("1-liste-triee.txt", 'w')

newlist.sort(key = lambda line: line, reverse=False)        #sort#     #lambda#

for line in newlist:
    print line
    fw.write(line + "\n")
fw.close()

# resultat :
# c-abandon.txt
# c-allow.txt

#fin#
#%_1187

haut

Concatener plusieurs fichiers [var crochet] #part1188


#!/usr/bin/python

#@_1188y_concatener_inplusieursfichiers!!forinlistdirif_sort_replace_join
#deb#

import os

PATHHOME="/Python27/TESTS"
OUT="out.txt"

# uyz-1.txt :
# Missfire SP 2233 , 4365
# G3 disabled data1
# autofire SP 4512 , 5227
# disabled SP 2325
# ------------- SP

# uyz-2.txt :
# disabled data1 SP 4431
# autofire
# disabled SP 2325 SP 4512 , 5227
# Missfire SP 2233 , 4365

#######

newlist = [i for i in os.listdir(PATHHOME) if i[0:3] == "uyz"]      #forinlistdir#
#print newlist

newlist2 = sorted(newlist)         #sort#
fw = open ("out-uyz.txt" , "w")

for file1 in newlist2:
    var=[        #var=[#
    line1.replace("Missfire" , "").replace("disabled" , "").replace("autofire" , "")       #replace#
    for line1 in open(file1)                 #forinopen#
    if "SP" in line1 or "shot" in line1 or "Shot" in line1
    ] #finduvar#
    print ''.join(var)
    w1b = ''.join(var)
    fw.write(w1b)
fw.close()

# resultat :
# SP 2233 , 4365
# SP 4512 , 5227
# SP 2325
# ------------- SP
# data1 SP 4431
# SP 2325 SP 4512 , 5227
# SP 2233 , 4365

#fin#
#%_1188

haut

Concatener plusieurs fichiers avec SEARCH [var crochet] #part1189


#!/usr/bin/python

#@_1189y_concatener_lignes_inplusieursfichiers_avec_search!!forinlistdir_split_search_join_concat
#deb#

import re
import os
import glob

PATH="/Python27/tests-2018-06"

OUT="1-out.txt"
DELETE="comply"

# 5-abide.txt :
# to accept or submit to; suffer
# to comply (with)
# to withstand or sustain; endure

# 5-account.txt :
# money deposited at a bank
# they transferred their publicity account to a new agent
# to consider or reckon


##########

# deleter les anciens resultats

for currentFile in glob.glob('*'):
    e1 = "1-out";
    if currentFile.startswith(e1):
        os.remove(currentFile)

############

newlist = [i for i in os.listdir(PATH) if i[0:2] == "5-"]      #forinlistdir#
#print newlist

for file in newlist:
    file = file.strip()

    f3 = open(OUT , 'a')          # concatenation

    f1 = open(file , 'r')     #open#

    var=[i for i in f1 if not re.search(DELETE , i)]    #forinopen#       #var=[#      #search#
    #finduvar#
    w2 = ''.join(var)
    print w2
    f3.write(w2)
f3.close()

# resultat : concatenation ds 1 seul fichier :

# to accept or submit to; suffer
# to withstand or sustain; endure
# money deposited at a bank
# they transferred their publicity account to a new agent
# to consider or reckon

#fin#
#%_1189

haut

Concatener plusieurs fichiers [var crochet] (exemple complet) #part1190


#!/usr/bin/python

#@_1190y_concatener_inplusieursfichiers!!read_splitlines_forinlistdirif_sort_replace_split_search_isspace_startswith_any_inforin_join_count_chdir_concat
#deb#

import re
import os

PATH="/Python27/tests-2017-09"

GOODWORDS="1-good_words.txt"      # contenant patterns a capturer

# les fichiers out sont ds le script

#      3a-1.txt :
# 1001 5 data
#            # ligne vide
# 1002 12
# 1003 7 @ first capture 4
# 1006 3 @ first town 9
# line6
# newline7


#      3a-2.txt :                                           #    resultat ds 1 seul fichier :
# 1003 6 @ second capture 4                                 # start ---
# 1004 15 data                                              # first capture 4
#           # ligne vide                                    # first town 9
# 1005 9                                                    # end ---
# 1006 2 @ second town 7                                    # start ---
# 1012 8                                                    # second capture 4
# line7                                                     # second town 7
# newline8                                                  # end ---


# GOODWORDS :
# 1003
# 1006

################################
# BUT : concatener

good_words = open(GOODWORDS).read().splitlines()       #openreadsplitlines#

## activer soit mono soit multi fichiers - mais pas les 2 a la fois
#OUTONEFILE="2-result-one-file.txt"         # activer si ecriture ds mono fichier --> accumulation)
#fz1 = open(OUTONEFILE , 'w')            # activer si ecriture ds mono fichier --> accumulation)

newlist = [i for i in os.listdir(PATH) if i[0:2] == "3a"]      #forinlistdir#
#print newlist

# newlist = [i for i in os.listdir(PATH) if ("." in i and not "color" in i)] # pour retirer repertoires
# newlist = [i for i in os.listdir(PATH) if ("." in i
# and not "1-count.sh" in i)]               # possibilite de retourner a la ligne

newlist2 = sorted(newlist)        #sort#
#print newlist2      # ['3a-1.txt', '3a-2.txt']

count=1
for file1 in newlist2:
    fz1 = open("y-" + file1 , 'w')   # activer si ecriture ds multi fichiers (desactiver si mono fichier)
    os.chdir(PATH)                 # pour changer de repertoire  (avant w1)

    print "start ---"               # ecrit "start" au debut des fichiers y-
    fz1.write("start ---" + "\n")    # ecrit "start" au debut des fichiers y-

    var=[       #var=[#
    line1                    # INDISPENSABLE  -  activer si pas de 'replace' ni de 'split'
#    line1.replace("1003" , "toto").replace("1006" , "road").split(" @ ")        #replace#    #split#   # desactiver si pas utilise
    for line1 in open(file1)           # main line   # <--- INDISPENSABLE     #forinopen#
    if not re.search("data" , line1)      #search#
    if not line1.isspace()        #isspace#
    if not line1.startswith("l")     #startswith#
    if not "new" in line1
    if any(good_word in line1 for good_word in good_words)    #ifany#
    ] #finduvar#

    print ''.join(var)
#    print "@@@@## " , count , " @@@@ " , file1 , " @@@@\n" , ''.join(w1)      # habillage incluant le 'count'
    print "end ---"
    w1b = ''.join(var)     # adapter le '\n' avec le resultat (depend du split)
    fz1.write(w1b)
    fz1.write("end ---" + "\n")    # ecrit "end" a la fin des fichiers y-
    count+=1
fz1.close()

print "#######################"

print "---> resultat ds 1 seul fichier ds : OUTONEFILE"
print "oubien "
print "---> resultat ds multi fichiers de prefixes :   y-3a-.."

# resultat ds 1 seul fichier :
# start ---
# first capture 4
# first town 9
# end ---
# start ---
# second capture 4
# second town 7
# end ---

#fin#
#%_1190

haut

Concatener plusieurs fichiers avec 2 ouvertures FOR (methode tres simple) #part1191


#!/usr/bin/python

#@_1191y_concatener_inplusieursfichiers_avec_for!!forinlistdirif_join_forinopen_readlines
#deb#

import os

PATH="/Python27/tests-2017-09"

# 3-data1.txt :
# line1 data1
# line2 data1

# 3-data2.txt :
# line1 data2
# line2 data2

# 3-data3.txt :
# line1 data3
# line2 data3

newlist = [ word for word in os.listdir(PATH) if word[:2]=='3-' ]       #forinlistdir#
#print newlist

fw = open('out-concatene.txt' , 'w')
for IN in newlist:
    print ''.join([line for line in open(IN).readlines() if line.strip()])            #forinopenreadlines#
    fw.write(''.join([line for line in open(IN).readlines() if line.strip()]))
fw.close()

# resultat :
# line1 data1
# line2 data1
# line1 data2
# line2 data2
# line1 data3
# line2 data3

#fin#
#%_1191

haut

Concatener plusieurs fichiers avec WHILE sur fichiers et FOR sur lignes [var crochet] #part1192


#!/usr/bin/python

#@_1192y_concatener_avec_boucle_WHILE_sur_fichiers_FOR_sur_lignes!!while_forinopen_join
#deb#

# z-in1.txt
# line1 data1
# line2 data1

# z-in2.txt
# line1 data2
# line2 data2

fw = open("out.txt" , 'w')
i = 1
while i < 3:       #while#
    var=[j for j in open("z-in" + str(i) + ".txt").read().splitlines()]       #forinopenreadsplitlines#         #var=[#
    #finduvar#
    w2b = '\n'.join(var)
    print w2b
    fw.write(w2b + "\n")
    i += 1

# resultat :
# line1 data1
# line2 data1
# line1 data2
# line2 data2

#fin#
#%_1192

haut

Concatener avec : fichier2:readsplitlines + IFANY dans [var crochet] @ Capturer plusieurs patterns [var crochet] #part1193


#!/usr/bin/python

#@_1193y_capture_pattern_inplusieursfichiers!!read_splitlines_forinlistdirif_sort_any_inforin_join
#deb#

import os

PATHHOME="/Python27/tests-2017-10"


# 1-good-words.txt :
# disabled

# uyz-1.txt :
# Missfire SP 2233 , 4365
# G3 disabled data1
# autofire SP 4512 , 5227
# disabled SP 2325
# ------------- SP

# uyz-2.txt :
# disabled data1 SP 4431
# autofire
# disabled SP 2325 SP 4512 , 5227
# Missfire SP 2233 , 4365

good_words = open("1-good-words.txt").read().splitlines()      #openreadsplitlines#

newlist = [i for i in os.listdir(PATHHOME) if i[0:3] == "uyz"]      #forinlistdir#
#print newlist

newlist2 = sorted(newlist)        #sort#

fz1 = open("out.txt" , 'w')

for file1 in newlist2:
    var=[       #var=[#
    line1
    for line1 in open(file1)      #forinopen#
    if any(good_word in line1 for good_word in good_words)      #ifany#
#    if re.search(PATTERN , line1)        # PATTERN a definir      #search#
    ] #finduvar#

    w1b = ''.join(var)
    print w1b
    fz1.write(w1b)
fz1.close()

# resultat :
# G3 disabled data1
# disabled SP 2325
# disabled data1 SP 4431
# disabled SP 2325 SP 4512 , 5227

#fin#
#%_1193

haut

Concatener plusieurs fichiers avec READ #part1194


#!/usr/bin/python

#@_1194y_concatener_inplusieursfichiers_avec_read!!forinlistdirif_read
#deb#

import os

CONCAT="1-concat.txt"

PATH="/Python27/tests-2018-03"


# z-in1.txt :
# line1 data1
# line2 data1

# z-in2.txt :
# line1 data2
# line2 data2


##########

newlist = [j for j in os.listdir(PATH) if j[0:4] == "z-in"]      #forinlistdir#
#print newlist

filout = open (CONCAT, 'w')
for files in newlist:
    filin = open (files,'r')
#    with open(files) as filin:
    data = filin.read()        #openread#
    print data
    filout.write(data)

filout.close()

# resultat :
# line1 data1
# line2 data1
# line1 data2
# line2 data2

#fin#
#%_1194

haut

Concatener plusieurs fichiers avec 2 ouvertures FOR (methode tres simple) #part1195


#!/usr/bin/python

#@_1195y_concatener_2_fichiers!!withopen_read
#deb#

# 2 fichiers a concatener :
IN1="1-in1.txt"
IN2="1-in2.txt"

OUT="2-out.txt"

# IN1 :
# file1 data10 line1
# file1 data10 line2

#IN2 :
# file2 data20 line1
# file2 data20 line2

filenames = [IN1, IN2]
with open(OUT, 'w') as outfile:         #withopen#
    for fname in filenames:
        with open(fname) as infile:
            print infile.read()
            outfile.write(infile.read())
outfile.close()

# resultat :
# file1 data10 line1
# file1 data10 line2
# file2 data20 line1
# file2 data20 line2

#fin#
#%_1195

haut

Concatener plusieurs fichiers en supprimant leur derniere ligne #part1196


#!/usr/bin/python

#@_1196y_concatener_plusieurs_fichiers_en_supprimant_leur_derniere_ligne!!forinlistdir_read_splitlines_join
#deb#

import os
import glob

OUT="1-without-last-line.txt"

PATH="/nfs/u/tests-2018-03"

TIME=1

# file1.txt :
# line1 datafile1
# line2 datafile1

# file2.txt
# line1 datafile2
# line2 datafile2

######

# deleter anciens fichiers
for currentFile in glob.glob('*'):
    e1 = "1-without-last-line";
    if currentFile.startswith(e1):
        os.remove(currentFile)

######

# utilisation d'1 tableau

newlist = [i for i in os.listdir(PATH) if i[0:4] == "file"]      #forinlistdir#
#print newlist

for file in newlist:
    file = file.strip()
#    print file

    fw = open (OUT , "a")
    f1 = open(file , 'r')

    line1 = f1.read().splitlines()      #openreadsplitlines#
    w = line1[:-1]
    w2 = ''.join(w)
    print w2
    fw.write(w2 + "\n")
fw.close()

# resultat :
# line1 datafile1
# line1 datafile2

#fin#
#%_1196

haut

Concatener plusieurs fichiers avec WHILE pour fichiers et FOR pour lignes #part1197


#!/usr/bin/python

#@_1197y_concatener_inplusieursfichiers_avec_WHILE_et_FOR!!while_forinopenreadsplitlines_endswith_concat
#deb#

import os
import re

f2 = open ("out-3225-C.txt" , "w")
i = 1
while i < 4:         #while#      # ouverture de 3 fichiers
    for line in \
    [file for file in open("in" + str(i) + ".txt").read().splitlines()       #openreadsplitlines#
#    if file.endswith("py")      #endswith#
    ]:                            # attention au crochet fermant ]
        print line
        f2.write(line + "\n")
    i += 1
f2.close()

# resultat :
# data1 line1
# data1 line2
# data2 line1
# data2 line2
# data3 line1
# data3 line2

#fin#
#%_1197

haut

Concatener plusieurs fichiers avec WHILE 1 (+ nom des fichiers devant lignes) #part1198


#!/usr/bin/python

#@_1198y_concatener_inplusieursfichiers_while_1!!forlistdirif_while_readline_break_concat_split
#deb#

import os

PATH="/Python27/2018-01"

OUT="out2.txt"

# 3-file1.txt :
# line1 data1
# line2 data1

# 3-file2.txt :
# line1 data2
# line2 data2


newlist = [ word for word in os.listdir(PATH) if word[:2]=='3-' ]   # (= concatenation)      #forinlistdir#
#print newlist        # ['3-data1.txt', '3-data2.txt', '3-data3.txt']

fw2 = open(OUT , 'w')

for file in newlist:
    w = file.split("-")[1].split(".")[0]       #split#
    fd = open(file , 'r')     #open#

    while 1:       #while#
        line = fd.readline()     # lire lignes de tous les fichiers avec WHILE

        if not line:
            break        #break#

        print w , " @@ " , line.strip()
        road = w + " @@ " + line
        fw2.write(road)

fw2.close()

# resultat :
# file1 @@ line1 data1
# file1 @@ line2 data1
# file2 @@ line1 data2
# file2 @@ line2 data2

#fin#
#%_1198

haut

Concatener plusieurs fichiers avec nom et numeros devant lignes avec APPEND + WHILE #part1199


#!/usr/bin/python

#@_1199y_concatener_inplusieursfichiers_numeroter_lignes!!forinlistdir_startswith_append_readline_while_concat_count
#deb#

import os
import re

# BUT : creation d'1 liste de fichiers

PATTERN1="in"

PATHHOME="/Python27/tests-2017-10"

file_list = []         # liste de fichiers

f2 = open ("out.txt" , "w")

for file in \
[ doc for doc in os.listdir(PATHHOME)      #forinlistdir#

if doc.startswith(PATTERN1)     #startswith#
#or doc.startswith(PATTERN2)
]:                            # attention au crochet fermant ]

    file_list.append(file)    # creation de la liste de fichiers a concatener   #append#

for file in file_list:       # ouvre chaque fichier de la liste

    fr = open (file, 'r')
    line = fr.readline()        #openreadline#
    count = 1
    while line:          #while#    # boucle sur les lignes

        w = file , " : " , str(count) , " _ " , line.strip()   # reecrit une nouvelle syntaxe
        w2 = ''.join(w)
        print w2
        f2.write(w2 + "\n")     # ecriture de la concatenation
        line = fr.readline()
        count = count + 1

f2.close()

# resultat :
# in1.txt : 1 _ data1 line1
# in1.txt : 2 _ data1 line2
# in2.txt : 1 _ data2 line1
# in2.txt : 2 _ data2 line2
# in3.txt : 1 _ data3 line1
# in3.txt : 2 _ data3 line2

#fin#
#%_1199

haut

Concatener plusieurs fichiers avec nom et numeros devant lignes avec FOR + WHILE #part1200


#!/usr/bin/python

#@_1200y_concatener_inplusieursfichiers_avec_nom_et_numeros_devant_lignes!!forinlistdirif_readline_while_join_count_concat
#deb#

import os
import re

PATHHOME="/Python27/tests-2017-11"

# file1.txt :
# line1
# line2

# file2.txt :
# line1b
# line2b

f2 = open ("out.txt" , "w")

newlist = [i for i in os.listdir(PATHHOME) if i[0:4] == "file"]   # i[-4:] == ".txt"      #forinlistdir#
#print newlist

for road in newlist:
    fr = open (road, 'r')

    line = fr.readline()   # lecture ligne par ligne     #openreadline#
    count = 1
    while line:          #while#    # boucle sur les lignes

        w = road , " : " , str(count) , " _ " , line.strip()
        w2 = ''.join(w)
        print w2

        f2.write(w2 + "\n")     # ecriture de la concatenation
        line = fr.readline()   # important : repeter une 2eme fois la ligne
        count = count + 1
f2.close()

# resultat :
# file1.txt : 1 _ line1
# file1.txt : 2 _ line2
# file2.txt : 1 _ line1b
# file2.txt : 2 _ line2b

#fin#
#%_1200

haut

Concatener plusieurs fichiers avec FOR pour fichiers et WHILE pour lignes #part1201


#!/usr/bin/python

#@_1201y_concatener_inplusieursfichiers_en_changeant_syntaxe_avec_FOR_et_WHILE!!forinlistdirif_readline_while_join_concat_count
#deb#

import os
import re

PATHHOME="/Python27/data-2017-10"

f2 = open ("out.txt" , "w")

newlist = [i for i in os.listdir(PATHHOME) if i[0:2] == "in"]   # i[-4:] == ".txt"      #forinlistdir#
#print newlist

for file in newlist:
#    print file         # 3 fichiers

    fr = open (file, 'r')
    line = fr.readline()       #openreadline#
    count = 1
    while line:         #while#     # boucle sur les lignes

        w = file , " : " , str(count) , " _ " , line.strip()
        w2 = ''.join(w)
        print w2

        f2.write(w2 + "\n")
        line = fr.readline()
        count = count + 1

f2.close()

# resultat :
# in1.txt : 1 _ data1 line1
# in1.txt : 2 _ data1 line2
# in2.txt : 1 _ data2 line1
# in2.txt : 2 _ data2 line2
# in3.txt : 1 _ data3 line1
# in3.txt : 2 _ data3 line2

#fin#
#%_1201

haut

Concatener plusieurs fichiers avec nom et numeros devant lignes avec WHILE + WHILE #part1202


#!/usr/bin/python

#@_1202y_concatener_avec_WHILE_inplusieursfichiers!!while_forinlistdirif_readline_join_concat_count
#deb#

import os
import re

PATHHOME="/Python27/2017-10"

f2 = open ("out.txt" , "w")

i = 1
while count_file < 4:         #while#     # 1ere boucle WHILE sur les fichiers

    newlist = [file for file in os.listdir(PATHHOME) if file == ("in" + str(count_file) + ".txt")]      #forinlistdir#
    #print newlist

    for file in newlist:

        fr = open (file, 'r')
        line = fr.readline()       #openreadline#

        count_line = 1
        while line:              # 2eme boucle WHILE sur les lignes
            w = file , " : " , str(count_line) , " _ " , line.strip()
            w2 = ''.join(w)
            print w2
            f2.write(w2 + "\n")
            line = fr.readline()
            count_line = count_line + 1
    count_file += 1

f2.close()

# resultat :
# in1.txt : 1 _ data1 line1
# in1.txt : 2 _ data1 line2
# in2.txt : 1 _ data2 line1
# in2.txt : 2 _ data2 line2
# in3.txt : 1 _ data3 line1
# in3.txt : 2 _ data3 line2

#fin#
#%_1202

haut

Concatener plusieurs fichiers avec nom du fichier devant lignes avec SEARCH #part1203


#!/usr/bin/python

#@_1203y_concatener_avec_nom_fichier_devant_lignes_inplusieursfichiers!!forinlistdirif_split_search
#deb#

import os
import re

OUT="out.txt"

PATH="/Python27/tests-2018-02"

PATTERN="data"


# v-abide.txt :
# data1
# verb
# data2
# noun
# data3

# v-accept.txt :
# data1b
# verb
# data2b
# adj
# data3b


newlist = [i for i in os.listdir(PATH) if i[0:3] == "v-a"]         #forinlistdir#
#print newlist

fw = open(OUT , 'w')
for id18 in newlist:
    id18=id18.strip()

    id18b=id18.split("-")[1].split(".")[0]       #split#
    print ""
    for line18 in open(id18 , 'r'):      #open#
#        if re.search("{0}".format(PATTERN) , line18):
        if re.search(PATTERN , line18):      #search#

            print id18 , " : " , line18.strip()   # nom du fichier devant lignes
            fw.write(id18 + " : " + line18)

fw.close()

# resultat :
# v-abide.txt : data1
# v-abide.txt : data2
# v-abide.txt : data3
# v-accept.txt : data1b
# v-accept.txt : data2b
# v-accept.txt : data3b

#fin#
#%_1203

haut

Concatener plusieurs fichiers avec RANGE APPEND (methode simple) #part1204


#!/usr/bin/python

#@_1204y_concatener_inplusieursfichiers_avec_APPEND!!range_read_splitlines_append
#deb#

min=1
max=4

# 3-data1.txt :
# line1 data1
# line2 data1

# 3-data2.txt :
# line1 data2
# line2 data2

# 3-data3.txt :
# line1 data3
# line2 data3

all = []
fw = open("out.txt" , 'w')
for i in range(min , max):      #range#
#    mylist = open("3-data" + str(i) + ".txt").read().splitlines()       #openreadsplitlines#
    mylist = open("3-data" + str(i) + ".txt")        #open#
    for h in mylist:
        all.append(h)      #append#

for g in all:
    print g.strip()
#    fw.write(g + "\n")   # syntaxe avec : read().splitlines()
    fw.write(g)
fw.close()

# resultat :
# line1 data1
# line2 data1
# line1 data2
# line2 data2
# line1 data3
# line2 data3

#fin#
#%_1204

haut

Capturer plusieurs patterns dans plusieurs fichiers (out mono fichier) [var crochet] #part1205


#!/usr/bin/python

#@_1205y_capture_pattern_inplusieursfichiers!!read_splitlines_forinlistdirif_sort_any_inforin_join
#deb#

import os

PATHHOME="/Python27/tests-2017-10"


# 1-good-words.txt :
# disabled

# uyz-1.txt :
# Missfire SP 2233 , 4365
# G3 disabled data1
# autofire SP 4512 , 5227
# disabled SP 2325
# ------------- SP

# uyz-2.txt :
# disabled data1 SP 4431
# autofire
# disabled SP 2325 SP 4512 , 5227
# Missfire SP 2233 , 4365

good_words = open("1-good-words.txt").read().splitlines()      #openreadsplitlines#

newlist = [i for i in os.listdir(PATHHOME) if i[0:3] == "uyz"]      #forinlistdir#
#print newlist

newlist2 = sorted(newlist)        #sort#

fz1 = open("out.txt" , 'w')

for file1 in newlist2:
    var=[       #var=[#
    line1
    for line1 in open(file1)      #forinopen#
    if any(good_word in line1 for good_word in good_words)      #ifany#
#    if re.search(PATTERN , line1)        # PATTERN a definir      #search#
    ] #finduvar#

    w1b = ''.join(var)
    print w1b
    fz1.write(w1b)
fz1.close()

# resultat :
# G3 disabled data1
# disabled SP 2325
# disabled data1 SP 4431
# disabled SP 2325 SP 4512 , 5227

#fin#
#%_1205

haut

Capturer des paragraphes si la colonne 1 change avec WHILE 1 #part1206


#!/usr/bin/python

#@_1206y_paragraphes_crees_si_colonne_1_change_outplusieursfichiers!!readline_while_break_startswith
#deb#

import os
import glob

LETTRE="1-alpha.txt"

IN1="1-in1.txt"
IN2="1-in2.txt"

# LETTRE :
# a
# b


# deleter anciens resultats

for currentFile in glob.glob('*'):
    e1 = "zzz-";
    if currentFile.startswith(e1):
        os.remove(currentFile)

#########

# ajouter 1 ligne vide en ligne 1

open (IN2 , "w").write (" \n" + open (IN1).read())       #openread#

##############################

# creation d'autant de fichiers que de lettres : a b ..

f1 = open(LETTRE , 'r')        #open#
for lettre in f1:
    lettre = lettre.strip()
#    print lettre

    f2 = open(IN2 , 'r')
    line2 = f2.readline()       #openreadline#

    while 1:       #while#
        line2 = f2.readline()
        line2 = line2.rstrip()

        if not line2:
            break        #break#

        if line2.startswith(lettre):     #startswith#
            o = open("zzz-" + lettre + ".txt" , 'a')
            print line2
            print >> o , line2
o.close()

# resultats : creation de 2 fichiers
# zzz-a.txt :
# allowed
# abduct
# abducted

# zzz-b.txt :
# behaviour
# behind
# being

#fin#
#%_1206

haut

Capturer des paragraphes si la colonne 1 change avec FOR #part1207


#!/usr/bin/python

#@_1207y_paragraphes_crees_si_col1_change_outplusieursfichiers!!readlines_split_>>
#deb#

import os
import glob
import time

IN="in.txt"

TIME=2


# IN :
# aboard  :  rigging aboard a vessel
# aboard  :  running rigging aboard a
# about  :  deciding about something
# about  :  wrapping about the body
# across  :  being situated across
# across  :  lying across a boat

#########

# deleter anciens resultats  (fichiers dont le nom commence par : prepo_)

for currentFile in glob.glob('*'):
    e1 = "prepo_";
    if currentFile.startswith(e1):
        os.remove(currentFile)

print "ok fichiers deletes"
time.sleep(TIME)

#########

# chaque prepo est ecrit ds 1 fichier different
# creation de fichiers : 1 prepo = 1 fichier

f = open (IN , 'r')
for line in f.readlines():       #openreadlines#

    x = line.split(" ")[0]       #split#
    o = open ("prepo_" + x + ".txt" , "a")

    print >> o , line.strip()

f.close()

print "ok 3 fichiers crees"
time.sleep(TIME)

# resultat : 3 fichiers :
# prepo_aboard.txt
# aboard  :  rigging aboard a vessel
# aboard  :  running rigging aboard a

# prepo_about.txt
# about  :  deciding about something
# about  :  wrapping about the body

# prepo_across.txt
# across  :  being situated across
# across  :  lying across a boat

#fin#
#%_1207

haut

Capturer des paragraphes avec pattern debut et fin #part1208


#!/usr/bin/python

#@_1208y_paragraphes_avec_pattern_debut_et_fin!!forinopen_flag_chdir_forinlistdir
#deb#

import os

# BUT : creer des paragraphes (= fichiers)
#       avec ecriture du pattern de START mais pas celui de END

# debut-fin des paragraphes :

START1="verb"
END1="noun"
END2="adj"

PATH="/nfs/u/tests-2018-02"


# v-abide.txt :
# data1
# verb
# data2
# line4
# noun
# data3

# v-accept.txt :
# data1b
# verb
# data2b
# line4b
# line5b
# adj
# data3


################################

newlist = [i for i in os.listdir(PATH) if i[0:3] == "v-a"]      #forinlistdir#
#print newlist

for id18 in newlist:
    id18=id18.strip()

    id18b=id18.split("-")[1].split(".")[0]       #split#
    print ""

    flag=0      #flag#
#    for line in open (PATHZ + id18b):
    for line in open (id18):        #forinopen#
        line=line.strip()
        if START1 in line:
            o = open ("w-" + id18b + ".txt" , "a")
            flag=1
        if END1 in line or END2 in line:
            flag=0

        if flag == 1:
            print line
            print >> o , line

# resultat :

# w-abide.txt
# verb
# data2
# line4

# w-accept.txt
# verb
# data2b
# line4b
# line5b

#fin#
#%_1208

haut

Capturer des paragraphes avec pattern de debut et de fin #part1209


#!/usr/bin/python

#@_1209y_creer_paragraphes_avec_pattern_de_debut_et_de_fin!!forinopen_startswith_split_flag_count
#deb#

# contenu de in.txt
debug: on
allowed connection: 3
tr#45
Starting: interface 78e23
Fan Status: On
Speed: -
sl no: 3431212-2323-90
vendor: aledaia
Stopping: interface 78e23
tr#90
newdebug received
Starting: interface 78e24
Fan Status: Off
Speed: 5670
sl no: 3431212-2323-90
vendor: aledaia
Stopping: interface 78e24
Starting: interface 68e73
Fan Status: On
Speed: 1200
sl no: 3431212-2323-90
vendor: aledaia
Stopping: interface 68e73
tr#99

flag=0;count=0      #flag#
for line in open ("in.txt"):         #forinopen#
    line=line.strip()
    if line.startswith("Stopping"):     #startswith#
        flag=0
        o.close()
    if line.startswith("Starting"):
        interface=line.split(" ")[2]       #split#
        flag=1;count=count+1
        o = open (interface+"_"+str(count)+".txt","w")
    if flag and not line.startswith("Starting"):
        print >> o, line

# resultat
# contenu de : 78e23_1.txt
Fan Status: On
Speed: -
sl no: 3431212-2323-90
vendor: aledaia

# contenu de : 78e24_2.txt
Fan Status: Off
Speed: 5670
sl no: 3431212-2323-90
vendor: aledaia

# contenu de : 68e73_3.txt
Fan Status: On
Speed: 1200
sl no: 3431212-2323-90
vendor: aledaia

#fin#
#%_1209

haut

Capturer des paragraphes avec pattern de debut et de fin et rstrip #part1210


#!/usr/bin/python

#@_1210y_creer_paragraphe!!startswith_flag_rstrip_split_count
#deb#

IN="in.txt"

# IN :
# !@_file1
# format de out conserve
# choisir une des 2 lignes
# %
# !@_file2
# trier la liste
# apres une capture de paragraphes
# %


BEGIN="!@"
END="%"

flag=0 ; count=0      #flag#
for line in open (IN):       #forinopen#

    line=line.rstrip()     # format conserve avec rstrip()

    if line.startswith(BEGIN):     #startswith#
        interface=line.split(BEGIN)[1]       #split#
        flag=1;count=count+1
        o = open ("y" + str(count) + interface + ".txt" ,"w")

    if line.startswith(END):
        print line
        print >> o, line
        flag=0

    if flag == 1:
        print line
        print >> o, line

# resultat : creation de 2 fichiers :
# y1_file1.txt :
# !@ file1
# format de out conserve
# choisir une des 2 lignes
# %

# y2_file2.txt :
# !@ file2
# trier la liste
# apres une capture de paragraphes
# %

#fin#
#%_1210

haut

Capturer des paragraphes avec pattern de debut et de fin WHILE + WHILE inout monofichier #part1211


#!/usr/bin/python

#@_1211y_paragraphes_avec_pattern_debut_et_fin__inout_mono_fichier!!while1_readline_break_join
#deb#

import time

# BUT : complet ; in et out sont tous les deux en 1 seul fichier
# avantages :
#      ecriture de la ligne 1 (ou non)
#      warning si pas de END avant 1 nouveau START

START="verb"
END="noun"

IN="x-parag.txt"
OUT="x-out.txt"

TIME=0.2

#IN :
# data1
# verb noun
# data2
# verb
# line4
# noun
# data3
# symbole
# data1b
# verb
# data2b
# line4b
# line5b
# noun
# data3
# symbole



count=1
#f8 = open(STAT5  , 'r')
#f9 = open(OUT1 , 'w')          # OUT1="1-0-RESULT-PARAGRA-START-END1.txt"
f1 = open(IN  , 'r')
f2 = open(OUT , 'w')


while 1:       #while#
    line = f1.readline()      #openreadline#

    if not line:
        break        #break#

    if START in line and END in line:
        print line.strip()
        print "----------"
        f2.write(line)
        f2.write("-------------------------------------\n")

    if START in line and not END in line:
        print line.strip()
        f2.write(line)
        while 1:
            line = f1.readline()

            if not line:
                break

            if not END in line and not START in line:
                print line.strip()
                f2.write(line)

            if not END in line and START in line:
                print "il y a 2 START"
                w3 = line.strip() , "################################# il y a 2 START sans END entre"
                w3b = ''.join(w3)
                f2.write(w3b + "\n")
                f2.write("-------------------------------------\n")
                break

            if not END in line and "#%_1211 line:
#                print "-------------------------------------"
                w4 = line.strip() , "################################# ne pas prendre ce part"
                w4b = ''.join(w4)
                f2.write(w4b + "\n")
                f2.write("-------------------------------------\n\n")
                break

            if END in line:
                print line.strip()
                print "-------------------------------------"
#                print count
                count+=1
                f2.write(line)
                f2.write("-------------------------------------\n")
                break
                time.sleep(TIME)

f2.close()

# resultat : 1 seul fichier :
# verb noun
# -------------------------
# verb
# line4
# noun
# -------------------------
# verb
# data2b
# line4b
# line5b
# noun
# -------------------------

#fin#
#%_1211

haut

Capturer des paragraphes avec choix sur le pattern de debut #part1212


#!/usr/bin/python

#@_1212y_creer_paragraphes!!readlines_startswith_flag_count
#deb#

# idlist.txt:
# ID

# details.txt:
# #ID10
# data:A
# class:9
# year:1985
# status=nill
# #WD12
# data:B
# class:X
# year:1987
# #ID20
# data:X
# class:X
# year:1983
# #ID90
# data:Y
# class:9
# year:1984
# #ID55
# data:Z
# class:X
# year:1985


# script
flag = 0 ; count = 0      #flag#

idlist = open ("idlist.txt").readlines()       #openreadlines#
idlist = [i.strip() for i in idlist]
detailslist = open ("details.txt").readlines()

for id in idlist:
    for lines in detailslist:
        lines=lines.strip()
        if lines.startswith("#") and id in lines:     #startswith#
            interface = lines
            flag = 1 ; count = count + 1
            fp = open (str(count) + "_" + interface  + ".txt" , "w")
        if lines.startswith("#") and not id in lines:
            flag = 0
        if flag:
            print  >> fp , lines

# resultat : creation de 4 fichiers

# 1_#ID10.txt
#ID10
# data:A
# class:9
# year:1985
# status=nill

# 2_#ID20.txt
#ID20
# data:X
# class:X
# year:1983

# 3_#ID90.txt
#ID90
# data:Y
# class:9
# year:1984

# 4_#ID55.txt
#ID55
# data:Z
# class:X
# year:1985

#fin#
#%_1212

haut

Capturer des paragraphes avec seulement un pattern de debut #part1213


#!/usr/bin/python

#@_1213y_creer_paragraphes_avec_seulement_1_pattern_de_debut!!forinopen_split_flag-count
#deb#

START="1-extract.txt"

# START :
#C 1 CLIENT TOTAL
#File 1
#Data 1
#Data 2
#C 1 CLIENT TOTAL
#File 2
#Data 3
#Data 4
#C 1 CLIENT TOTAL
#File 3
#Data 5
#Data 6

# C 1 CLIENT TOTAL
# ^ ^    ^
# 0 1    2       # champs du split

flag=0      #flag#
count=1
for line in open (START):      #forinopen#
    line=line.strip()
    if "C 1" in line:
        road = line.split(" ")[2] + str(count)       #split#
        o = open (road , "w")
        flag = 1
        count = count + 1
    if flag:
        print >> o , line

# resultat :
# creation de 3 fichiers :
#CLIENT1
#CLIENT2
#CLIENT3

#fin#
#%_1213

haut

Deleter le repertoire s'il existe ; creer le repertoire s'il n'existe pas ; changer de repertoire #part1214


#!/usr/bin/python

#@_1214y_creer_repertoire_et_deleter_repertoire!!exists_shutil_rmtree_makedirs
#deb#

import os
import shutil
import time

# changer de repertoire : os.chdir(PATHZ)

PATHZ="/Python27/0-0-7-FROM-SITE/Z-"

# deleter le repertoire s'il existe

# check if folder exists
if os.path.exists(PATHZ):        #pathexists#
# remove if exists
    shutil.rmtree(PATHZ)  # will delete a directory and all its contents

print " --> ok repertoire  PATHZ  delete"
time.sleep(TIME)

######

# creer le repertoire s'il n'exite pas

if not os.path.exists(PATHZ):
    os.makedirs(PATHZ)

print " --> ok repertoire  PATHZ  cree"
time.sleep(TIME)

#fin#
#%_1214

haut

Chaque ligne est en STRING (en 8 scripts) #part1215


#!/usr/bin/python

#@_1215y_lire_chaque_ligne_est_en_STRING!!while_readline_break_read_splitlines_forinopen_rstrip_input
#deb#

# 1/8_ lire chaque ligne est en STRING

IN="in.txt"
fd = open (IN , 'r')      #open#
while 1:       #while#
    line = fd.readline()
    if not line:
        break        #break#

    print line.strip() , type(line)
fd.close()

# she plans to attempt the record again <type 'str'>
# his aim is to get rich <type 'str'>

###################################

# 2/8_ lire chaque ligne est en STRING

mylist = open (IN).read().splitlines()       #openreadsplitlines#
for i in mylist:
    print i , type(line)

# she plans to attempt the record again <type 'str'>
# his aim is to get rich <type 'str'>

###################################

# 3/8_ lire chaque ligne est en STRING

with open(IN , 'r') as f:        #withopen#
    for i in f:
        print i.strip() , type(line)
f.close()

# she plans to attempt the record again <type 'str'>
# his aim is to get rich <type 'str'>

###################################

# 4/8_ lire chaque ligne est en STRING

newlist = [line.strip() for line in open (IN)]       #forinopen#
#print newlist

for line in newlist:
    print line , type(line)

# she plans to attempt the record again <type 'str'>
# his aim is to get rich <type 'str'>

###################################

# 5/8_ lire chaque ligne est en  STRING

f = open (IN,"r")
for line in f.readlines():       #openreadlines#
    line = str(line).rstrip()
    print line , type(line)

f.close()

# she plans to attempt the record again <type 'str'>
# his aim is to get rich <type 'str'>

###################################

# 6/8_ lire chaque ligne est en STRING

f = open (IN,"r")
line = f.readline()      #openreadline#
while line:
    print line.strip() , type(line)
    line = f.readline()

f.close()

# she plans to attempt the record again <type 'str'>
# his aim is to get rich <type 'str'>

###################################

# 7/8_ lire chaque ligne est en STRING

with open (IN , "r") as f:
    my_list = f.read().splitlines()       #withopenreadsplitlines#
#    print my_list , type(my_list)    # <type 'list'>

    for line in my_list:
        print line , type(line)

f.close()

# she plans to attempt the record again <type 'str'>
# his aim is to get rich <type 'str'>

###################################

# 8/8_ lire chaque ligne est en STRING

import fileinput

# the fileinput.input() call reads lines sequentially,
# but doesn't keep them in memory after they've been read.

for line in fileinput.input([IN]):
    print line.strip() , type(line)

# she plans to attempt the record again <type 'str'>
# his aim is to get rich <type 'str'>

#fin#
#%_1215

haut

Chaque mot est en STRING #part1216


#!/usr/bin/python

#@_1216y_lire_chaque_mot_est_en_STRING!!split_read
#deb#

IN="in.txt"

#  lire fichier decoupe 1 mot par ligne

with open(IN , 'r') as f:      #withopen#
    for line in f:
        for word in line.split():       #split#
            print word , type(word)
f.close()

# she <type 'str'>
# plans <type 'str'>
# to <type 'str'>
# attempt <type 'str'>
# the <type 'str'>
# record <type 'str'>
# again <type 'str'>
# his <type 'str'>
# aim <type 'str'>
# is <type 'str'>
# to <type 'str'>
# get <type 'str'>
# rich <type 'str'>

#####################################

# lire fichier decoupe 1 mot par ligne

f = open (IN , 'r')
for word in f.read().split():       #openreadsplit#
    print word , type(word)

# she <type 'str'>
# plans <type 'str'>
# to <type 'str'>
# attempt <type 'str'>
# the <type 'str'>
# record <type 'str'>
# again <type 'str'>
# his <type 'str'>
# aim <type 'str'>
# is <type 'str'>
# to <type 'str'>
# get <type 'str'>
# rich <type 'str'>

#fin#
#%_1216

haut

Une STRING contient toutes les lignes #part1217


#!/usr/bin/python

#@_1217y_lire_tout_le_fichier_en_une_STRING!!read
#deb#

IN="in.txt"

fd = open (IN , 'r')
t = fd.read()        #openread#
print t , type(t)

# she plans to attempt the record again
# his aim is to get rich      <type 'str'>

#fin#
#%_1217

haut

Une STRING contient tous les mots [var crochet] #part1218


#!/usr/bin/python

#@_1218y_string_contient_tous_les_mots !!forinopenforin_split_join
#deb#

IN="in.txt"

var=[word for line in open (IN , 'r') for word in line.split()]       #forinopen#         #var=[#       #split#
#finduvar#
w2 = '\n'.join(var)
print w2 , type(w2)

# resultat :
# she
# plans
# to
# attempt
# the
# record
# again
# his
# aim
# is
# to
# get
# rich   <type 'str'>

#fin#
#%_1218

haut

Chaque ligne est en LISTE #part1219


#!/usr/bin/python

#@_1219y_lire_chaque_ligne_est_en_LISTE!!forinopen_split
#deb#

IN="in.txt"

for line in open (IN):      #forinopen#
    line = line.split()       #split#
    print line , type(line)

# ['she', 'plans', 'to', 'attempt', 'the', 'record', 'again']  <type 'list'>
# ['his', 'aim', 'is', 'to', 'get', 'rich']  <type 'list'>

#fin#
#%_1219

haut

Une LISTE contient toutes les lignes #part1220


#!/usr/bin/python

#@_1220y_lire_une_LISTE_contenant_toutes_les_lignes!!read_splitlines_forinopen
#deb#

IN="in.txt"

# lire une LISTE contenant toutes les lignes (exemple 1)

mylist = open (IN).read().splitlines()      #openreadsplitlines#
print mylist , type(mylist)

# ['she plans to attempt the record again', 'his aim is to get rich'] <type 'list'>

#################################

# lire une LISTE contenant toutes les lignes (exemple 2)

newlist = [line.strip() for line in open (IN)]      #forinopen#
#print newlist

# ['she plans to attempt the record again', 'his aim is to get rich'] <type 'list'>

#fin#
#%_1220

haut

Une LISTE contient tous les mots [var crochet] #part1221


#!/usr/bin/python

#@_1221y_lire_une_LISTE_contenant_tous_les_mots!!forinopenforin_split_read_string
#deb#

import string

IN="in.txt"

# 1/2_ tout le fichier est dans une LISTE

var=[word for line in open (IN , 'r') for word in line.split()]       #forinopen#        #var=[#       #split#
#finduvar#
print var , type(var)

# ['she', 'plans', 'to', 'attempt', 'the', 'record', 'again', 'his', 'aim', 'is', 'to', 'get', 'rich']    <type 'list'>

#####################################

# 2/2_ tout le fichier est dans une LISTE

f = open (IN)
t = f.read()      #openread#
print string.split(t) , type(string.split(t))      #string#

# ['she', 'plans', 'to', 'attempt', 'the', 'record', 'again', 'his', 'aim', 'is', 'to', 'get', 'rich']  <type 'list'>

#fin#
#%_1221

haut

XARGS : Rechercher un pattern avec READ et NAME #part1222


#!/usr/bin/python

#@_1222y_xargs_capture_syntaxe_dans_fichiers!!glob_read_name_xargs
#deb#

import glob

# idem que xargs

pattern="activate"

for files in glob.glob( "*.txt" ):
    f = open( files, 'r' )
    file_contents = f.read()        #openread#
    if pattern in file_contents:
            print f.name      #name#     # donne le nom du fichier
f.close()

# resultat :
# 1-0-data-crochets-LAR-2.txt
# 1-0-data-crochets-LAR-3.txt
# 1-0-data-crochets-LAR-4.txt
# 1-0-data-larousse-10-26-converti.txt

#fin#
#%_1222

haut

XARGS : Rechercher un pattern (depart = liste de fichiers) regex [var crochet] (exemple 1) #part1223


#!/usr/bin/python

#@_1223y_xargs_chercher_pattern_dans_fichier_de_path!!rawinput_forinlistdirif_split_forinopenif_endswith_any_regex_join_xargs
#deb#

import os

# script concu pour traiter des path de fichiers  (pas des fichiers avec data concatenes) :
# exemple :    /Python27/tests-2018-06/0-3019.py

# DEPART : provient d'1 scan deja fait dans 1 repertoire :
IN1="1-in1.txt"        # <--- DEPART

# out
OUT="1-out.txt"

LIMITMIN="020"      # nb lignes des fichiers
LIMITMAX="065"

# RAWINPUT reel :
RAWINPUT = raw_input("entrer un mot cle : ")      # ici : join       #rawinput#

print RAWINPUT         # ici : join

regex = ["905" , "921"]      # exclusion        #regex#

EXTENSION=".txt"

PATHHOME="/Python27/tests-2018-06"

# IN1 :
# 015 /Python27/tests-2018-06/1-file.py
# 038 /Python27/tests-2018-06/1-919.py
# 039 /Python27/tests-2018-06/1-905.txt
# 045 /Python27/tests-2018-06/1-920.txt
# 062 /Python27/tests-2018-06/1-909.txt
# 073 /Python27/tests-2018-06/1-921.txt

# contenu de : 1-920.txt :
# join

####################

# utilisation de xargs

f1 = open(IN1 , 'r')
f2 = open(OUT , 'w')

newlist = [i for i in os.listdir(PATHHOME) if i == IN1]      #forinlistdir#
#print newlist

for file in newlist:

    var=[         #var=[#
    line
    for line in open(file)           #forinopen#
    if line.split()[-1].endswith(EXTENSION)         #endswith#       #split#

    if line.split()[0] > LIMITMIN and line.split()[0] < LIMITMAX       # selection du nb lignes

    if not any(j in line.split()[0] for j in regex)      #ifany#
    ]
    #finduvar#
    print ""
    for k in var:        # ouverture des lignes
        k = k.strip()

        k2 = k.split()[1]
        k01 = k.split()[0] , k.split()[1]
        k01b = ' '.join(k01)

        f5 = open(k2 , 'r')      #open#
        for line5 in f5:
            line5 = line5.strip()
            if RAWINPUT in line5:
                print k01b , " .. " , line5
                k3 = k01b , " .. " , line5
                k4 = ''.join(k3)
                f2.write(k4 + "\n")
f2.close()

# resultat :

# 045 /Python27/tests-2018-06/1-920.txt  ..  join

#fin#
#%_1223

haut

XARGS : Rechercher un pattern dans plusieurs fichiers [var crochet] (exemple 2) #part1224


#!/usr/bin/python

#@_1224y_xargs_chercher_pattern_ds_inplusieursfichiers!!rawinput_forinlistdirif_sort_chdir_forinopenif_count_join_xargs
#deb#

import os
import re

PATH="/Python27/tests-2017-09"

PATTERN = raw_input("Rechercher un pattern ds le repertoire PATH : ")      #rawinput#

##########
# idem que xargs

newlist = [i for i in os.listdir(PATH) if ("." in i and not "color" in i)]      #forinlistdir#
#print newlist

newlist2 = sorted(newlist)        #sort#

count=1
for file1 in newlist2:
    os.chdir(PATH)

    var=[        #var=[#
    line1
    for line1 in open(file1)        #forinopen#
    if PATTERN in line1
    ] #finduvar#

    print "@@@@### " , count , " @@@@ " , file1 , " @@@@\n" , ''.join(var)
    count+=1

print "resultat de la recherche de : " , PATTERN , "   ; ds le rep : " , PATH

# resultat :
# @@@@###  247  @@@@  x-part.....txt  @@@@
# .... # 1          @  % Print last instance of file
# .... # @  % Print last instance of file - @!! 1

#fin#
#%_1224

haut

XARGS : Rechercher une syntaxe dans des sous repertoires WALK (exemple 3) #part1225


#!/usr/bin/python

#@_1225y_xargs_capture_syntaxe_dans_fichiers_directories_sub-directories!!walk_path_join
#deb#

import os

# BUT : rechercher une syntaxe dans des fichiers pouvant etre dans des directories et sub directories
# idem que  xargs

SEARCH="activate"    # syntaxe recherchee

PATH="/Python27"

for dname, dirs, files in os.walk(PATH):       #walk#
    for fname in files:
        fpath = os.path.join(dname, fname)
        with open(fpath) as f:      #withopen#     # ouverture en str
            for line in f:
                # retirer de la recherche des fichiers mal formates
                if not "1-ascii-1.txt" in fpath and not "1-ascii-6.txt" in fpath:
                    if SEARCH in line:
                        print fpath , " @@@ " , line.strip()

# resultat :
# /Python27\0-0-1-LAROUSSE\1-0-data-crochets-LAR-2.txt  @@@  @ activate
# /Python27\0-0-1-LAROUSSE\1-0-data-crochets-LAR-2.txt  @@@  3.  ( become activated  bomb )     exploser
# /Python27\0-0-1-LAROUSSE\1-0-data-crochets-LAR-2.txt  @@@  7.  ( activate  switch ,  lever ,  clutch )     actionner
# /Python27\0-0-1-LAROUSSE\1-0-data-crochets-LAR-3.txt  @@@  @ activate

#fin#
#%_1225

haut

XARGS : Rechercher une syntaxe dans des sous repertoires (exemple complet 1) #part1226


#!/usr/bin/python

#@_1226y_xargs_chercher_pattern_ds_1_fichier_avec ANY!!rawinput_split_endswith_any_inforin_join_len_readlines_continue_regex_xargs
#deb#

import os
import re

# fichier entree : IN   provient d'1 scan du script suivant :
#      /nfs/u/0-0-05-SCAN-SEARCH-XARGS/0-0-001-A-SCAN-NB-LINES-XARGS.py

# 091956 /nfs/u/0-0-04-WR-FRdef/1-0-WR-FR-8-21.txt 85071
# 076392 /nfs/u/tests-2017-10/result-with-out-yield.txt 84229
# ^^col1    ^^col2                                      ^^col3


IN="1-0-result.txt"       # <--- fichier entree

NEW1="1-file1.txt"
NEW2="1-file2.txt"

LIMITMIN="000010"      # limite du nombre de lignes
LIMITMAX="001000"

PATTERN = raw_input("entrer un mot cle : ")      #rawinput#

print PATTERN

regex = ["old" , "OLD"]       #regex#

EXTENSION=".py"

############

# habillage : capture colonnes 1 et 2

fw1 = open(NEW1 , 'w')
f1 = open(IN , 'r')        #open#

for line1 in f1:
    col0 = line1.split()[0]       #split#
    col1 = line1.split()[1]

    if col0 > LIMITMIN and col0 < LIMITMAX:
        z = col1
        z2 = ''.join(z)
        fw1.write(z2 + "\n")

fw1.close()

##############

# idem que xargs
# capture du PATTERN dans les lignes des fichiers
# utilisation de ANY pour selectionner des fichiers

f3 = open(NEW2 , 'w')
f4 = open(NEW1 , 'r')
for file4 in f4:
    file4 = file4.strip()
    if file4.endswith(EXTENSION) and not any(i in file4 for i in regex):    #ifany#   #endswith#

        f5 = open(file4 , 'r')
        for line5 in f5:
            if PATTERN in line5:
                z = file4 , " @@@ " , line5
                z2 = ''.join(z)
                print z2.strip()
                f3.write(z2)
            if not PATTERN in line5:
                continue        #continue#
f3.close()

##########################

# nb lignes de NEW2

print "----------"
nblines = len(open(NEW2 , 'r').readlines())      #openreadlines#       #len#
print "pattern = " , PATTERN , " ;  nb lignes du fichier : " , NEW2 , " : " , nblines

#fin#
#%_1226

haut

XARGS : Rechercher une syntaxe dans des sous repertoires - WALK [var crochet] (exemple complet 2) #part1227


#!/usr/bin/python

#@_1227y_xargs_capture_syntaxe_dans_fichiers_directories_sub-directories!!rawinput_read_splitlines_walk_len_readlines_any_inforin_join_count_xargs
#deb#

import os
import re

PATH="/Python27/2017-09"

# idem que  xargs

# verifier le contenu de ce fichier : (par defaut fichier vide)
BADWORDS="1-bad_words.txt"      #contenant des patterns a supprimer dans les lignes

GOODWORDS="1-0-good_words.txt"     # contenant des patterns a capturer

OUT="1-0-result-choix-nb-lines.txt"

PATTERNFPATH = raw_input("1/4_Rechercher un pattern ds les fpath (noms des fichiers) - (exemple : py) : ")      #rawinput#
NOTPATTERNFPATH = raw_input("2/4_Retirer un pattern ds les fpath ('blanc' si rien a retirer) : ")

# selection d'1 seul pattern qu'on veut dans les lignes
PATTERNLINE = raw_input("3/4_Rechercher un pattern ds les lignes ('blanc' si rien a retirer) : ")

NBLINESFILES = raw_input("4/4_indiquer limite sup nb lines des fichiers (1 chiffre - pas de blanc) : ")
new = int(NBLINESFILES)

print "mot recherche ds les fpath: " , PATTERNFPATH
print "mot retire des fpath : " , NOTPATTERNFPATH
print "mot recherche dans les lignes: " , PATTERNLINE
print "limite nb lines in files : " , new

print "----------"
print ""

fz1 = open(OUT , 'w')

bad_words = open(BADWORDS).read().splitlines()        #openreadsplitlines#
good_words = open(GOODWORDS).read().splitlines()

count=1

for dname, dirs, file1 in os.walk(PATH):       #walk#
    for fname in file1:
        fpath = os.path.join(dname, fname)

# traitement des fpath
        m1 = len(open(fpath , 'r').readlines())        #openreadlines#       #len#

        if m1 < new and PATTERNFPATH in fpath and not NOTPATTERNFPATH in fpath:

# traitement des lignes
            var=[            #var=[#
            line1
            for line1 in open(fpath)       #forinopen#
            if PATTERNLINE in line1          # selection d'1 seul pattern (manuel - blanc si rien)
            if not any(bad_word in line1 for bad_word in bad_words)      #ifany#
#            if any(good_word in line1 for good_word in good_words)
            ] #finduvar#

            print "@@@@### " , m1 , " @@@@ " , fpath  , " @@@@ " , count , " @@@@\n" , ''.join(var)
            p1 = "@@@@### " , str(m1) , " @@@@ " , fpath  , " @@@@ " , str(count) , " @@@@\n" , ''.join(var)
            p2 = ''.join(p1)
            fz1.write(p2)
            count+=1

fz1.close()

# resultat :
# 1/4_Rechercher un pattern ds les fpath (noms des fichiers) : py
# 2/4_Retirer un pattern ds les fpath (noms des fichiers) : color
# 3/4_Rechercher un pattern ds les lignes : split
# 4/4_indiquer limite sup nb lines des fichiers : 100

# @@@@###  82  @@@@  /Python27/2017-09\0-1-922-OK.py  @@@@  1  @@@@
#         w = line.split()[3]          #split#        # variable memorisee

# @@@@###  72  @@@@  /Python27/2017-09\0-1-974-OK.py  @@@@  2  @@@@

# @@@@###  84  @@@@  /Python27/2017-09\0-1-carcasse-1.py  @@@@  3  @@@@
#         w = line.split()[3]           # variable memorisee
#         line2 = line.split()[3] , line         # modifier la syntaxe ligne contenant le pattern

#fin#
#%_1227

haut

Modifier la derniere occurrence d'un pattern avec SUB #part1228


#!/usr/bin/python

#@_1228y_modifier_derniere_occurrence_d'1_pattern!!sub
#deb#

import re

IN="1-in.txt"
OUT="1-out.txt"

# IN :
# %data1/login/larousse/file1.txt
# @data2/login/collins/file2.txt


f1 = open(IN , 'r')      #open#
fw = open(OUT , "w")

for line in f1:
    line = re.sub(r'(.*)/' , r'\1 @ ' , line)     #sub#
    print line.strip()
    fw.write(line)

fw.close()

# resultat
# %data1/login/larousse @ file1.txt
# @data2/login/collins @ file2.txt

#fin#
#%_1228

haut

Modifier une portion de ligne avec SUB [var crochet] #part1229


#!/usr/bin/python

#@_1229y_deleter_groupes_de_mots!!forinlistdirif_sort_forinopenif_join_sub
#deb#

import os
import re

PATHHOME="/Python27/tests-2017-10"

# uyz-1.txt
# Missfire SP 2233 , 4365
# SP Time error 5ms data1
# autofire SP 4512 , 5227
# disabled field shot 2325
# ------------- SP

# uyz-2.txt
# disabled data1 SP 4431
# autofire Tmg error 7ms SP3379
# disabled SP 2325 SP 4512 , 5227
# missfire SP 2233 , 4365


newlist = [i for i in os.listdir(PATHHOME) if i[0:3] == "uyz"]      #forinlistdir#
#print newlist

newlist2 = sorted(newlist)        #sort#
fz1 = open("out-3085.txt" , 'w')

for file1 in newlist2:
    var=[        #var=[#
    line1
    for line1 in open(file1)        #forinopen#
    if "SP" in line1 or "shot" in line1 or "autofire" in line1
    ] #finduvar#
    w1b = ''.join(var)

    w1c = re.sub('Time.*?ms|Tmg.*?ms|dis.*?ed|f.*?d' , '' , w1b)      #sub#

    print w1c
    fz1.write(w1c)
fz1.close()

# resultat :
# Missfire SP 2233 , 4365
# SP  data1
# autofire SP 4512 , 5227
#   shot 2325
# ------------- SP
#  data1 SP 4431
# autofire  SP3379
#  SP 2325 SP 4512 , 5227
# missfire SP 2233 , 4365

#fin#
#%_1229

haut

Modifier syntaxe avec SUB REPLACE - out multi fichiers #part1230


#!/usr/bin/python

#@_1230y_modifier_syntaxe_inoutplusieursfichiers_avec_sub_replace!!oslistdir_split_inoutplusieursfichiers_sub_replace
#deb#

import os
import re

PATH="/Python27/tests-2018-02"

# v-abide.txt :
# to accept or submit to; suffer
# to comply (with)
# to withstand or sustain; endure

# v-account.txt :
# money deposited at a bank
# they transferred their publicity account to a new agent
# to consider or reckon


newlist = [i for i in os.listdir(PATH) if i[0:2] == "v-"]      #forinlistdir#
#print newlist

for file in newlist:
    file = file.strip()
    fileb = file.split("-")[1].split(".")[0]       #split#
    print ""
    f2 = open("u-" + fileb + ".txt" , "w")
    with open(file , "r") as f1:       #withopen#
        for line1 in f1:

            line1 = re.sub('suffer' , 'to comply' , line1)       #sub#
            line1 = line1.replace('(with)' , '')       #replace#
            print line1.strip()
            f2.write(line1)
f2.close()

# resultat :
# u-abide.txt :
# to accept or submit to; to comply
# to comply
# to withstand or sustain; endure

# u-account.txt :
# money deposited at a bank
# they transferred their publicity account to a new agent
# to consider or reckon

#fin#
#%_1230

haut

Colorier plusieurs mots dans un texte #part1231


#!/usr/bin/python

#@_1231y_colorier_des_mots_cles!!rawinput_join_read_splitlines_readlines_any_search_format_group_pass
#deb#

from __future__ import print_function
import fixpath
from colorama import init, Fore, Back, Style

init()

import os
import sys
import re
import time


SITE="1-0-RESULT-OF9BA2-CONCAT-only-SITE.txt"

# SITE (de la forme : exemple de lignes :)
# part9999  line data

# liste des mots cles
keysinfile="1-1-liste-ORIGINALE-CLE.txt"

TIME = 1

#############################

# keysinfile :
# cle1
# cle2
# cle3

part = raw_input('taper un numero de  part....   : ')      #rawinput#
print (part)
part2 = "part" , part
part2b = ''.join(part2)
print (part2b)
print ()

###########################

good_words = open (keysinfile).read().splitlines()        #openreadsplitlines#

fs = open(SITE , 'r').readlines()      #openreadlines#
for s in fs:

    if any(good_word in s for good_word in good_words):  # en premier      #ifany#
        for good_word in good_words:                     # en deuxieme

            m = re.search(("(.*)" + good_word + "(.*)") , s)      #search#

            if m and part2b in s:

                m1 = "{0}".format(m.group(1))
                m2 = "{0}".format(m.group(2))

                print (Style.NORMAL + m1
                    + Back.RED + Style.BRIGHT + good_word
                    + Style.NORMAL
                    + Back.RESET + m2
                    , end='')
                print ()

            if not m and part2b in s:
                pass      #pass#

    if not any(good_word in s for good_word in good_words) and part2b in s:
        print (s.strip())

#fin#
#%_1231

haut

Remplacer un pattern avec REPLACE #part1232


#!/usr/bin/python

#@_1232y_modifier_remplacer_1_pattern!!replace
#deb#

INTER4="inter4.txt"
INTER5="inter5.txt"

# INTER4 :
# Line 1
# @@ data
# %@
# Line 4
# End

with open (INTER4 , 'r') as fwhab4:       #withopen#
    fwhab5 = open (INTER5 , 'w')
    for line4 in fwhab4:
        print line4.replace('@@' , 'NEW1').replace('%@' , 'NEW2').strip()       #replace#
        w5 = line4.replace('@@' , 'NEW1').replace('%@' , 'NEW2').strip()
        fwhab5.write(w5 + "\n")

fwhab4.close()
fwhab5.close()

# resultat :
# Line 1
# NEW1 data
# NEW2
# Line 4
# End

#fin#
#%_1232

haut

Joindre 2 lignes consecutives sur une seule ligne #part1233


#!/usr/bin/python

#@_1233y_joindre_2_lignes_successives_sur_1_ligne!!read_split_enumerate_endswith
#deb#

IN="in.txt"

# IN :
# a data=go1</a>
# des_valeurs_d_1_colonne_avec_remise_a_zero
# a data=go2</a>
# afficher_1_fichier


f = open (IN).read().split("\n")        #openreadsplit#
fw = open ("out.txt" , "w")

for i,line in enumerate(f):      #enumerate#
    if line.endswith(">"):      #endswith#
        h = line + f[i+1]
        print h
        fw.write(h + "\n")
fw.close()

# resultat :
# a data=go1</a>des_valeurs_d_1_colonne_avec_remise_a_zero
# a data=go2</a>afficher_1_fichier

#fin#
#%_1233

haut

Coller 2 files side by side (= PASTE en bash) #part1234


#!/usr/bin/python

#@_1234y_coller_2_files_side_by_side_idem_que_PASTE!!rstrip_readline_paste
#deb#

INTER1="1-file1.txt"
INTER2="1-file2.txt"
INTER3="1-file-result.txt"

# INTER1 :
# A1 A2 A3
# A4 A5 A6
# A7 A8 A9

# INTER2 :
# B1 B2 B3
# B4 B5 B6
# B7 B8 B9


# merge 2 files side by side = paste

comp1 = open (INTER1 , "r")       #open#
comp2 = open (INTER2 , "r")
f5 = open (INTER3 , "w")

for line in comp1:

    wcomp = line.rstrip() + " " + comp2.readline().strip()
    print wcomp
    f5.write(wcomp + "\n")

comp1.close()
comp2.close()
f5.close()

# resultat
# A1 A2 A3 B1 B2 B3
# A4 A5 A6 B4 B5 B6
# A7 A8 A9 B7 B8 B9

#fin#
#%_1234

haut

Joindre 2 lignes consecutives, deleter la 3 eme #part1235


#!/usr/bin/python

#@_1235y_joindre_2_lignes_consecutives_et_deleter_la_3eme!!readlines_enumerate_startswith_join_replace_occur
#deb#

DATAUKFR="in.txt"
OUT71="out1.txt"
OUT72="out2.txt"


# DATAUKFR
# we'll have to accept whatever the panel says
# nous devons accepter tout le panneau dit
# traduction automatique de Google
# I got your invitation but I'm unable to accept
# Je ai recu votre invitation, mais je suis incapable d'accepter
# traduction automatique de Google

# ajouter texte fin de ligne

data = open (DATAUKFR).readlines()        #openreadlines#
fw1 = open (OUT71, 'w')

for n,line in enumerate(data):       #enumerate#
    if line.startswith("traduction"):     #startswith#
       data[n] = "\n" + line.rstrip()
    else:
       data[n]=line.rstrip()
#      print ' @@ '.join(data).replace('traduction automatique de Google  @@  ' , '')       #replace#
       fw1.write('  @@  '.join(data).replace('traduction automatique de Google  @@  ' , ''))

# deleter 2 eme occurrence d 1 pattern bout de ligne

with open (OUT71 , 'r') as fw1:      #withopen#
    fw2 = open (OUT72, 'w')
    for line in fw1:

        fchar = '@@'
        rchar = ''
        print line[::-1].replace(fchar, rchar[::-1], 1)[::-1].strip()
        fw2.write(line[::-1].replace(fchar, rchar[::-1], 1)[::-1])

fw1.close()
# data.close()

# resultat
# we'll have to accept whatever the panel says  @@  nous devons accepter tout le panneau dit
# I got your invitation but I'm unable to accept  @@  J' ai recu votre invitation, mais je suis incapable d'accepter

#fin#
#%_1235

haut

Joindre des lignes apres marquage RANGE LEN ENUMERATE #part1236


#!/usr/bin/python

#@_1236y_joindre_lignes_apres_marquage!!readlines_range_len_split_enumerate_startswith_break_join_tableau
#deb#

import re

IN1="in1.txt"
IN2="in2.txt"

OUT1="out1.txt"
OUT2="out2.txt"

# IN1 :
# we'll have to accept whatever the panel says
# nous devons accepter quoi que dise le panneau
# traduction automatique de Google
# I got your invitation but I'm unable to accept
# Je ai recu votre invitation, mais je suis incapable d'accepter
# traduction automatique de Google

# ajouter des lignes a la fin

open (IN2 , "w").write( open (IN1).read() + "end\nend\n")        #openread#

######################

# marquer des lignes en utilisant le repere : "traduction automatique de Google"

fd30 = open (IN2 , 'r').readlines()       #openreadlines#
fd31 = open (OUT1 , "w")

for i in range(len(fd30)):      #rangelen#

    element1 = fd30[i]
    element2 = fd30[i+1]
    element3 = fd30[i+2].split("de Google")[0]       #split#
    if element3 == "traduction automatique ":
        w = element1 , "@ " , element2
        w2 = ''.join(w)
        print element1 , "@ " , element2
        fd31.write(w2)
    if element2.startswith("end"):     #startswith#
        break        #break#

fd31.close()

###################

# joindre 2 lignes consecutives

f = open (OUT1).read().split("\n")       #openreadsplit#
fw = open (OUT2 , "w")

for i,line in enumerate(f):      #enumerate#
    if line.startswith("@"):
        h = f[i -1] + " " +line
        print h
        fw.write(h + "\n")
fw.close()

# resultat :
# we'll have to accept whatever the panel says @ nous devons accepter quoique dise le panneau
# I got your invitation but I'm unable to accept @ J' ai recu votre invitation, mais je suis incapable d'accepter

#fin#
#%_1236

haut

Compter les fichiers d'un repertoire FNMATCH #part1237


#!/usr/bin/python

#@_1237y_count_fichiers_d_1_repertoire!!len_fnmatch_filter_listdir_search_count
#deb#

import fnmatch

# nb de fichiers  du repertoire  PATHTOTALWXY   (methode courte)

print len(fnmatch.filter(os.listdir(PATHTOTALWXY), '*.txt'))       #len#      #match#

######

data = os.listdir(PATHZ)
count2 = 0
for file in data:

    count2 = count2 + 1

print "nb de fichiers z- ds PATHZ : " , count2

#######

import re

data = os.listdir(PATHY)
count1 = 0
count2 = 0
for file in data:

    m1 = re.search("y-" + lettrealpha , file)      #search#
    if m1:
#        print file.strip()

        count1 = count1 + 1

    m2 = re.search("y-" , file)
    if m2:
#        print file.strip()

        count2 = count2 + 1

print ""
print "dans : " , PATHY , " ; nb de fichiers : y-" + lettrealpha , " : " , count1

print "dans : " , PATHY , " ; nb de fichiers : y-  : " , count2

# resultat :

#dans :  /Python27/0-0-2-COLLINS-syno/Y-  ; nb de fichiers : y-g  :  16
#dans :  /Python27/0-0-2-COLLINS-syno/Y-  ; nb de fichiers : y-  :  916

#fin#
#%_1237

haut

Compter les fichiers d'un repertoire APPEND #part1238


#!/usr/bin/python

#@_1238y_count_fichiers_d'1_repertoire!!forinlistdirif_append_count_join
#deb#

import os

PATH="/Python27/tests-2018-03"

# BUT : compter nombre de fichiers (sans creation de fichier)

# 3-data1.txt :
# line1 data1
# line2 data1

# 3-data2.txt :
# line1 data2
# line2 data2

# 3-data3.txt :
# line1 data3
# line2 data3


# 1/2_ utiliser la str

newlist2 = [j for j in os.listdir(PATH) if j[0:3] == "3-d"]      #forinlistdir#
#print newlist

# newlist2 = [j for j in os.listdir(PATH) if "." in j]     # pour tous les fichiers du repertoire

count = 1
for file2 in newlist2:       # file2  -->  str
    z1 = str(count)        # convert integer to str
    count += 1

# z1         # cale a gauche apres la boucle count (pour avoir le dernier)
print "     utiliser la str ---> nb de fichiers  selectionnes : " , z1     # 3

#####

# 2/2_ utiliser la liste

newlist2 = [j for j in os.listdir(PATH) if j[0:3] == "3-d"]
#print newlist

count = 1
for file2 in newlist2:       # file2  -->  str
    z1 = str(count)        # convert integer to str
    count += 1

    combined_list = []
    combined_list.append(z1)      #append#

w = ''.join(combined_list)

print "     utiliser la liste ---> nb de fichiers  selectionnes : " , w     # 3

# resultat :
#     utiliser la str ---> nb de fichiers  selectionnes :  3
#     utiliser la liste ---> nb de fichiers  selectionnes :  3

#fin#
#%_1238

haut

Lister un repertoire avec 'getstatusoutput' #part1239


#!/usr/bin/python

#@_1239y_lister_repertoire!!getstatusoutput_split_forinifin_command
#deb#

import commands

(st, output) = commands.getstatusoutput('ls -a')

print [f for f in output.split('\n') if 'pattern' in f ]         #split#     # liste de fichiers

# resultat : (liste des fichiers)
z-pattern1.txt z-pattern2.txt

#fin#
#%_1239

haut

Lister un repertoire avec STARTSWITH #part1240


#!/usr/bin/python

#@_1240y_lister_repertoire!!listdir_startswith
#deb#

import os

PATH="/Python27/1_2015-5-28"
OUT1="1-out1.txt"
FILESTOCONCATENATE="CLIENT"

with open(OUT1 , 'w') as output:
    dir = os.listdir(PATH)    # autre syntaxe : for file in os.listdir(PATH)     #forinlistdir#
    for file in dir:
        if file.startswith(FILESTOCONCATENATE):     #startswith#
            print file
            output.write(file + "\n")
output.close()

# resultat
# CLIENT1
# CLIENT2
# CLIENT3

#fin#
#%_1240

haut

Lister un repertoire avec SEARCH #part1241


#!/usr/bin/python

#@_1241y_lister_repertoire!!listdir_search_chdir
#deb#

import os
import re

PATHHOME="/Python27/TESTS/"
LISTY="1-liste-y.txt"

os.chdir(PATHHOME)
fy = open (LISTY , "w")
road = os.listdir(PATHHOME)

for filey in road:     
    my = re.search("y-" , filey)      #search#
    if my:
#        print filey.strip()
        os.chdir(PATHHOME)
        fy.write(filey + "\n")

fy.close()

# resultat :
# y-1.txt
# y-2.txt
# y-3.txt

#fin#
#%_1241

haut

Tester si un fichier est vide #part1242


#!/usr/bin/python

#@_1242y_tester_si_1_fichier_est_vide!!listdir_path_getsize
#deb#

import os

PATHY="/PYTHON/Y"

road = os.listdir(PATHY)
for filey in road:      # autre syntaxe : for filey in os.listdir(PATHY)
    os.chdir(PATHY)
    taille = os.path.getsize(filey)       #getsize#
    if taille == 0:
        print "   ---> la taille de " , filey , " est 0"

#fin#
#%_1242

haut

Deleter un fichier s'il est vide #part1243


#!/usr/bin/python

#@_1243y_deleter_fichier_si_vide!!forinlistdirif_path_getsize_startswith_remove
#deb#

# get list of all files in the directory
newlist = [x for x in os.listdir(PATH) if x[0]!='.']      #forinlistdir#

# now loop through the files and remove empty ones
for each_file in newlist:

# check size and delete if 0
    if os.path.getsize(each_file) == 0:       #getsize#
        if each_file.startswith("stz-"):     #startswith#
#            print each_file
            os.remove(each_file)      # attention : effacement

#fin#
#%_1243

haut

Supprimer les lignes vides ISSPACE #part1244


#!/usr/bin/python

#@_1244y_deleter_lignes_vides!!join_forinif_isspace
#deb#

IN="3-in-site.txt"

OUT1="3-out1-site.txt"
OUT2="3-out2-site.txt"

# depart : IN
# Line 1

# Line 3
# Line 4

# Line 6

############################

# methode 1/2 : la plus courte :

f2 = open(OUT1 , 'w')

with open(IN,'r') as file3:
    for line3 in file3:
        if not line3.isspace():            #isspace#
            print line3.strip()
            f2.write(line3)
f2.close()

print ""
print "ok lignes vides supprimees dans : " , OUT1

#################################

# methode 2/2 :

with open (IN , 'r') as fwhab5:       #withopen#
    fwhab6 = open (OUT2 , 'w')
    w8 = "".join(line6 for line6 in fwhab5 if not line6.isspace())        #isspace#
    print w8
    fwhab6.write(w8 + "\n")

fwhab5.close()
fwhab6.close()

print "ok lignes vides supprimees dans : " , OUT2

# resultat :
# Line 1
# Line 3
# Line 4
# Line 6

#fin#
#%_1244

haut

Creer des fichiers vides dont les noms sont contenus dans un fichier #part1245


#!/usr/bin/python

#@_1245y_creer_fichiers_vides_en_lisant_un_fichier!!path_exists_makedirs_chdir
#deb#

import os

# BUT : creer un repertoire qui contiendra des fichiers vides

PATHTOUCH="/Python27/0-0-3-WR-UKdef/TOUCH"

# in.txt :
# abandon
# absorb
# abstain
# abstract

# creer repertoire s'il n'existe pas

if not os.path.exists(PATHTOUCH):        #pathexists#
    os.makedirs(PATHTOUCH)

# ouvrir le fichier contenant les noms des fichiers vides

f = open ("in.txt" , "r")       #open#
for i in f:
    i = i.strip()
    os.chdir(PATHTOUCH)
    with open("z-" + i + ".txt" , "w") as fw:      #withopen#

        fw.write("")
f.close()
fw.close()

# resultat  dans le repertoire TOUCH :
# creation de 4 fichiers vides :

# z-abandon.txt
# z-absorb.txt
# z-abstain.txt
# z-abstract.txt

#fin#
#%_1245

haut

Tester si la derniere ligne contient 1 pattern (entree multi fichiers) #part1246


#!/usr/bin/python

#@_1246y_capture_tester_ds_inplusieursfichiers_si_pattern_dans_derniere_ligne!!forinlistdir_read_splitlines_tableau
#deb#

import os
import glob
import time

OUT="1-result-last-line.txt"

PATTERN="end"

PATH="/nfs/u/tests-2018-03"

TIME=1

# file1.txt :
# line1 datafile1
# line2 datafile1

# file2.txt
# line1 datafile2
# line2 datafile2
# end

# file3.txt
# line1 datafile3
# line2 datafile3

################

# deleter anciens fichiers
for currentFile in glob.glob('*'):
    e1 = "1-result-last-line";
    if currentFile.startswith(e1):
        os.remove(currentFile)

print "ok fichiers anciens deletes"
time.sleep(TIME)

##########

# utilisation d'1 tableau

newlist = [i for i in os.listdir(PATH) if i[0:4] == "file"]      #forinlistdir#
#print newlist

for file in newlist:
    file = file.strip()
    print file

    fw = open (OUT , "a")
    f1 = open(file , 'r')
    secondlist = f1.read().splitlines()      #openreadsplitlines#
    print secondlist[-1]                 # print la derniere ligne
    print ""
    if not PATTERN in secondlist[-1]:
        print file , " : manque end"
        print "----------"
        fw.write(file + " : manque end" + "\n")

    if PATTERN in secondlist[-1]:
        print file , " : ok"
        print "----------"
        fw.write(file + " : ok" + "\n")

    time.sleep(TIME)
fw.close()

# resultat :
# file1.txt : manque end
# file2.txt : ok
# file3.txt : manque end

#fin#
#%_1246

haut

Tester l'existence d'un mot dans un fichier IF ANY #part1247


#!/usr/bin/python

#@_1247y_tester_existence_d'1_mot_dans_1_fichier!!readlines_any_inforin
#deb#

# in.txt
# to make an attempt at doing something OR to do something
# we made our first attempt in January
# she made every attempt to put him at ease

pattern = "make"

fd = open ("in.txt", 'r').readlines()        #openreadlines#

if any([pattern in s for s in fd]):        #ifany#
    print "ok : le mot : " , pattern , " : existe dans le fichier"
else:
    print "non pas de mot : " , pattern , " dans le fichier"

# resultat :
# ok : le mot :  make  : existe dans le fichier

#fin#
#%_1247

haut

Tester l'existence d'un mot dans plusieurs fichiers IF ANY #part1248


#!/usr/bin/python

#@_1248y_tester_existence_d'1_mot_dans_plusieurs_fichiers!!readlines_any
#deb#

import os

pattern="#toto#"

PATH="/Python27/0-0-05-SCAN-SEARCH-XARGS"
#####

newlist = [i for i in os.listdir(PATH) if i[0:6] == "s-part"]       #forinlistdir#

for file in newlist:
    fd = open (file , 'r').readlines()          #openreadlines#

    if any([pattern in s for s in fd]):          #ifany#
        print "ok : le mot : " , pattern , " : existe dans le fichier : " , file
    else:
        print "non pas de mot : " , pattern , " dans le fichier : " , file

# resultat :
# ok : le mot :  #toto#  : existe dans le fichier :  s-part965.txt
# non pas de mot :  #toto#  dans le fichier :  s-part966.txt
# ok : le mot :  #toto#  : existe dans le fichier :  s-part967.txt

#fin#
#%_1248

haut

Tester l'existence d'un mot dans un fichier [var crochet] #part1249


#!/usr/bin/python

#@_1249y_tester_existence_d'1_mot_dans_1_fichier!!read_splitlines_any_inforin
#deb#

# in.txt
# to make an attempt at doing something OR to do something
# we made our first attempt in January
# she made every attempt to put him at ease


# 1-good-words.txt :
# make

good_words = open("1-good-words.txt").read().splitlines()       #openreadsplitlines#

var=[        #var=[#
line1
for line1 in open("in.txt")        #forinopen#
if any(good_word in line1 for good_word in good_words)       #ifany#
] #finduvar#

w1b = ''.join(var)
print w1b

# resultat :
# to make an attempt at doing something OR to do something

#fin#
#%_1249

haut

Copier un fichier et le renommer avec la date du jour #part1271


#!/usr/bin/python

#@_1271y_copier_fichier_et_renommer_avec_date!!datetime_shutil_copie2
#deb#

# BUT : renommer un fichier en y incluant la date

import time
import shutil
import datetime

TIME=1

IN = "1-site.txt"    

# IN:
# to remain faithful (to): (exemple :) to abide by your promise
# to dwell ; to abide
# to await in expectation ; to abide

###########

# date du jour pour nommer la copie de IN

date = datetime.datetime.now()           # datetime

y = str(date.year) + "-" + str(date.month) + "-" + str(date.day)
print "ok la date du jour est dans une variable"
print "     cette variable servira a nommer la copie du fichier IN"

COPYNEW = y + '.txt'

time.sleep(TIME)

###########################

# copier IN et le renommer COPYNEW : incluant la date du jour
shutil.copy2(IN, COPYNEW)          # shutil  # copy2

print "ok copie du fichier IN ds :" , COPYNEW
time.sleep(TIME)

# resultat :  (si la date est le 20 janvier 2020)
# ok copie du fichier IN ds : 2020-1-20.txt

#fin#
#%_1271

haut

Copier un fichier dans un autre repertoire (ou changer de repertoire) #part1250


#!/usr/bin/python

#@_1250y_copier_deplacer_fichier_ds_autre_repertoire!!shutil_move_copy
#deb#

import os
import shutil

dir_src = PATHHOME + RESULTTOTAL
dir_dst = PATHFR + RESULTTOTAL


# shutil.move(dir_src, dir_dst)
shutil.copy2(dir_src, dir_dst)       #copy#

# changer de repertoire : os.chdir(PATHHOME)
# os.chdir(PATHHOME)   # important de revenir ds repertoire d'origine

print " ---> ok copie de UK ING ds le repertoire FR"

#fin#
#%_1250

haut

Copier ou changer de repertoire plusieurs fichiers de meme prefixe dans un autre repertoire #part1251


#!/usr/bin/python

#@_1251y_copier_changer_repertoire_inoutplusieursfichiers!!listdir_startswith_shutil_copy_path_join
#deb#

import os
import shutil

PATH="/Python27/0-0-2-collins-10-9"
PATHZ="/Python27/0-0-2-collins-10-9/Z-/"
PATHW = "/Python27/0-0-2-collins-10-9/W-/"

for fna in os.listdir(PATH):      #forinlistdir#
    if fna.startswith("w-"):     #startswith#

        shutil.copy(os.path.join(PATH, fna), PATHW)       #copy#
#        shutil.move(os.path.join(PATH, fna), PATHW)       #move#

# resultat :
# les fichiers w- sont copies ou transferes dans le repertoire PATHW

#fin#
#%_1251

haut

Copier plusieurs directories dans un autre directory - WALK FNMATCH #part1252


#!/usr/bin/python

#@_1252y_copier_plusieurs_repertoires_dans_1_autre_repertoire!!glob_walk_path_exists_makedirs_fnmatch_filter_shutil_copy_sub
#deb#

import sys
import os
import re
import glob
import shutil

import os.path
import fnmatch

# BUT : copier tous les fichiers uwxy (provenant de plusieurs directories) ds 1 seul directory : PATHTOTALUWXY

# source
PATHUWXY="/nfs/u/0-0-3-WR-FR/UWXY-*"       # tous les UWXY-  (garder le tiret -)

# destination
PATHTOTALUWXY="/nfs/u/0-0-3-WR-FR/UWXYTOTAL"      # (sans tiret -)

# excluded_subdirs = ['dir1', 'dir2']                 # subdir a exclure de la copie

# folder pour la destination de la copie
dest_dir = PATHTOTALUWXY

files_patterns = ['*.txt']

for root_path in glob.glob(PATHUWXY):
    print root_path

    for root, dirs, files in os.walk(root_path):       #walk#
#        for dir in excluded_subdirs:
#            if dir in dirs:
#                dirs.remove(dir)            # remove the dir from the subdirs to visit

        if not os.path.exists(dest_dir):        #pathexists#
            os.makedirs(dest_dir)               # create the dir if not exists

        for pattern in files_patterns:
            for thefile in fnmatch.filter(files, pattern):         #match#
                shutil.copy2(os.path.join(root, thefile), dest_dir)       #copy#

#fin#
#%_1252

haut

Renommer un fichier #part1253


#!/usr/bin/python

#@_1253y_renommer_1_fichier!!listdir_pathexists_rename_replace
#deb#

import os

for road in os.listdir('.'):      #forinlistdir#
    if os.path.exists('file_1.txt'):        #pathexists#
        os.rename(road, road.replace('file_1.txt' , 'file_001.txt'))       #replace#

# a few lines written in addition to reach 20 lines for this script
# a few lines written in addition to reach 20 lines for this script
# a few lines written in addition to reach 20 lines for this script
# a few lines written in addition to reach 20 lines for this script

#fin#
#%_1253

haut

Renommer plusieurs fichiers : exemple 1 #part1254


#!/usr/bin/python

#@_1254y_renommer_inoutplusieursfichiers!!glob_join_rename_chdir_split
#deb#

import os
import glob

# les fichiers de depart sont :
# cable
# calculate

# selectionner les fichiers qui changeront d'extension : ici les fichiers commencant par  c
LETTREMIN="c"          

PATH="C:/Users/jeant/Desktop/r2/script"

os.chdir(PATH)
for file in glob.glob(LETTREMIN + "*"):        #glob#
# for file in glob.glob("*"):
    oldF="".join(file.split()[0])       #split#
    newF = oldF , ".txt"
    newF2 = ''.join(newF)

    os.rename(oldF , newF2)        #rename#

# resultat dans PATH :
# cable.txt
# calculate.txt

#fin#
#%_1254

haut

Renommer plusieurs fichiers : exemple 2 #part1278


#!/usr/bin/python

#@_1278y_renommer_extension_inoutplusieursfichiers!!os.path.join_os.path.isfile_os.path.splitext_replace_rename
#deb#

import os
import sys

# les fichiers de depart sont :
# file1.265
# file2.265


PATH="C:/Users/toto/Desktop/r2"

for filename in os.listdir(PATH):
        infilename = os.path.join(PATH,filename)     #os.path.join#
        if not os.path.isfile(infilename):           #os.path.isfile# 
            continue
        oldbase = os.path.splitext(filename)             #os.path.splitext#
        newname = infilename.replace('.265', '.264')        #replace#
        output = os.rename(infilename, newname)             #rename#

# resultat dans PATH :
# file1.264
# file2.264

#fin#
#%_1278

haut

Renommer plusieurs fichiers selon correspondance noms anciens-nouveaux #part1255


#!/usr/bin/python

#@_1255y_renommer_inoutplusieursfichiers_selon_correspondance_ancien_nouveau!!split_path_exists_rename
#deb#

import os
import re

# BUT :  renommer les fichiers part... selon le fichier LI5

LI5="1-file-old-new.txt"

# il existe 3 fichiers dont les noms sont au depart :
# part123.txt
# part137.txt
# part151.txt

# LI5 :  (col1 = ancien  //  col2 = nouveau)
# 123  901
# 137  902
# 151  903

with open(LI5 , "r") as f1:        #withopen#
    for row in f1:
        old = row.split()[0]
        old2 = "part" + old + ".txt"
        new = row.split()[1]       #split#
        new2 = "part" + new + ".txt"

        if os.path.exists(old2):        #pathexists#
            os.rename(old2 , new2)
f1.close()

# resultat :  les 3 fichiers ont ete renommes :
# part901.txt
# part902.txt
# part903.txt

#fin#
#%_1255

haut

Renommer plusieurs fichiers en ajoutant 0 devant des digits #part1256


#!/usr/bin/python

#@_1256y_renommer_inoutplusieursfichiers!!glob_join_split_rename
#deb#

import os,glob

# noms des fichiers avant :
# CLIENT-1
# CLIENT-11

# fichier sous la forme : CLIENT-1
for file1 in glob.glob("CLIENT-[0-9]"):
    digita = "".join(file1.split("-")[0])       #split#
    digitb = "".join(file1.split("-")[1])
    digitc = digita , "-00" , digitb
    digitd = "".join(digitc)

    os.rename(file1,digitd)

#####
# fichier sous la forme : CLIENT-11
for file2 in glob.glob("CLIENT-[0-9][0-9]"):
    digitaa = "".join(file2.split("-")[0])
    digitbb = "".join(file2.split("-")[1])
    digitcc = digitaa , "-0" , digitbb
    digitdd = "".join(digitcc)

    os.rename(file2,digitdd)

# noms des fichiers apres :
# CLIENT-001
# CLIENT-011

#fin#
#%_1256

haut

Deleter un fichier (exemple 1) #part1257


#!/usr/bin/python

#@_1257y_deleter_fichier_exemple_1!!path_exists_remove
#deb#

import os

os.path.exists('fichier.txt') and os.remove('fichier.txt')        #pathexists#

# a few lines written in addition to reach 20 lines for this script
# a few lines written in addition to reach 20 lines for this script
# a few lines written in addition to reach 20 lines for this script

#fin#
#%_1257

haut

Deleter un fichier (exemple 2) #part1258


#!/usr/bin/python

#@_1258y_deleter_fichier_exemple_2!!startswith_remove
#deb#

import time
import glob
import os

TIME=2

for currentFile in glob.glob('*'):
    e1 = "interrepl";
    e2 = "data-forsear";
    if currentFile.startswith(e1):
        os.remove(currentFile)
    if currentFile.startswith(e2):
        os.remove(currentFile)

print "fichiers deletes"
time.sleep(TIME)

#fin#
#%_1258

haut

Inclure une commande BASH avec OS.SYSTEM (exemple 1) #part1259


#!/usr/bin/python

#@_1259y_inclure_commande_BASH_dans_python_OS.SYSTEM!!system_bash
#deb#

import os

IN="in.txt"
OUT="out.txt"

# deleter la derniere ligne d'1 fichier :

os.system("sed '$d' " + IN + " > " + OUT)

#fin#
#%_1259

haut

Inclure une commande BASH avec OS.SYSTEM (exemple 2) #part1260


#!/usr/bin/python

#@_1260y_inclure_commande_BASH_dans_python_OS.SYSTEM!!system_count_bash
#deb#

import os

# commande 'du' pour chaque user : resultat de la taille finale (tail)
# creation d'1 fichier pour chaque user

fg = open(LISTUSERSSELECTION , 'r')       #open#

count=1
for i in fg:
    i = i.strip()           # user
    i2 = "/nfs/u/" + i      # /nfs/u/user

#    print i
    print "  --> " , count , "/" , linecount , " : " , i
    time.sleep(TIME)

    os.chdir(i2)

    os.system("du | tail -n1 >> /nfs/u/user/home/2-du-user-" + i + ".txt")

    count = count + 1
    print ""

# resultat :
#2-du-user-toto1.txt :
# 6342847

#2-du-user-toto2.txt :
# 310858

#fin#
#%_1260

haut

Inclure une commande BASH avec OS.SYSTEM (exemple 3) #part1261


#!/usr/bin/python

#@_1261y_deleter_blanc_au_debut_ou_fin_de_ligne_deleter_retour_chariot!!readlines_sorted_set_sub_sed_system
#deb#

# retirer le blanc devant toutes les lignes
# 1/3_ methode courte avec sed BASH : (OUT2 et OUT3 sont a definir)

os.system("sed 's/^ \{1\}//g' " + OUT2 + " > " + OUT3)

# 2/3_ autre methode

f1 = open(OUT1 , 'r')
f2 = open(OUT2 , 'w')
for line1 in f1.readlines():       #openreadlines#
    line1 = line1[:-2]        # oubien [:-1] si necessaire pour retirer le '\r'
    print line1
    f2.write(line1 + "\n")
f2.close()

# 3/3_ methode longue :

basket = open (CONCAT3).readlines()
f6 = open (CONCAT4 , 'w')
for f in sorted(set(basket)):      #set#        #sort#
#    if not f.startswith("---"):

    f = re.sub(r"(^\s)" , "" , f )   # blanc au debut de ligne      #sub#
#    f = re.sub(r"(\s$)" ,"" , f)    # blanc en fin de ligne
    f6.write(f)
f6.close()

###############

# retirer le \r (retour chariot d'un fichier)

os.system("sed 's/\r//g' " + OUT1 + " > " + OUT2)

#fin#
#%_1261

haut

Inclure une commande BASH avec GETSTATUSOUTPUT #part1262


#!/usr/bin/python

#@_1262y_inclure_commande_bash_dans_python!!getstatusoutput_split_forinifin_bash_command
#deb#

import commands

(st, output) = commands.getstatusoutput('ls -a')

print [f for f in output.split('\n') if 'pattern' in f ]       #split#

# resultat : (liste des fichiers)
z-pattern1.txt z-pattern2.txt

#fin#
#%_1262

haut

Envoyer un fichier sur un compte FTP #part1263


#!/usr/bin/python

#@_1263y_ftp_fichier_sur_un_compte_ftp!!ftplib_storbinary_uneSessionFTP
#deb#

import ftplib
uneSessionFTP = ftplib.FTP( host = 'mylost' ,
                            user = 'myuser' ,
                            passwd = 'mypasswd' )

unFichier = open('toto.txt' , 'rb')
uneSessionFTP.storbinary( 'STOR toto.txt' , unFichier )
unFichier.close()
uneSessionFTP.quit()

#fin#
#%_1263

haut

Envoyer un mail contenant le texte d'un fichier (sans piece attachee) #part1264


#!/usr/bin/python

#@_1264y_mail_envoyer_texte_fichier_a_interieur_du_mail!!MIMEText_smtplib_login_sendmail
#deb#

import smtplib

FILETOSEND="in.txt"

from email.MIMEText import MIMEText

def envoyer(data="none"):
    print "Message en preparation...."
    de = "user1 <prenom.nom@hotmail.fr>"
    pour = "user2 <prenom.nom@hotmail.fr>"


    monFichierMail = open(FILETOSEND , 'rb')
    mail = MIMEText(monFichierMail.read())
    monFichierMail.close()

    mail['From'] = de
    mail['Subject'] = "Test : Envoyer"
    mail['To'] = pour
    username = 'prenom.nom@hotmail.fr'     # votre login ici
    password = 'password'            # votre password ici

    smtp = smtplib.SMTP('smtp.live.com:25')
    smtp.starttls()
    smtp.login(username,password)    # ici laisser telquel
    smtp.sendmail(de, [pour], mail.as_string())
    smtp.close()
    print "Message envoye !"
envoyer()

#fin#
#%_1264

haut

Envoyer un mail contenant une piece attachee (texte-image-audio) #part1265


#!/usr/bin/python

#@_1265y_mail_envoyer_fichier_en_piece_attachee!!MIMEMultipart_mimetypes_encode
#deb#

import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText

fileToSend = "example1.txt"

emailfrom = "prenom.nom@hotmail.fr"
emailto = "prenom.nom@hotmail.fr"

username = "prenom.nom@hotmail.fr"
password = "yourpassword"

msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = emailto
msg["Subject"] = "help I cannot send an attachment to save my life"
msg.preamble = "help I cannot send an attachment to save my life"

ctype, encoding = mimetypes.guess_type(fileToSend)
if ctype is None or encoding is not None:
    ctype = "application/octet-stream"

maintype, subtype = ctype.split("/", 1)      #sub#       #split#

if maintype == "text":
    fp = open(fileToSend)
    # Note: we should handle calculating the charset
    attachment = MIMEText(fp.read(), _subtype=subtype)
    fp.close()
elif maintype == "image":
    fp = open(fileToSend, "rb")
    attachment = MIMEImage(fp.read(), _subtype=subtype)
    fp.close()
elif maintype == "audio":
    fp = open(fileToSend, "rb")
    attachment = MIMEAudio(fp.read(), _subtype=subtype)
    fp.close()
else:
    fp = open(fileToSend, "rb")
    attachment = MIMEBase(maintype, subtype)
    attachment.set_payload(fp.read())        #set#
    fp.close()
    encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
msg.attach(attachment)

server = smtplib.SMTP("smtp.live.com:25")
server.starttls()
server.login(username,password)
server.sendmail(emailfrom, emailto, msg.as_string())
server.quit()

#fin#
#%_1265

haut

ITERTOOLS : generateur de imap ifilter chain groupby takewhile dropwhile #part1266


#!/usr/bin/python

#@_1266y_itertools_operations_math!!lambda_xrange_imap_ifilter_chain_groupby_len_takewhile_dropwhile_sort_read_splitlines_count
#deb#

from itertools import imap , ifilter , ifilterfalse
from itertools import chain
from itertools import groupby
from itertools import takewhile, dropwhile
from itertools import *

# itertools fournit des generateurs : imap , ifilter , ifilterfalse , chain , groupby ...

# imap

carre = lambda n: n ** 2
imap(carre , xrange(5))   # renvoie 1 generateur      #range#
print list(imap(carre , xrange(5)))          # [0, 1, 4, 9, 16]

for elem in imap(carre , xrange(5)):  # recuperer elements
    print elem         # idem mais 1 str par ligne

##############

# ifilter   et   ifilterfalse

pair = lambda n: n % 2 == 0
print list(ifilter(pair , xrange(10)))         # [0, 2, 4, 6, 8]

print list(ifilterfalse(pair , xrange(10)))         # [1, 3, 5, 7, 9]

####################################

# chain

print list(chain(xrange(5) , xrange(8 , 11) , xrange(14 , 18)))

# resultat :
# [0, 1, 2, 3, 4, 8, 9, 10, 14, 15, 16, 17]

#######

for i in chain([1, 2], ['a', 'b']):
    print i

# resultat
# 1
# 2
# a
# b

#################################

# islice

# Stop at 3 :
for i in islice(count(), 3):
    print i

# resultat
# 0
# 1
# 2

########

# By tens to 30 :
for i in islice(count(), 0, 30, 10):
    print i

# resultat
# 0
# 10
# 20

#################################

# groupby

IN="4-in.txt"        # 1 colonne - 1 str par ligne

# IN :
# a
# b
# a
# c
# d
# d
# b
# c
# a
# d

mylist = open(IN).read().splitlines()       #openreadsplitlines#
mylist2 = sorted(mylist)        #sort#

for element , iterateur in groupby(mylist2):
    print "%s est repete %d fois" % (element , len(list(iterateur)))       #len#

# resultat :
# a est repete 3 fois
# b est repete 2 fois
# c est repete 2 fois
# d est repete 3 fois

#############################

# takewhile - dropwhile

print list(takewhile(lambda i: i < 10, xrange(20)))         # On prend tant que i < 10     #lambda#
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print list(dropwhile(lambda i: i < 10, xrange(20)))         # On jette tant que i < 10
# [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

#fin#
#%_1266

haut

YIELD : raccourcit le temps d'execution d'un script #part1267


#!/usr/bin/python

#@_1267y_capture_mots_selon_leur_longueur_yield!!def_listdir_split_len_pass_startswith_yield
#deb#

import os
import time

start = time.time()

PATH="/Python27/data-2017-10"

OUT1="result.txt"

#NOREP="OLD"

LIMIT=5

##############

# 1/2_ with yield  ---> more than 2 times faster

def extraire_mots(PATH):

    for fichier in os.listdir(PATH):       #forinlistdir#
#    print fichier
        if not "SAUV-scripts-from-cgg-py" in fichier:  # and not NOREP in fichier:    # retirer les repertoires

            with open(os.path.join(PATH, fichier)) as f:      #withopen#
                for ligne in f:
                    for mot in ligne.split():       #split#
                        if len(mot) == LIMIT and mot.startswith("s"):     #startswith#       #len#
                            yield mot

for mot in extraire_mots(PATH):
    pass      #pass#

print "script running with 'yield' : it took : " , time.time()-start , "seconds"

#########################################

# 2/2_ without yield

fw1 = open(OUT1 , 'w')

for fichier in os.listdir(PATH):

    if not "colorama" in fichier and not "psutil" in fichier:    # retirer les repertoires

        with open(os.path.join(PATH, fichier)) as f:
            for ligne in f:
                for mot in ligne.split():
                    if len(mot) == LIMIT and mot.startswith("s"):
#                        print mot
                        fw1.write(mot + "\n")
fw1.close()

print "script running without 'yield' : it took : " , time.time()-start , "seconds"

# resultat : mots de 5 digits commencant par un 's'
# script running with 'yield' : it took :   2,5 seconds
# script running without 'yield' : it took :   4,8 seconds

#fin#
#%_1267

haut

YIELD genere un compteur (exemple 1) #part1268


#!/usr/bin/python

#@_1268y_count_controle_depart_arrivee!!def_while_True_yield_break
#deb#

def countfrom(x):
    while True:       #while#
        yield x
        x += 1

for n in countfrom(10):
    print n
    if n > 16:
        break        #break#

# resultat :
# 10
# 11
# 12
# 13
# 14
# 15
# 16
# 17

#fin#
#%_1268

haut

YIELD genere un compteur (exemple 2) #part1269


#!/usr/bin/python

#@_1269y_count_avec_saut_de_valeurs!!def_yield_while_send_count_None
#deb#

def compteur(x):
    n = 0
    while n <= x:       #while#
        v = (yield n)

        if v is not None:
            n = v
        else:
            n += 1

gen = compteur(10)
for i in gen:
    print i
    # Si on en est a 15, on veut aller directement a 18
    if i == 4:
        gen.send(6)

# resultat
# 0
# 1
# 2
# 3
# 4
# 7
# 8
# 9
# 10

#fin#
#%_1269

haut

LAST modification : FEBRUARY 2024