-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestions.csv
We can't make this file beautiful and searchable because it's too large.
945 lines (945 loc) · 568 KB
/
questions.csv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
,Question,Answer
0,Help me!,ok
1,Python not working,sad
2,Need some help,here
3,help needed,:)
4,Windows crashed,O_o
5,What does the “yield” keyword do?,To understand what yield does you must understand what generators are. And before you can understand generators you must understand iterables.
6,"
Q: How to print colored text in terminal in Python? ","1789 This somewhat depends on what platform you are on. The most common way to do this is by printing ANSI escape sequences. For a simple example, here's some python code from the blender build scripts: class bcolors: HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' To use code like this, you can do something like print(bcolors.WARNING + ""Warning: No active frommets remain. Continue?"" + bcolors.ENDC) or, with Python3.6+: print(f""{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}"") This will work on unixes including OS X, linux and windows (provided you use ANSICON, or in Windows 10 provided you enable VT100 emulation). There are ansi codes for setting the color, moving the cursor, and more. If you are going to get complicated with this (and it sounds like you are if you are writing a game), you should look into the ""curses"" module, which handles a lot of the complicated parts of this for you. The Python Curses HowTO is a good introduction. If you are not using extended ASCII (i.e. not on a PC), you are stuck with the ascii characters below 127, and '#' or '@' is probably your best bet for a block. If you can ensure your terminal is using a IBM extended ascii character set, you have many more options. Characters 176, 177, 178 and 219 are the ""block characters"". Some modern text-based programs, such as ""Dwarf Fortress"", emulate text mode in a graphical mode, and use images of the classic PC font. You can find some of these bitmaps that you can use on the Dwarf Fortress Wiki see (user-made tilesets). The Text Mode Demo Contest has more resources for doing graphics in text mode. Hmm.. I think got a little carried away on this answer. I am in the midst of planning an epic text-based adventure game, though. Good luck with your colored text! share|| follow |||| edited Dec 10 '19 at 19:35 Richard 37.8k2222 gold badges124124 silver badges193193 bronze badges answered Nov 13 '08 at 19:25 joeldjoeld 20.7k33 gold badges2323 silver badges2222 bronze badges 6 what's the disabled used for? – cregox May 11 '10 at 21:58 3 @Cawas It looks to me like it is for disabling coloring for all colors which are printed using a particular bcolors instance. For example, you could create a bcolors instance and then use the member variables of the instance to print out your coloring characters, but then if you decided you no longer wanted coloring, you could call disable before printing out the characters and they would just print out as empty strings. – Steven Oxley Jun 15 '10 at 18:42 4 On Linux, you might want to use tput, like so since it results in more portable code. – Martin Ueding Nov 3 '12 at 11:04 3 @Cawas: A real use case for disable is when you pipe the output to a file; while tools like cat may support colors, it is generally better to not print color information to files. – Sebastian Mach Apr 9 '14 at 6:27 1 @AlexanderSimko, here's a ctypes code snippet to enable VT100 support in Windows 10: import ctypes; kernel32 = ctypes.WinDLL('kernel32'); hStdOut = kernel32.GetStdHandle(-11); mode = ctypes.c_ulong(); kernel32.GetConsoleMode(hStdOut, ctypes.byref(mode)); mode.value |= 4; kernel32.SetConsoleMode(hStdOut, mode). – Eryk Sun Sep 1 '16 at 23:38 | show 15 more comments"
7,"
Q: How do I download a file over HTTP using Python? ","446 In Python 2, use urllib2 which comes with the standard library. import urllib2 response = urllib2.urlopen('http://www.example.com/') html = response.read() This is the most basic way to use the library, minus any error handling. You can also do more complex stuff such as changing headers. The documentation can be found here. share|| follow |||| edited Jun 5 '14 at 11:06 Deep LF 1691212 bronze badges answered Aug 22 '08 at 15:38 CoreyCorey 12.7k77 gold badges3333 silver badges3535 bronze badges 11 This won't work if there are spaces in the url you provide. In that case, you'll need to parse the url and urlencode the path. – Jason Sundram Apr 14 '10 at 21:17 91 Here is the Python 3 solution: stackoverflow.com/questions/7243750/… – tommy.carstensen Feb 25 '14 at 12:09 6 Just for reference. The way to urlencode the path is urllib2.quote – André Puel Aug 2 '14 at 2:09 11 @JasonSundram: If there are spaces in it, it isn't a URI. – Zaz Oct 1 '15 at 2:51 1 This does not work on windows with larger files. You need to read all blocks! – Avia Oct 10 '16 at 22:47 | show 1 more comment"
8,"
Q: Print string to text file ","1191 text_file = open(""Output.txt"", ""w"") text_file.write(""Purchase Amount: %s"" % TotalAmount) text_file.close() If you use a context manager, the file is closed automatically for you with open(""Output.txt"", ""w"") as text_file: text_file.write(""Purchase Amount: %s"" % TotalAmount) If you're using Python2.6 or higher, it's preferred to use str.format() with open(""Output.txt"", ""w"") as text_file: text_file.write(""Purchase Amount: {0}"".format(TotalAmount)) For python2.7 and higher you can use {} instead of {0} In Python3, there is an optional file parameter to the print function with open(""Output.txt"", ""w"") as text_file: print(""Purchase Amount: {}"".format(TotalAmount), file=text_file) Python3.6 introduced f-strings for another alternative with open(""Output.txt"", ""w"") as text_file: print(f""Purchase Amount: {TotalAmount}"", file=text_file) share|| follow |||| edited May 2 '17 at 3:57 answered Mar 7 '11 at 0:34 John La RooyJohn La Rooy 239k4545 gold badges315315 silver badges458458 bronze badges 2 Assuming TotalAmount is an integer, shouldn't the ""%s"" be a ""%d""? – Rui Curado Aug 22 '13 at 10:46 6 @RuiCurado, if TotalAmount is an int, either %d or %s will do the same thing. – John La Rooy Aug 22 '13 at 10:54 2 Great answer. I'm seeing a syntax error with a nearly identical use case: with . . .: print('{0}'.format(some_var), file=text_file) is throwing: SyntaxError: invalid syntax at the equal sign... – nicorellius Apr 6 '16 at 19:51 4 @nicorellius, if you wish to use that with Python2.x you need to put from __future__ import print_function at the top of the file. Note that this will transform all of the print statements in the file to the newer function calls. – John La Rooy Apr 6 '16 at 20:48 To make sure know what the variable type is often convert it to make sure, ex: ""text_file.write('Purchase Amount: %s' % str(TotalAmount))"" which will work with lists, strings, floats, ints, and anything else that is convertable to a string. – EBo Sep 9 '16 at 11:19 | show 4 more comments"
9,"
Q: Python module for converting PDF to text [closed] ","140 Try PDFMiner. It can extract text from PDF files as HTML, SGML or ""Tagged PDF"" format. The Tagged PDF format seems to be the cleanest, and stripping out the XML tags leaves just the bare text. A Python 3 version is available under: https://github.com/pdfminer/pdfminer.six share|| follow |||| edited Jun 1 '19 at 3:12 Felipe Augusto 4,37055 gold badges2121 silver badges4040 bronze badges answered Aug 25 '08 at 5:21 David CrowDavid Crow 14.7k88 gold badges3636 silver badges3434 bronze badges 2 I just added an answer descibing how to use pdfminer as a library. – codeape Nov 24 '08 at 14:21 23 no python 3 support :( – Karl Adler Dec 19 '14 at 16:13 1 The answer I provided in this thread might be useful for people looking at this answer and wondering how to use the library. I give an example on how to use the PDFMiner library to extract text from the PDF. Since the documentation is a bit sparse, I figured it might help a few folks. – DuckPuncher Feb 13 '15 at 16:56 15 regarding python 3, there is a six-based fork pypi.python.org/pypi/pdfminer.six – Denis Cornehl Dec 4 '15 at 10:10 1 sample code at stackoverflow.com/a/26495057/125617 – Renaud Jan 6 '16 at 21:38 | show 3 more comments"
10,"
A: How can I print literal curly-brace characters in python string and also use .format on it?
","1996 You need to double the {{ and }}: >>> x = "" {{ Hello }} {0} "" >>> print(x.format(42)) ' { Hello } 42 ' Here's the relevant part of the Python documentation for format string syntax: Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}. share|| follow |||| edited Jan 17 at 0:14 Boris 3,47744 gold badges3030 silver badges4141 bronze badges answered Mar 29 '11 at 0:08 Greg HewgillGreg Hewgill 754k163163 gold badges10621062 silver badges12081208 bronze badges 255 So if you want to print ""{42}"", you'd use ""{{{0}}}"".format(42) ! – hughes Jul 24 '13 at 20:21 7 What about if you want a single curly brace? ""{ something { } {value}"".format(42) doesn't work. – AJP Oct 2 '13 at 10:10 14 ""{{"".format() and ""}}"".format() print single curly braces. In your example: print ""{{ something {{ }} {0}"".format(42) will print ""{ something { } 42"". – Mark Visser Oct 18 '13 at 21:19 1 What does the {0} mean? – CodyBugstein Feb 21 '14 at 1:27 5 @Imray: {0} refers to the first argument to .format(). You can print more than one value like {0} {1} {2} as long as you give the same number of arguments to .format(). See docs.python.org/library/string.html#format-examples for extensive examples. – Greg Hewgill Feb 21 '14 at 1:30 | show 8 more comments"
11,"
Q: Selenium using Python - Geckodriver executable needs to be in PATH ","336 selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. First of all you will need to download latest executable geckodriver from here to run latest firefox using selenium Actually The Selenium client bindings tries to locate the geckodriver executable from the system PATH. You will need to add the directory containing the executable to the system path. On Unix systems you can do the following to append it to your system’s search path, if you’re using a bash-compatible shell: export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step On Windows you will need to update the Path system variable to add the full directory path to the executable geckodriver manually or command line(don't forget to restart your system after adding executable geckodriver into system PATH to take effect). The principle is the same as on Unix. Now you can run your code same as you're doing as below :- from selenium import webdriver browser = webdriver.Firefox() selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line Exception clearly states you have installed firefox some other location while Selenium is trying to find firefox and launch from default location but it couldn't find. You need to provide explicitly firefox installed binary location to launch firefox as below :- from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary binary = FirefoxBinary('path/to/installed firefox binary') browser = webdriver.Firefox(firefox_binary=binary) share|| follow |||| edited Oct 24 '16 at 3:12 answered Oct 23 '16 at 23:16 Saurabh GaurSaurabh Gaur 19.3k77 gold badges3333 silver badges6161 bronze badges 6 Thanks but I set the my geckodriver.exe on the C:\Python\Python35\selenium directory and I set the path like you described but it's giving me the error below: – tadm123 Oct 24 '16 at 2:57 3 Thanks @Saurabh Gaur, it's working now. I added the path of Firefox to the system variables manually and it's all working. Takes a little bit of time to launch but I'm guessing that's normal. Thanks! – tadm123 Oct 24 '16 at 3:28 3 I got the error ""WebDriverException: Message: Failed to start browser: permission denied"" at first when I started specifying the firefox binary path, but restarting the computer (Windows 10) resolved the problem. - Just in case anyone else is hitting the same problem as me. – NoSuchElephantException Nov 3 '16 at 19:46 2 What is the binary? Does that mean executable? – User Dec 8 '16 at 8:24 12 In addition to this answer, I would like to expand on setting the PATH in unix environment. You can set it in code since you don't need it system wide: os.environ[""PATH""] += os.pathsep + 'path/to/dir/containing/geckodriver/' Or simply keep the geckodriver binary in the directory that is already in your path: mv geckodriver /usr/local/bin – dsalaj Mar 23 '17 at 7:47 | show 8 more comments"
12,"
Q: Combine two columns of text in dataframe in pandas/python ","479 if both columns are strings, you can concatenate them directly: df[""period""] = df[""Year""] + df[""quarter""] If one (or both) of the columns are not string typed, you should convert it (them) first, df[""period""] = df[""Year""].astype(str) + df[""quarter""] Beware of NaNs when doing this! If you need to join multiple string columns, you can use agg: df['period'] = df[['Year', 'quarter', ...]].agg('-'.join, axis=1) Where ""-"" is the separator. share|| follow |||| edited Feb 5 at 5:18 cs95 203k4545 gold badges323323 silver badges392392 bronze badges answered Oct 15 '13 at 10:09 silvadosilvado 9,35822 gold badges2525 silver badges4444 bronze badges 13 Is it possible to add multiple columns together without typing out all the columns? Let's say add(dataframe.iloc[:, 0:10]) for example? – Heisenberg May 9 '15 at 19:15 5 @Heisenberg That should be possible with the Python builtin sum. – silvado May 11 '15 at 11:06 6 @silvado could you please make an example for adding multiple columns? Thank you – c1c1c1 Oct 25 '16 at 16:45 6 Be careful, you need to apply map(str) to all columns that are not string in the first place. if quarter was a number you would do dataframe[""period""] = dataframe[""Year""].map(str) + dataframe[""quarter""].map(str) map is just applying string conversion to all entries. – Ozgur Ozturk Feb 1 '17 at 21:17 10 This solution can create problems iy you have nan values, e careful – user2270655 Dec 27 '17 at 17:14 | show 8 more comments"
13,"
Q: How do I run Python code from Sublime Text 2? ","370 Tools -> Build System -> (choose) Python then: To Run: Tools -> Build -or- Ctrl + B CMD + B (OSX) This would start your file in the console which should be at the bottom of the editor. To Stop: Ctrl + Break or Tools -> Cancel Build Fn + C (OSX) You can find out where your Break key is here: http://en.wikipedia.org/wiki/Break_key. Note: CTRL + C will NOT work. What to do when Ctrl + Break does not work: Go to: Preferences -> Key Bindings - User and paste the line below: {""keys"": [""ctrl+shift+c""], ""command"": ""exec"", ""args"": {""kill"": true} } Now, you can use ctrl+shift+c instead of CTRL+BREAK share|| follow |||| edited Jan 13 '18 at 20:57 Community♦ 111 silver badge answered Dec 18 '11 at 13:49 matiitmatiit 7,32455 gold badges3535 silver badges6464 bronze badges 9 sublime text can also ""auto detect"" the language. So it worked for me to just CTRL + B – Alexis Mar 3 '12 at 4:31 4 Make sure python is in your PATH... the windows installer doesn't seem to do this automagically – SeanJA May 15 '12 at 23:07 2 how do you stop running program (python)? – Ib33X Jun 1 '12 at 15:25 3 My keyboard's break key looks like ""Pause/Break"", so to stop process I also added {""keys"": [""pause""], ""command"": ""exec"", ""args"": {""kill"": true} } – icy Aug 15 '13 at 16:14 3 Having the line {""keys"": [""ctrl+shift+c""], ""command"": ""exec"", ""args"": {""kill"": true} } in User key bindings and using ctrl+shift+c prints that the build is [Cancelled] while the program still being run. – Devi Dec 18 '13 at 10:14 | show 2 more comments"
14,"
A: Is there a way to create multiline comments in Python?
","1786 You can use triple-quoted strings. When they're not a docstring (the first thing in a class/function/module), they are ignored. ''' This is a multiline comment. ''' (Make sure to indent the leading ''' appropriately to avoid an IndentationError.) Guido van Rossum (creator of Python) tweeted this as a ""pro tip"". However, Python's style guide, PEP8, favors using consecutive single-line comments, and this is also what you'll find in many projects. Text editors usually have a shortcut to do this easily. share|| follow |||| edited Feb 3 at 16:56 Peter Mortensen 25.5k2121 gold badges9090 silver badges118118 bronze badges answered Oct 8 '11 at 12:58 Petr ViktorinPetr Viktorin 52.9k55 gold badges6565 silver badges7474 bronze badges 15 Hm. I put a huge multiline string in a python script test.py just to see. When I do import test, a test.pyc file is generated. Unfortunately, the pyc file is huge and contains the entire string as plain text. Am I misunderstanding something, or is this tweet incorrect? – unutbu Oct 8 '11 at 13:18 23 @unutbu, if it was the only thing in the file, it was a docstring. Put some code before it and it'll disappear from the pyc. I edited the answer and put „module“ to the list of things that have docstrings. – Petr Viktorin Oct 8 '11 at 13:21 31 I don't like multiline string as comments. Syntax highlighting marks them as strings, not as comments. I like to use a decent editor that automatically deals with commenting out regions and wrapping multiline comments while I type. Of course, it's a matter of taste. – Sven Marnach Oct 8 '11 at 13:31 61 As a convention I find it helpful to use """""" for docstrings and ''' for block comments. In this manner you can wrap ''' around your usual docstrings without conflict. – Roshambo Dec 18 '12 at 20:03 18 While you can use multi-line strings as multi-line comments, I'm surprised that none of these answers refer to the PEP 8 subsection that specifically recommends constructing multi-line comments from consecutive single-line comments, with blank # lines to distinguish paragraphs. – Air May 21 '14 at 19:32 | show 19 more comments"
15,"
Q: Extracting text from HTML file using Python ","134 html2text is a Python program that does a pretty good job at this. share|| follow |||| edited Jun 30 '18 at 20:54 Alireza Savand 3,16522 gold badges2121 silver badges3636 bronze badges answered Nov 30 '08 at 3:23 RexERexE 13.9k1414 gold badges5151 silver badges7575 bronze badges 5 bit it's gpl 3.0 which means it may be incompatible – frog32 Nov 7 '12 at 10:35 136 Amazing! it's author is RIP Aaron Swartz. – Atul Arvind Aug 10 '13 at 7:42 2 Did anyone find any alternatives to html2text because of GPL 3.0? – jontsai Sep 5 '14 at 1:21 1 GPL not as bad as people want it to be. Aaron knew best. – stevek Oct 13 '14 at 10:59 2 I tried both html2text and nltk but they didn't work for me. I ended up going with Beautiful Soup 4, which works beautifully (no pun intended). – Ryan Apr 30 '15 at 18:58 | show 3 more comments"
16,"
Q: Python argparse: How to insert newline in the help text? ","383 Try using RawTextHelpFormatter: from argparse import RawTextHelpFormatter parser = ArgumentParser(description='test', formatter_class=RawTextHelpFormatter) share|| follow |||| edited Jul 23 '18 at 19:23 akhan 2,25622 gold badges1515 silver badges1010 bronze badges answered Oct 4 '10 at 8:49 Michał KwiatkowskiMichał Kwiatkowski 7,04111 gold badge2121 silver badges2020 bronze badges 6 I think it's not. You could subclass it, but unfortunately Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. So probably not a great idea, although it might not matter, since 2.7 is meant to be the last 2.x python and you'll be expected to refactor lots of things for 3.x anyway. I'm actually running 2.6 with argparse installed via easy_install so that documentation may itself be out of date. – intuited Oct 4 '10 at 9:00 3 Some links: for python 2.7, and python 3.*. The 2.6 package should, according to its wiki, comply with the official 2.7 one. From the doc: ""Passing RawDescriptionHelpFormatter as formatter_class= indicates that description and epilog are already correctly formatted and should not be line-wrapped"" – Stefano Nov 21 '11 at 15:34 80 Try instead formatter_class=RawDescriptionHelpFormatter which only works on description and epilog rather than help text. – MarkHu May 24 '14 at 4:47 3 I have noticed that even with RawTextHelpFormatter, leading and trailing newlines are removed. To work around this, you can simply add two or more consecutive newlines; all but one newline will survive. – MrMas Mar 18 '16 at 16:04 10 You can combine formatters too, e.g. class Formatter( argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter): pass and then formatter_class=Formatter. – Terry Brown Jul 28 '16 at 16:44 | show 2 more comments"
17,"
A: How to print colored text in terminal in Python?
","1789 This somewhat depends on what platform you are on. The most common way to do this is by printing ANSI escape sequences. For a simple example, here's some python code from the blender build scripts: class bcolors: HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' To use code like this, you can do something like print(bcolors.WARNING + ""Warning: No active frommets remain. Continue?"" + bcolors.ENDC) or, with Python3.6+: print(f""{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}"") This will work on unixes including OS X, linux and windows (provided you use ANSICON, or in Windows 10 provided you enable VT100 emulation). There are ansi codes for setting the color, moving the cursor, and more. If you are going to get complicated with this (and it sounds like you are if you are writing a game), you should look into the ""curses"" module, which handles a lot of the complicated parts of this for you. The Python Curses HowTO is a good introduction. If you are not using extended ASCII (i.e. not on a PC), you are stuck with the ascii characters below 127, and '#' or '@' is probably your best bet for a block. If you can ensure your terminal is using a IBM extended ascii character set, you have many more options. Characters 176, 177, 178 and 219 are the ""block characters"". Some modern text-based programs, such as ""Dwarf Fortress"", emulate text mode in a graphical mode, and use images of the classic PC font. You can find some of these bitmaps that you can use on the Dwarf Fortress Wiki see (user-made tilesets). The Text Mode Demo Contest has more resources for doing graphics in text mode. Hmm.. I think got a little carried away on this answer. I am in the midst of planning an epic text-based adventure game, though. Good luck with your colored text! share|| follow |||| edited Dec 10 '19 at 19:35 Richard 37.8k2222 gold badges124124 silver badges193193 bronze badges answered Nov 13 '08 at 19:25 joeldjoeld 20.7k33 gold badges2323 silver badges2222 bronze badges 6 what's the disabled used for? – cregox May 11 '10 at 21:58 3 @Cawas It looks to me like it is for disabling coloring for all colors which are printed using a particular bcolors instance. For example, you could create a bcolors instance and then use the member variables of the instance to print out your coloring characters, but then if you decided you no longer wanted coloring, you could call disable before printing out the characters and they would just print out as empty strings. – Steven Oxley Jun 15 '10 at 18:42 4 On Linux, you might want to use tput, like so since it results in more portable code. – Martin Ueding Nov 3 '12 at 11:04 3 @Cawas: A real use case for disable is when you pipe the output to a file; while tools like cat may support colors, it is generally better to not print color information to files. – Sebastian Mach Apr 9 '14 at 6:27 1 @AlexanderSimko, here's a ctypes code snippet to enable VT100 support in Windows 10: import ctypes; kernel32 = ctypes.WinDLL('kernel32'); hStdOut = kernel32.GetStdHandle(-11); mode = ctypes.c_ulong(); kernel32.GetConsoleMode(hStdOut, ctypes.byref(mode)); mode.value |= 4; kernel32.SetConsoleMode(hStdOut, mode). – Eryk Sun Sep 1 '16 at 23:38 | show 15 more comments"
18,"
Q: Rotate axis text in python matplotlib ","490 This works for me: plt.xticks(rotation=90) share|| follow |||| answered Jun 8 '16 at 16:36 scottlittlescottlittle 9,80244 gold badges3434 silver badges5454 bronze badges 6 In case anyone else is using mpld3, and is frustrated about none of these solutions working. Rotation is not supported in mpld3. – beetree Sep 30 '18 at 0:39 4 Extra points for making these stay inside the figure/window. Mine are half outside. – Gauthier Oct 20 '18 at 13:50 Call this after plotting. Ie, first ax.plot(...) then plt.xticks(rotation=90) – CGFoX Mar 1 at 16:25 add a comment |"
19,"
A: UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not…
","1350 You need to read the Python Unicode HOWTO. This error is the very first example. Basically, stop using str to convert from unicode to encoded text / bytes. Instead, properly use .encode() to encode the string: p.agent_info = u' '.join((agent_contact, agent_telno)).encode('utf-8').strip() or work entirely in unicode. share|| follow |||| edited Oct 4 '17 at 22:59 answered Mar 30 '12 at 12:21 agfagf 133k3030 gold badges253253 silver badges217217 bronze badges 23 agreed! a good rule of thumb I was taught is to use the ""unicode sandwich"" idea. Your script accepts bytes from the outside world, but all processing should be done in unicode. Only when you are ready to output your data should it be mushed back into bytes! – Andbdrew Mar 30 '12 at 12:29 252 In case someone else gets confused by this, I found a strange thing: my terminal uses utf-8, and when I print my utf-8 strings it works nicely. However when I pipe my programs output to a file, it throws a UnicodeEncodeError. In fact, when output is redirected (to a file or a pipe), I find that sys.stdout.encoding is None! Tacking on .encode('utf-8') solves the problem. – drevicko Dec 18 '12 at 8:15 93 @drevicko: use PYTHONIOENCODING=utf-8 instead i.e., print Unicode strings and let the environment to set the expected encoding. – jfs Dec 21 '13 at 3:51 1 @steinar: nothing is valid in every case. In general, a user shouldn't care that you use Python to implement your utility (the interface shouldn't change if you decide to reimplement it in another language for whatever reason) and therefore you should not expect that user even aware about python-specific envvars. It is bad UI to force user to specify character encoding; embed the character encoding in the report format if necessary. Note: no hardcoded encoding can be ""sensible default"" in the general case. – jfs Nov 25 '15 at 10:24 13 This is bad and confusing advice. The reason people use str is because the object IS NOT already a string, so there's no .encode() method to call. – Cerin Oct 5 '16 at 17:59 | show 14 more comments"
20,"
Q: UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <un… ","908 The file in question is not using the CP1252 encoding. It's using another encoding. Which one you have to figure out yourself. Common ones are Latin-1 and UTF-8. Since 0x90 doesn't actually mean anything in Latin-1, UTF-8 (where 0x90 is a continuation byte) is more likely. You specify the encoding when you open the file: file = open(filename, encoding=""utf8"") share|| follow |||| edited Sep 5 '17 at 13:05 fat 4,40033 gold badges3434 silver badges5656 bronze badges answered Feb 10 '12 at 18:53 Lennart RegebroLennart Regebro 133k3636 gold badges197197 silver badges235235 bronze badges 19 Cool, I had that problem with some Python 2.7 code that I tried to run in Python 3.4. Latin-1 worked for me! – 1vand1ng0 Apr 14 '15 at 8:56 2 if you're using Python 2.7, and getting the same error, try the io module: io.open(filename,encoding=""utf8"") – christopherlovell Jun 3 '15 at 14:02 8 @1vand1ng0: of course Latin-1 works; it'll work for any file regardless of what the actual encoding of the file is. That's because all 256 possible byte values in a file have a Latin-1 codepoint to map to, but that doesn't mean you get legible results! If you don't know the encoding, even opening the file in binary mode instead might be better than assuming Latin-1. – Martijn Pieters♦ Mar 6 '17 at 14:10 1 It is unicode by default, but unicode is not an encoding. regebro.wordpress.com/2011/03/23/… – Lennart Regebro Feb 16 '18 at 16:16 1 filename = ""C:\Report.txt"" with open(filename,encoding =""utf8"") as my_file: text = my_file.read() print(text) even after using this I am getting the same error. I have also tried with other encoding but all in vain. In this code I am also using from geotext import GeoText. Please suggest a solution. – Salah Jun 4 '18 at 14:37 | show 8 more comments"
21,"
Q: Python Text Editor(Very Basic) ","2 The error is pretty explicit: TypeError: coercing to Unicode: need string or buffer, NoneType found More specifically, that ""NoneType"" found indicates that the open method was expecting a unicode string, but you handed it None. Looks like you're hitting a code path where the global filename variable (terrible choice to use a global, by the way) isn't set. I'm not going to debug for you but this should give you a pointer in the right direction. It's just a bug in your code, not an issue with Tkinter or the interpreter. share|| follow |||| answered Jan 4 '16 at 6:55 Chris TavaresChris Tavares 22.1k33 gold badges3939 silver badges6060 bronze badges add a comment |"
22,"
Q: Python script to copy text to clipboard [duplicate] ","166 See Pyperclip. Example (taken from Pyperclip site): import pyperclip pyperclip.copy('The text to be copied to the clipboard.') spam = pyperclip.paste() Also, see Xerox. But it appears to have more dependencies. share|| follow |||| edited Oct 4 '18 at 17:15 vauhochzett 38833 silver badges1717 bronze badges answered Jun 16 '12 at 12:35 robertrobert 26.4k88 gold badges4848 silver badges6868 bronze badges 2 I tried it on my system, and .setcb doesn't work, but .copy does. I'm using pyperclip 1.5.4 on py 2.7. Just in case someone runs into the same problems - and @robert, I'd love to hear why this syntax works on your system but doesn't on mine. – Vincent Tjeng Sep 28 '14 at 18:20 1 .copy seems to be the offical one. github.com/asweigart/pyperclip – fnkr Oct 17 '14 at 19:50 @VincentTjeng updated – robert Oct 24 '14 at 20:59 If the copied text doesn't persist after running from the script, refer to this issue for the solution. – xtluo Jun 10 '19 at 7:28 add a comment |"
23,"
A: Print string to text file
","1191 text_file = open(""Output.txt"", ""w"") text_file.write(""Purchase Amount: %s"" % TotalAmount) text_file.close() If you use a context manager, the file is closed automatically for you with open(""Output.txt"", ""w"") as text_file: text_file.write(""Purchase Amount: %s"" % TotalAmount) If you're using Python2.6 or higher, it's preferred to use str.format() with open(""Output.txt"", ""w"") as text_file: text_file.write(""Purchase Amount: {0}"".format(TotalAmount)) For python2.7 and higher you can use {} instead of {0} In Python3, there is an optional file parameter to the print function with open(""Output.txt"", ""w"") as text_file: print(""Purchase Amount: {}"".format(TotalAmount), file=text_file) Python3.6 introduced f-strings for another alternative with open(""Output.txt"", ""w"") as text_file: print(f""Purchase Amount: {TotalAmount}"", file=text_file) share|| follow |||| edited May 2 '17 at 3:57 answered Mar 7 '11 at 0:34 John La RooyJohn La Rooy 239k4545 gold badges315315 silver badges458458 bronze badges 2 Assuming TotalAmount is an integer, shouldn't the ""%s"" be a ""%d""? – Rui Curado Aug 22 '13 at 10:46 6 @RuiCurado, if TotalAmount is an int, either %d or %s will do the same thing. – John La Rooy Aug 22 '13 at 10:54 2 Great answer. I'm seeing a syntax error with a nearly identical use case: with . . .: print('{0}'.format(some_var), file=text_file) is throwing: SyntaxError: invalid syntax at the equal sign... – nicorellius Apr 6 '16 at 19:51 4 @nicorellius, if you wish to use that with Python2.x you need to put from __future__ import print_function at the top of the file. Note that this will transform all of the print statements in the file to the newer function calls. – John La Rooy Apr 6 '16 at 20:48 To make sure know what the variable type is often convert it to make sure, ex: ""text_file.write('Purchase Amount: %s' % str(TotalAmount))"" which will work with lists, strings, floats, ints, and anything else that is convertable to a string. – EBo Sep 9 '16 at 11:19 | show 4 more comments"
24,"
Q: Python concatenate text files ","247 This should do it For large files: filenames = ['file1.txt', 'file2.txt', ...] with open('path/to/output/file', 'w') as outfile: for fname in filenames: with open(fname) as infile: for line in infile: outfile.write(line) For small files: filenames = ['file1.txt', 'file2.txt', ...] with open('path/to/output/file', 'w') as outfile: for fname in filenames: with open(fname) as infile: outfile.write(infile.read()) … and another interesting one that I thought of: filenames = ['file1.txt', 'file2.txt', ...] with open('path/to/output/file', 'w') as outfile: for line in itertools.chain.from_iterable(itertools.imap(open, filnames)): outfile.write(line) Sadly, this last method leaves a few open file descriptors, which the GC should take care of anyway. I just thought it was interesting share|| follow |||| edited Aug 16 '13 at 6:06 answered Nov 28 '12 at 19:57 inspectorG4dgetinspectorG4dget 86.8k1919 gold badges113113 silver badges208208 bronze badges 9 This will, for large files, be very memory inefficient. – Gareth Latty Nov 28 '12 at 20:06 1 @inspectorG4dget: I wasn't asking you, I was asking eyquem, who complained that your solution wasn't going to be efficient. I'm willing to bet it's more than efficient enough for the OP's use case, and for whatever use case eyquem has in mind. If he thinks it isn't, it's his responsibility to prove that before demanding that you optimize it. – abarnert Nov 28 '12 at 21:16 1 what are we considering a large file to be? – Dee Aug 2 '15 at 22:55 2 @dee: a file so large that it's contents don't fit into main memory – inspectorG4dget Aug 2 '15 at 22:59 5 Just to reiterate: this is the wrong answer, shutil.copyfileobj is the right answer. – Paul Crowley Apr 5 '17 at 17:05 | show 13 more comments"
25,"
Q: How do I find an element that contains specific text in Selenium Webdriver (Python)? ","314 Try the following: driver.find_elements_by_xpath(""//*[contains(text(), 'My Button')]"") share|| follow |||| answered Sep 9 '13 at 14:54 RickyRicky 17.8k44 gold badges3535 silver badges2626 bronze badges 3 Thank you for the reply, it was 50% of what I needed (got me started). The form I arrived to is this ""(//*[contains(text(), '"" + text + ""')] | //*[@value='"" + text + ""'])"" it will search for given text not only inside element nodes, but also inside input elements whose text was set via 'value' attribute i.e. <button value=""My Button"" /> . Though do note, the value must be strict match, not just contain the text. – Ivan Koshelev Oct 11 '14 at 20:37 9 Also worth mentioning for other search engine visitors: if you're looking for a link, there are find_element(s)_by_link_text and find_element(s)_by_partial_link_text methods – Dan Passaro Nov 17 '14 at 23:45 3 What if the text is dynamic? That is, might contain quotes. Wouldn't that break this solution? – IcedDante Jul 8 '15 at 17:59 3 Searching for certain names seems to break this. Take the following for an example: ""//*[contains(text(), '""+username+""')]"" if username = ""O'Reilly""; then the xpath would become invalid. Is there a way around this? – Sakamoto Kazuma Jun 7 '16 at 19:37 It doesn't seem to work when the target text has multiple lines. – Shawn Nov 20 '16 at 18:25 | show 1 more comment"
26,"
Q: Are dictionaries ordered in Python 3.6+? ","486 Are dictionaries ordered in Python 3.6+? They are insertion ordered[1]. As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. This is considered an implementation detail in Python 3.6; you need to use OrderedDict if you want insertion ordering that's guaranteed across other implementations of Python (and other ordered behavior[1]). As of Python 3.7, this is no longer an implementation detail and instead becomes a language feature. From a python-dev message by GvR: Make it so. ""Dict keeps insertion order"" is the ruling. Thanks! This simply means that you can depend on it. Other implementations of Python must also offer an insertion ordered dictionary if they wish to be a conforming implementation of Python 3.7. How does the Python 3.6 dictionary implementation perform better[2] than the older one while preserving element order? Essentially, by keeping two arrays. The first array, dk_entries, holds the entries (of type PyDictKeyEntry) for the dictionary in the order that they were inserted. Preserving order is achieved by this being an append only array where new items are always inserted at the end (insertion order). The second, dk_indices, holds the indices for the dk_entries array (that is, values that indicate the position of the corresponding entry in dk_entries). This array acts as the hash table. When a key is hashed it leads to one of the indices stored in dk_indices and the corresponding entry is fetched by indexing dk_entries. Since only indices are kept, the type of this array depends on the overall size of the dictionary (ranging from type int8_t(1 byte) to int32_t/int64_t (4/8 bytes) on 32/64 bit builds) In the previous implementation, a sparse array of type PyDictKeyEntry and size dk_size had to be allocated; unfortunately, it also resulted in a lot of empty space since that array was not allowed to be more than 2/3 * dk_size full for performance reasons. (and the empty space still had PyDictKeyEntry size!). This is not the case now since only the required entries are stored (those that have been inserted) and a sparse array of type intX_t (X depending on dict size) 2/3 * dk_sizes full is kept. The empty space changed from type PyDictKeyEntry to intX_t. So, obviously, creating a sparse array of type PyDictKeyEntry is much more memory demanding than a sparse array for storing ints. You can see the full conversation on Python-Dev regarding this feature if interested, it is a good read. In the original proposal made by Raymond Hettinger, a visualization of the data structures used can be seen which captures the gist of the idea. For example, the dictionary: d = {'timmy': 'red', 'barry': 'green', 'guido': 'blue'} is currently stored as [keyhash, key, value]: entries = [['--', '--', '--'], [-8522787127447073495, 'barry', 'green'], ['--', '--', '--'], ['--', '--', '--'], ['--', '--', '--'], [-9092791511155847987, 'timmy', 'red'], ['--', '--', '--'], [-6480567542315338377, 'guido', 'blue']] Instead, the data should be organized as follows: indices = [None, 1, None, None, None, 0, None, 2] entries = [[-9092791511155847987, 'timmy', 'red'], [-8522787127447073495, 'barry', 'green'], [-6480567542315338377, 'guido', 'blue']] As you can visually now see, in the original proposal, a lot of space is essentially empty to reduce collisions and make look-ups faster. With the new approach, you reduce the memory required by moving the sparseness where it's really required, in the indices. [1]: I say ""insertion ordered"" and not ""ordered"" since, with the existence of OrderedDict, ""ordered"" suggests further behavior that the dict object doesn't provide. OrderedDicts are reversible, provide order sensitive methods and, mainly, provide an order-sensive equality tests (==, !=). dicts currently don't offer any of those behaviors/methods. [2]: The new dictionary implementations performs better memory wise by being designed more compactly; that's the main benefit here. Speed wise, the difference isn't so drastic, there's places where the new dict might introduce slight regressions (key-lookups, for example) while in others (iteration and resizing come to mind) a performance boost should be present. Overall, the performance of the dictionary, especially in real-life situations, improves due to the compactness introduced. share|| follow |||| edited Nov 21 '19 at 7:49 answered Oct 11 '16 at 15:17 Dimitris Fasarakis HilliardDimitris Fasarakis Hilliard 99.6k2323 gold badges200200 silver badges206206 bronze badges 14 So, what happens when an item is removed? is the entries list resized? or is a blank space kept? or is it compressed from time to time? – njzk2 Oct 11 '16 at 19:19 17 @njzk2 When an item is removed, the corresponding index is replaced by DKIX_DUMMY with a value of -2 and the entry in the entry array replaced by NULL, when inserting is performed the new values are appended to the entries array, Haven't been able to discern yet, but pretty sure when the indices fills up beyond the 2/3 threshold resizing is performed. This can lead to shrinking instead of growing if many DUMMY entries exist. – Dimitris Fasarakis Hilliard Oct 11 '16 at 20:03 3 @Chris_Rands Nope, the only actual regression I've seen is on the tracker in a message by Victor. Other than that microbenchmark, I've seen no other issue/message indicating a serious speed difference in real-life work loads. There's places where the new dict might introduce slight regressions (key-lookups, for example) while in others (iteration and resizing come to mind) a performance boost would be present. – Dimitris Fasarakis Hilliard Mar 14 '17 at 13:26 3 Correction on the resizing part: Dictionaries don't resize when you delete items, they re-calculate when you re-insert. So, if a dict is created with d = {i:i for i in range(100)} and you .pop all items w/o inserting, the size won't change. When you add to it again, d[1] = 1, the appropriate size is calculated and the dict resizes. – Dimitris Fasarakis Hilliard Aug 2 '17 at 19:47 6 @Chris_Rands I'm pretty sure it is staying. The thing is, and the reason why I changed my answer to remove blanket statements about 'dict being ordered', dicts aren't ordered in the sense OrderedDicts are. The notable issue is equality. dicts have order insensitive ==, OrderedDicts have order sensitive ones. Dumping OrderedDicts and changing dicts to now have order sensitive comparisons could lead to a lot of breakage in old code. I'm guessing the only thing that might change about OrderedDicts is its implementation. – Dimitris Fasarakis Hilliard Apr 10 '18 at 16:57 | show 10 more comments"
27,"
Q: How do I print bold text in Python? ","338 class color: PURPLE = '\033[95m' CYAN = '\033[96m' DARKCYAN = '\033[36m' BLUE = '\033[94m' GREEN = '\033[92m' YELLOW = '\033[93m' RED = '\033[91m' BOLD = '\033[1m' UNDERLINE = '\033[4m' END = '\033[0m' print(color.BOLD + 'Hello World !' + color.END) share|| follow |||| edited Aug 28 '19 at 0:07 FraggaMuffin 2,4261414 silver badges2222 bronze badges answered Jun 25 '13 at 17:10 BoubakrBoubakr 4,01311 gold badge1515 silver badges1616 bronze badges 9 I like the way you didn't just did bold, but created a whole class for them to reference and to help all users viewing. Thank you. – GreenHawk1220 Dec 9 '16 at 21:43 3 I'm gonna use this as dict, not a class. Thanks! – dvlden Feb 27 '17 at 20:49 6 It is printing the following : [1mHello World ![0m – user8167727 Jul 12 '17 at 17:59 3 what about python3? this seams not to work in p3. – mm_ Mar 17 '19 at 6:14 Working on my setup: ubuntu 19.10, python 3.7 – Elouan Keryell-Even Mar 13 at 16:03 | show 1 more comment"
28,"
Q: How to split a large text file into smaller files with equal number of lines? ","840 Have you looked at the split command? $ split --help Usage: split [OPTION] [INPUT [PREFIX]] Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default size is 1000 lines, and default PREFIX is `x'. With no INPUT, or when INPUT is -, read standard input. Mandatory arguments to long options are mandatory for short options too. -a, --suffix-length=N use suffixes of length N (default 2) -b, --bytes=SIZE put SIZE bytes per output file -C, --line-bytes=SIZE put at most SIZE bytes of lines per output file -d, --numeric-suffixes use numeric suffixes instead of alphabetic -l, --lines=NUMBER put NUMBER lines per output file --verbose print a diagnostic to standard error just before each output file is opened --help display this help and exit --version output version information and exit You could do something like this: split -l 200000 filename which will create files each with 200000 lines named xaa xab xac ... Another option, split by size of output file (still splits on line breaks): split -C 20m --numeric-suffixes input_filename output_prefix creates files like output_prefix01 output_prefix02 output_prefix03 ... each of max size 20 megabytes. share|| follow |||| edited May 30 '18 at 11:10 Denilson Sá Maia 36.6k2727 gold badges9595 silver badges100100 bronze badges answered Jan 6 '10 at 22:44 Mark ByersMark Byers 659k150150 gold badges14451445 silver badges13731373 bronze badges 15 you can also split a file by size: split -b 200m filename (m for megabytes, k for kilobytes or no suffix for bytes) – Abhi Beckert Jun 24 '11 at 7:55 136 split by size and ensure files are split on line breaks: split -C 200m filename – Clayton Stanley Dec 13 '12 at 2:12 2 split produces garbled output with Unicode (UTF-16) input. At least on Windows with the version I have. – Vertigo May 24 '13 at 7:57 4 @geotheory, be sure to follow LeberMac's advice earlier in the thread about first converting CR (Mac) line endings to LR (Linux) line endings using TextWrangler or BBEdit. I had the exact same problem as you until I found that piece of advice. – sstringer Aug 25 '13 at 20:00 6 -d option is not available on OSX, use gsplit instead. Hope this useful for Mac user. – user5698801 Jul 23 '17 at 11:54 | show 7 more comments"
29,"
Q: Turn counter for python text adventure ","1 Without seeing an example of your code, it's pretty much impossible to tell you anything specific that will work in your code. But I can give you something general that you can probably adapt to fit your code. class CountedInput(object): def __init__(self): self.counter = 0 def input(self, *args): self.counter += 1 return input(*args) counted_input = CountedInput() Now, anywhere in your code that you call input(), you instead call counted_input.input(). And when you want to display the turn counter, that's just counted_input.counter. (If you're using Python 2.x, change input to raw_input.) Now that you've added an example to the question: This suggestion will work just fine as it is, but you can make things even simpler. Your whole game is built around a command loop. You call input exactly once per loop. So, all you need to do is count how many times you go around that loop. You can do that like this: counter = 0 while True: counter += 1 playerInput = input(""What do you want to do? "") # all the rest of your code And now, you just print out or otherwise use counter the same as any other variable. For example: elif playerInput == ""score"": print(""You have 0/0 points after"", counter, ""turns"") (I'm guessing that you don't actually want to troll your players with a score command when you don't keep score, but that should show you the ideal.) If you want to get clever, there's an even simpler way to do this: Just loop over all the numbers from 1 to infinity. How? The count function, which works kind of like range except there's no stop value because it never stops: from itertools import count for counter in count(1): # the rest of your code share|| follow |||| edited Nov 21 '14 at 1:27 answered Nov 21 '14 at 1:10 abarnertabarnert 285k2828 gold badges432432 silver badges527527 bronze badges oh sorry I didn't think you would need to see it, im still a newbie – kebab Nov 21 '14 at 1:12 @kebab: No problem. It would be a good idea to read the Help on this site; there's some great information on how to ask good questions. But meanwhile, I've updated my answer to handle your more specific problem (and as you can see, it's a lot simpler this way, which is part of the reason it's good to ask more specific questions). – abarnert Nov 21 '14 at 1:28 yea I have its only my third day using the site and only my first semester of python so I do appreciate that you still bare with me while im learning, very much appreciated – kebab Nov 21 '14 at 1:30 Your first suggestion after seeing my code works perfectly, thanks so much! – kebab Nov 21 '14 at 1:35 @kebab: Most people on this site are so frustrated with newbies who have no interest in learning how it works that they'll gladly bear with anyone who obviously wants to learn like you do. :) – abarnert Nov 21 '14 at 1:43 add a comment |"
30,"
Q: Python Text Encoding ","5 Yes, you need to know the encoding of the text file to turn in into a unicode string (from the bytes that make up the file). For example, if you know the encoding is UTF-8: with open('foo.txt', 'rb') as f: contents = f.read().decode('utf-8-sig') # -sig takes care of BOM if present The text in your file seems not to be encoded Unicode, however; the accented character is apparently stored as an XML entity, which will have to be converted manually (tip of the hat to jleedev for the link). share|| follow |||| edited May 23 '17 at 11:47 Community♦ 111 silver badge answered Dec 16 '10 at 6:38 CameronCameron 78.7k1818 gold badges169169 silver badges212212 bronze badges what's BOM (in context of -sig) ? – Srikar Appalaraju Dec 16 '10 at 6:46 2 @MovieYoda: Ah, check out this article. Basically, when it takes multiple bytes together to represent a single character (as can be the case with UTF-8), those bytes could be interpreted in the a different order than intended (this order is called endianness). Because of this, a special unambiguous (and optional, in the case of UTF-8) mark is placed at the beginning of the file to indicate the endianness of the file. -sig removes the BOM if it's present so you don't get the marker appearing as part of your unicode string. – Cameron Dec 16 '10 at 6:51 add a comment |"
31,"
Q: Sending HTML email using Python ","413 From Python v2.7.14 documentation - 18.1.11. email: Examples: Here’s an example of how to create an HTML message with an alternative plain text version: #! /usr/bin/python import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # me == my email address # you == recipient's email address me = ""[email protected]"" you = ""[email protected]"" # Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart('alternative') msg['Subject'] = ""Link"" msg['From'] = me msg['To'] = you # Create the body of the message (a plain-text and an HTML version). text = ""Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"" html = """"""\ <html> <head></head> <body> <p>Hi!<br> How are you?<br> Here is the <a href=""http://www.python.org"">link</a> you wanted. </p> </body> </html> """""" # Record the MIME types of both parts - text/plain and text/html. part1 = MIMEText(text, 'plain') part2 = MIMEText(html, 'html') # Attach parts into message container. # According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred. msg.attach(part1) msg.attach(part2) # Send the message via local SMTP server. s = smtplib.SMTP('localhost') # sendmail function takes 3 arguments: sender's address, recipient's address # and message to send - here it is sent as one string. s.sendmail(me, you, msg.as_string()) s.quit() share|| follow |||| edited Mar 6 '18 at 16:27 community wiki 3 revs, 3 users 96%Andrew Hare 1 Is it possible to attach a third and a fourth part, both of which are attachments (one ASCII, one binary)? How would one do that? Thanks. – Hamish Grubijan Dec 5 '10 at 20:55 1 Hi, I noticed that in the end you quit the s object. What if I want to send multiple messages? Should I quit everytime I send the message or send them all (in a for loop) and then quit once and for all? – xpanta May 9 '12 at 9:58 Make sure to attach html last, as the preferred(showing) part will be the one attached last. # According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred. I wish i read this 2hrs ago – dwkd Jul 4 '15 at 21:44 1 Warning: this fails if you have non-ascii characters in the text. – guettli Apr 4 '16 at 8:23 2 Hmm, I get the error for msg.as_string(): list object has no attribute encode – JohnAndrews Feb 11 '19 at 14:44 | show 6 more comments"
32,"
Q: Python: Text saving issue ","2 To join a list of strings with newlines, use the join method: '\n'.join(['first line', 'second line', 'third line']) To write a string to a file: with open('output.txt', 'w') as f: f.write(some_string) share|| follow |||| answered Dec 30 '13 at 16:23 LynnLynn 7,8333333 silver badges5858 bronze badges Thank you, i think I now have an idea of how I would read the file. – user3146876 Dec 30 '13 at 16:42 add a comment |"
33,"
Q: Python - Text file into dictionary ","2 Let's process the file line per line, create a dictionary, split the line, and create entries, converting text to floats in the process (commas have to be replaced by dots) test=""""""Num Date____ Bank Type_______ NCu CCu Buy___________ Sell__________ 001 20161130 NLB_ individuals 840 USD 0001,074800000 0001,050800000 001 20161130 NLB_ individuals 840 CHF 0002,074800000 0004,050800000 """""".splitlines().__iter__() d = dict() next(test) # skip title line for line in test: toks = line.split() if len(toks)==8: d[toks[5]] = (float(toks[6].replace("","",""."")),float(toks[7].replace("","","".""))) print(d) result: {'USD': (1.0748, 1.0508), 'CHF': (2.0748, 4.0508)} That's just for a standalone test. When running with a file, just replace the string by: with open(""input.txt"") as test: and it will act the same, but with a file as input. (or use the csv module but 1) that seems to be overkill for your case, and 2) we don't know if the separators are tabs, space, or more than 1 space/tab. str.split handles that too so I'd stick with it until fields containing spaces appear :)) share|| follow |||| answered Dec 2 '16 at 13:31 Jean-François Fabre♦Jean-François Fabre 119k1111 gold badges8181 silver badges141141 bronze badges add a comment |"
34,"
Q: Python text processing/finding data ","1 file = open(""Register.txt"", ""rt"") lines = file.readlines() file.close() for indx, line in enumerate(lines): line = line.strip() print (line) if line.find('Per End') != -1: print lines[indx-1].strip() if line.find('File:') != -1: print lines[indx+1].strip() enumerate(lines) gives access to indices and line as well, there by you can access previous and next lines as well here is my stdout directly ran in python shell: >>> file = open(""r.txt"", ""rt"") >>> lines = file.readlines() >>> file.close() >>> lines ['SMITH, John\n', 'Per End: 12/10/2016\n', 'File:\n', '002013\n', 'Dept:\n', '000400\n', 'Rate:10384 60\n'] >>> for indx, line in enumerate(lines): ... line = line.strip() ... if line.find('Per End') != -1: ... print lines[indx-1].strip() ... if line.find('File:') != -1: ... print lines[indx+1].strip() SMITH, John 002013 share|| follow |||| edited Jun 20 '17 at 17:34 answered Jun 20 '17 at 16:12 be_good_do_goodbe_good_do_good 3,16733 gold badges2323 silver badges3737 bronze badges I try this code and I get this error: TypeError: 'builtin_function_or_method' object has no attribute 'getitem' This will need to run off of a text file with multiple employees – Jason Jabbour Jun 20 '17 at 17:22 yes, this did work for me for a text file with multiple employees. I created a sample text file based on your sample data in the question – be_good_do_good Jun 20 '17 at 17:29 Thanks. But how can I get around the error? "" TypeError: 'builtin_function_or_method' object has no attribute 'getitem' "" – Jason Jabbour Jun 20 '17 at 17:31 I found my mistake, corrected it, then received this error in it's stead: "" print line[indx+1].strip() Per End: 12/10/2016 IndexError: string index out of range"" The code is exact to yours at this point. Not sure how to get around it. – Jason Jabbour Jun 20 '17 at 17:34 how does your lines list look like? can you print it like I did it in my shell and put it here? – be_good_do_good Jun 20 '17 at 17:38 | show 8 more comments"
35,"
Q: Unicode (UTF-8) reading and writing to files in Python ","107 In the notation u'Capit\xe1n\n' the ""\xe1"" represents just one byte. ""\x"" tells you that ""e1"" is in hexadecimal. When you write Capit\xc3\xa1n into your file you have ""\xc3"" in it. Those are 4 bytes and in your code you read them all. You can see this when you display them: >>> open('f2').read() 'Capit\\xc3\\xa1n\n' You can see that the backslash is escaped by a backslash. So you have four bytes in your string: ""\"", ""x"", ""c"" and ""3"". Edit: As others pointed out in their answers you should just enter the characters in the editor and your editor should then handle the conversion to UTF-8 and save it. If you actually have a string in this format you can use the string_escape codec to decode it into a normal string: In [15]: print 'Capit\\xc3\\xa1n\n'.decode('string_escape') Capitán The result is a string that is encoded in UTF-8 where the accented character is represented by the two bytes that were written \\xc3\\xa1 in the original string. If you want to have a unicode string you have to decode again with UTF-8. To your edit: you don't have UTF-8 in your file. To actually see how it would look like: s = u'Capit\xe1n\n' sutf8 = s.encode('UTF-8') open('utf-8.out', 'w').write(sutf8) Compare the content of the file utf-8.out to the content of the file you saved with your editor. share|| follow |||| edited Jan 29 '09 at 18:44 answered Jan 29 '09 at 15:11 unbeknownunbeknown So, what's the point of the utf-8 encoded format if python can read in files using it? In other words, is there any ascii representation that python will read in \xc3 as 1 byte? – Gregg Lind Jan 29 '09 at 16:51 4 The answer to your ""So, what's the point…"" question is ""Mu."" (since Python can read files encoded in UTF-8). For your second question: \xc3 is not part of the ASCII set. Perhaps you mean ""8-bit encoding"" instead. You are confused about Unicode and encodings; it's ok, many are. – tzot Jan 30 '09 at 12:16 8 Try reading this as a primer: joelonsoftware.com/articles/Unicode.html – tzot Jan 30 '09 at 12:16 note: u'\xe1' is one Unicode codepoint U+00e1 that can be represented using 1 or more bytes depending on character encoding (it is 2 bytes in utf-8). b'\xe1' is one byte (a number 225), what letter if any it can represent depends on character encoding used to decode it e.g., it is б (U+0431) in cp1251, с (U+0441) in cp866, etc. – jfs Jun 15 '13 at 6:31 11 It is amazing how many British coders say ""just use ascii"" and then fail to realise that the £ sign is not it. Most are not aware that ascii!=local code page (ie latin1). – Danny Staple Sep 5 '13 at 12:58 add a comment |"
36,"
Q: How to read a text file into a list or an array with Python ","159 python's file.readlines() method returns a list of the lines in the file: f = open('file_name.ext', 'r') x = f.readlines() f.close() Now you should be able to iterate through the array of lines x. If you want to use the file and not have to remember to close it afterward, do this: with open('file_name.ext', 'r') as f: x = f.readlines() share|| follow |||| edited Jan 6 at 17:44 mightypile 5,31722 gold badges2727 silver badges3232 bronze badges answered Jun 18 '14 at 11:31 user3752281user3752281 1,78711 gold badge99 silver badges1515 bronze badges 65 You might want to replace the last line with x = f.read().splitlines() to avoid including the newlines (\n) - as mentioned in this post: stackoverflow.com/a/20756176/436794 – Pierz Sep 6 '17 at 16:08 Don't forget to close the file! – Blairg23 Mar 14 '18 at 4:55 4 Anyone skimming this - pay attention to Pierz above and don't waste an hour wondering why your split() to an array was failing due to the \n causing a potentially wrong-numbered list of substrings! – volvox Apr 26 '18 at 20:28 In what way does this address the actual question? This is answering the question ""How do I read the lines of a file in Python"", and quite poorly, I might add. – AMC Feb 15 at 1:34 add a comment |"
37,"
Q: Python text file modification ","0 You can use a combination of the fileinput and re modules. fileinput.input will be used for in-place file editing re.match and match.group will be used for capturing the text inside the brackets Here's the code (I've placed the comments inline): def modify_text_inside_brackets(file_name, tag): with fileinput.input(files=(file_name), inplace=True) as text_file: for line in text_file: # split the line into groups matches = re.match(r""(.*)(\[.*\])(.*)"", line) if matches: # get the text inside the brackets # group(0): the entire matching line # group(1): everything before the opening [ # group(2): [ .. ] # group(3): everything after the closing ] text_inside_brackets = matches.group(2)[1:-1] # create a new [..] string with the prepended tag modified_text = ""[{},{}]"".format(tag, text_inside_brackets.strip()) # replace the original [..] with the modified [..] modified_line = line.replace(matches.group(2), modified_text) # print out the modified line to the file print(modified_line, end="""") else: print(line, end="""") modify_text_inside_brackets(""input.txt"", ""TAG"") Given this ""input.txt"": [ 123,456] [ city is beautiful] This text has no brackets and should be unchanged. It can also [ handle lines with inline] brackets. The code will modify it like this: [TAG,123,456] [TAG,city is beautiful] This text has no brackets and should be unchanged. It can also [TAG,handle lines with inline] brackets. Notes: fileinput.input with inplace=True redirects the print output to the file It's a unclear what <mystring> and <newtag> is from your question, so I just used TAG. Modify ""[{},{}]"".format(tag, text_inside_brackets.strip()) based on the output format you need. For example, in your question, the spaces after [ and before ] are imbalanced and inconsistent in your examples, so add or strip() spaces as needed. You can check/test the regex itself from this demo. I passed end="""" to print because by default print adds a newline. share|| follow |||| edited May 2 '18 at 2:58 answered May 2 '18 at 2:47 Gino MempinGino Mempin 6,7091010 gold badges3232 silver badges3838 bronze badges add a comment |"
38,"
Q: How can I find all matches to a regular expression in Python? ","527 Use re.findall or re.finditer instead. re.findall(pattern, string) returns a list of matching strings. re.finditer(pattern, string) returns an iterator over MatchObject objects. Example: re.findall( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats') # Output: ['cats', 'dogs'] [x.group() for x in re.finditer( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats')] # Output: ['all cats are', 'all dogs are'] share|| follow |||| edited Jan 21 at 17:32 Mike Fogel 2,4401919 silver badges1919 bronze badges answered Jan 15 '11 at 3:54 AmberAmber 404k6666 gold badges566566 silver badges512512 bronze badges 13 finditer was what I was looking for. I'm surprised that one returns Match objects and the other strings. I was expecting to use a match_all or match_iter function. – dsclose Dec 7 '15 at 13:55 21 DISCLAIMER: those will find only non-overlapping matches – Antoine Lizée Sep 10 '16 at 22:16 3 @AntoineLizée, how does one find iterations WITH overlap? – Raksha May 10 '17 at 23:49 16 @Raksha - Use re.search in a loop. It'll return a Match object. You'll want to pass in Match.start() + 1 as the pos argument for re.search for the next iteration of the loop. – ArtOfWarfare May 31 '17 at 17:31 3 If the match contains more than one group, findall will return a list of matching tuples, not a list of matching strings. – rodorgas Jun 12 '18 at 3:55 | show 2 more comments"
39,"
Q: Python text file to xml ","2 I think easiest way would be to change your file into csv file like this: Program,Start,Duration,Title 5,2013-09-11 05:30:00,06:15:00,INFOCANALE And then convert it like: from lxml import etree import csv root = etree.Element('data') rdr = csv.reader(open(""your file name here"")) header = rdr.next() for row in rdr: eg = etree.SubElement(root, 'eg') for h, v in zip(header, row): etree.SubElement(eg, h).text = v f = open(r""C:\temp\data2.xml"", ""w"") f.write(etree.tostring(root)) f.close() # you also can use # etree.ElementTree(root).write(open(r""C:\temp\data2.xml"", ""w"")) share|| follow |||| edited Sep 11 '13 at 19:01 answered Sep 11 '13 at 13:03 Roman PekarRoman Pekar 83.1k2525 gold badges151151 silver badges170170 bronze badges Traceback (most recent call last): File ""./epg.py"", line 53, in <module> etree.SubElement(eg, h).text = v File ""lxml.etree.pyx"", line 2659, in lxml.etree.SubElement (src/lxml/lxml.etree.c:53668) File ""apihelpers.pxi"", line 204, in lxml.etree._makeSubElement (src/lxml/lxml.etree.c:12230) File ""apihelpers.pxi"", line 1542, in lxml.etree._tagValidOrRaise (src/lxml/lxml.etree.c:23956) ValueError: Invalid tag name u' Program 10 ' – Marin Sep 11 '13 at 14:01 @car have you converted your file into csv? could you put a few strings as an example? – Roman Pekar Sep 11 '13 at 17:26 No i get errors when i wanna convert it, but I'll try with my code for xml. I think it will work. – Marin Sep 12 '13 at 6:28 output = open('epg.xml','w') n = 0 print >> output, '<?xml version=""1.0"" encoding=""utf-8"" ?>'+'\t' print >> output, '<data>' with open('epg_slo_utf_xml.txt','r') as txt: for line in txt: if re.search('Program', line) !=None: n = n + 1 e = '<program name=SLO>'+line+'</program>' if re.search('Start', line) !=None: n = n + 1 f = '<start>'+line+'</start>' if re.search('Duration', line) !=None: n = n + 1 g = '<duration>'+line+'</duration>' wo = e + f + g print >> output, wo + w print >> output , '</data>' – Marin Sep 12 '13 at 6:30 @RomanPekar Will it consider the white spaces also? I'm using python 3.2. – varsha_holla Dec 19 '14 at 23:47 add a comment |"
40,"
A: User input and command line arguments
","495 To read user input you can try the cmd module for easily creating a mini-command line interpreter (with help texts and autocompletion) and raw_input (input for Python 3+) for reading a line of text from the user. text = raw_input(""prompt"") # Python 2 text = input(""prompt"") # Python 3 Command line inputs are in sys.argv. Try this in your script: import sys print (sys.argv) There are two modules for parsing command line options: optparse (deprecated since Python 2.7, use argparse instead) and getopt. If you just want to input files to your script, behold the power of fileinput. The Python library reference is your friend. share|| follow |||| edited Nov 12 '17 at 20:02 Palec 9,23177 gold badges4949 silver badges101101 bronze badges answered Sep 16 '08 at 9:50 Antti RasinenAntti Rasinen 8,26822 gold badges1919 silver badges1818 bronze badges 70 raw_input was renamed to input in Python 3.x - documentation here – steampowered Nov 30 '11 at 22:49 1 My favourite source for this: tutorialspoint.com/python/python_command_line_arguments.htm and this looks good too: cyberciti.biz/faq/python-command-line-arguments-argv-example – Jordan Stewart Apr 20 '16 at 1:30 1 sys.argv needs to be supplied with argument number, if suppose you pass a parameter as a value eg. python file_name.py 2017-02-10 and you want to use the date , it should be sys.argv[1] else it will be a list such as [file_name.py,2017-02-10] – Aravind Krishnakumar Feb 10 '17 at 20:10 add a comment |"
41,"
Q: Ampersands are not being replaced with commas in Python text file [closed] ","2 You're replacing the ampersand with an ampersand plus a space. Use this: Python> ""some & text"".replace(""&"", "","") See the docs. Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. share|| follow |||| edited Apr 6 '16 at 21:55 Padraic Cunningham 148k1717 gold badges164164 silver badges243243 bronze badges answered Apr 6 '16 at 21:50 KeithKeith 34.8k99 gold badges4646 silver badges6565 bronze badges add a comment |"
42,"
Q: Python Text Effects ",1 this sites may help you 1) http://www.pygame.org/download.shtml 2) https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ 3) https://inventwithpython.com/chapter17.html 4) https://www.youtube.com/playlist?list=PL7FC4E665CEAACD5E share|| follow |||| answered Feb 8 '17 at 5:40 Rehan ShikkalgarRehan Shikkalgar 90955 silver badges1414 bronze badges add a comment |
43,"
Q: Python, text document to list? [closed] ","1 If you simply want to process the entire file as one large String (which may be very inefficient), you could use the splitlines() method like: listOfWords = textFile.splitlines() where textFile is a String which represents all of the text in the text document. share|| follow |||| edited Apr 4 '13 at 2:36 answered Apr 4 '13 at 2:00 Kevin GurneyKevin Gurney 1,15188 silver badges1818 bronze badges add a comment |"
44,"
Q: Split Strings into words with multiple word boundary delimiters ","461 A case where regular expressions are justified: import re DATA = ""Hey, you - what are you doing here!?"" print re.findall(r""[\w']+"", DATA) # Prints ['Hey', 'you', 'what', 'are', 'you', 'doing', 'here'] share|| follow |||| edited May 23 '17 at 10:50 BartoszKP 30.1k1212 gold badges8383 silver badges117117 bronze badges answered Jun 29 '09 at 17:56 RichieHindleRichieHindle 224k4242 gold badges323323 silver badges383383 bronze badges 2 Thanks. Still interested, though - how can I implement the algorithm used in this module? And why does it not appear in the string module? – ooboo Jun 29 '09 at 18:06 29 Regular expressions can be daunting at first, but are very powerful. The regular expression '\w+' means ""a word character (a-z etc.) repeated one or more times"". There's a HOWTO on Python regular expressions here: amk.ca/python/howto/regex – RichieHindle Jul 4 '09 at 19:44 318 This isn't the answer to the question. This is an answer to a different question, that happens to work for this particular situation. It's as if someone asked ""how do I make a left turn"" and the top-voted answer was ""take the next three right turns."" It works for certain intersections, but it doesn't give the needed answer. Ironically, the answer is in re, just not findall. The answer below giving re.split() is superior. – Jesse Dhillon Sep 9 '13 at 18:47 4 @JesseDhillon ""take all substrings consisting of a sequence of word characters"" and ""split on all substrings consisting of a sequence of non-word characters"" are literally just different ways of expressing the same operation; I'm not sure why you'd call either answer superior. – Mark Amery Jul 13 '15 at 17:51 4 @TMWP: The apostophe means that a word like don't is treated as a single word, rather than being split into don and t. – RichieHindle Mar 21 '17 at 14:48 | show 12 more comments"
45,"
Q: python text widget get method ","2 When you get all text of the widget, there is also included a ""\n"" at the end. You can remove this last character like this: data=text.get(1.0,END)[:-1] Note that this always works independently of the length of the text length: >>> ""\n""[:-1] '' >>> """"[:-1] '' share|| follow |||| answered Mar 22 '13 at 9:25 A. RodasA. Rodas 17.7k77 gold badges4949 silver badges6565 bronze badges 2 The normal way to not get the automatic \n is to do text.get(1.0, END+""-1c"") (or the slightly less complex ""end-1c"") – Bryan Oakley Mar 22 '13 at 10:53 @BryanOakley I've used this since it is a common Python idiom and can be used with any string, while ""end-1c"" looks a Tkinter-specific solution. – A. Rodas Mar 22 '13 at 11:03 how about using .rstrip() – arvindh Aug 5 '15 at 7:35 add a comment |"
46,"
Q: Python Text Based Map [closed] ","2 use a package like curses to get 'direct' access to the text-screen. You have absolute positioning, colors, scrolling and of cause clearing. share|| follow |||| answered Apr 24 '14 at 10:54 DanielDaniel 36.3k44 gold badges3737 silver badges6767 bronze badges add a comment |"
47,"
Q: Easiest way to put python text game in html? ","0 I am developing a website with python3.8 and Sanic. It was pretty to use async, await and := ~ share|| follow |||| answered Jan 31 at 2:53 Waket ZhengWaket Zheng 1,53511 gold badge55 silver badges1414 bronze badges add a comment |"
48,"
Q: Python Text file statistics ","1 There are a few problems with your code. Specifically, the method split() with no arguments splits a string on any white-space rather than line boundaries. splitlines() will split on lines. Additionally your code: word_count += len(wordslist) is adding the length of the wordslist to the word count for each element of your wordslist. This is almost definitely not what you want! Also note that your code: for char in ': , . ! ?': data = data.replace(char,' ') is replacing each character from the supplied string (': , . ! ?':') with a space. However because your string of characters contains spaces itself, you are needlessly replacing all spaces in data with spaces 4 times over. It won't change the results, but it makes your code less efficient. Instead you could achieve more correct results with code like this: with open('book.txt','r') as file: data = file.read() for char in ':,.!?': data = data.replace(char,' ') word_count = len(data.split()) #count of words separated by whitespace line_count = len(data.splitlines()) #count of lines in data print(word_count,line_count) Addendum It was also asked in comments how to get the character count. Assuming that the character count should count all characters that are not whitespace (tabs, newlines etc) or in the list of special characters, then it could be done with regular expressions: import re #original code that stripped out punctuation here chars_only = re.sub(r""\s+"", """", data, flags=re.UNICODE) char_count = len(chars_only) re.sub performs a regular expression substitution, replacing characters that match the expression r""\s+"" (which is the equivalent of ""all whitespace characters"") with the second argument - an empty string in this case. However, it should be noted that this char_count would include any punctuation characters that aren't in the original list of special punctuation characters (such as apostrophes). share|| follow |||| edited May 24 '18 at 5:04 answered May 24 '18 at 4:20 Matt CoubroughMatt Coubrough 3,10822 gold badges2020 silver badges3333 bronze badges That would be another question entirely, but you'd have to remove all whitespace from data and then take its length. If the whitespace is only spaces and not newlines/tabs etc then you could just use len(data.replace(' ','')), However to remove all whitespace including tabs newlines etc would require regular expressions such as this answer: stackoverflow.com/a/28607213/3651800 – Matt Coubrough May 24 '18 at 4:32 I have added an example of how regular expressions could be used to get a char only count - 'regex' may seem a bit scary at first, and there are non-regex ways of doing this, but learning regex is definitely worthwhile for any software developer as it can often reduce dozens of lines of code to a single line. – Matt Coubrough May 24 '18 at 5:08 add a comment |"
49,"
Q: How can I read large text files in Python, line by line, without loading it into memory? ","298 I provided this answer because Keith's, while succinct, doesn't close the file explicitly with open(""log.txt"") as infile: for line in infile: do_something_with(line) share|| follow |||| edited Jun 25 '11 at 3:28 answered Jun 25 '11 at 2:26 John La RooyJohn La Rooy 239k4545 gold badges315315 silver badges458458 bronze badges 25 the question still is, ""for line in infile"" will load my 5GB of lines in to the memory? and, How can I read from tail? – Bruno Rocha - rochacbruno Jun 25 '11 at 2:31 62 @rochacbruno, it only reads one line at a time. When the next line is read, the previous one will be garbage collected unless you have stored a reference to it somewhere else – John La Rooy Jun 25 '11 at 2:33 1 @rochacbruno, Reading the lines in reverse order is not as easy to do efficiently unfortunately. Generally you would want to read from the end of the file in sensible sized chunks (kilobytes to megabytes say) and split on newline characters ( or whatever the line ending char is on your platform) – John La Rooy Jun 25 '11 at 2:36 4 Thanks! I found the tail solution stackoverflow.com/questions/5896079/… – Bruno Rocha - rochacbruno Jun 25 '11 at 3:09 1 @bawejakunal, Do you mean if a line is too long to load into memory at once? That is unusual for a text file. Instead of using for loop which iterates over the lines, you can use chunk = infile.read(chunksize) to read limited size chunks regardless of their content. You'll have to search inside the chunks for newlines yourself. – John La Rooy Jan 9 '18 at 21:50 | show 5 more comments"
50,"
Q: How to check if text is “empty” (spaces, tabs, newlines) in Python? ","300 yourString.isspace() ""Return true if there are only whitespace characters in the string and there is at least one character, false otherwise."" Combine that with a special case for handling the empty string. Alternatively, you could use strippedString = yourString.strip() And then check if strippedString is empty. share|| follow |||| edited Mar 8 '10 at 22:54 rjmunro 23.3k1818 gold badges9999 silver badges124124 bronze badges answered Mar 8 '10 at 22:30 VladislavVladislav 3,63611 gold badge1515 silver badges1515 bronze badges This fails in Python 2.4 if string contains a non-breaking space, charcode U+00A0 or ALT+160. Looks fixed in Python 2.7, however. – Kumba Dec 22 '12 at 1:51 16 Keep in mind that this does not check for None, or '' – Parth Mehrotra Sep 9 '15 at 18:30 1 In python 2.7.13, isspace() will consider non-breaking space as a space. Nice! – Iching Chang Jul 28 '17 at 7:28 add a comment |"
51,"
Q: Text processing - Python vs Perl performance [closed] ","18 This is exactly the sort of stuff that Perl was designed to do, so it doesn't surprise me that it's faster. One easy optimization in your Python code would be to precompile those regexes, so they aren't getting recompiled each time. exists_re = re.compile(r'^(.*?) INFO.*Such a record already exists') location_re = re.compile(r'^AwbLocation (.*?) insert into') And then in your loop: mprev = exists_re.search(currline) and mcurr = location_re.search(currline) That by itself won't magically bring your Python script in line with your Perl script, but repeatedly calling re in a loop without compiling first is bad practice in Python. share|| follow |||| answered Oct 9 '12 at 6:03 Josh WrightJosh Wright 2,3291313 silver badges2020 bronze badges 3 re caches recently-used regexes, so this is probably not a huge issue. – nneonneo Oct 9 '12 at 6:05 5 @nneonneo I've heard that numerous times and I've seen the lines in the re source code which do the caching. But somehow I've never seen a benchmark that puts the two in the same order of magnitude, but several benchmarks (including a quick and dirty one I did a second ago) which put the pre-compiling option at several times faster. – user395760 Oct 9 '12 at 6:15 3 Interesting. Well, it's definitely good practice to precompile regexes, but I didn't really pay attention to the performance gap. Care to share the numbers? – nneonneo Oct 9 '12 at 6:17 add a comment |"
52,"
Q: Python Text Adventure Loop ","2 Use a while loop: obj1 = input(...) while obj1 not in ('a', 'b'): obj1 = input(""Invalid. Enter again: "") if obj1 == 'a': ... elif obj1 == 'b': ... share|| follow |||| answered Feb 22 '14 at 22:09 ChristianChristian 29.4k55 gold badges4141 silver badges6565 bronze badges add a comment |"
53,"
Q: Python - Text Processing ","0 I'm not 100% sure of what you're trying to do, but I think this should get you on the right tracks: lines = open(""register.txt"", ""r"").readlines() search_counters = { ""Per End"": 0, ""File:"": 0, } lines = [line for line in lines if line] # removes empty lines, if there are any for i, line in enumerate(lines): for search_key in search_counters.keys(): if search_key in line: search_counters[search_key] += 1 # print the previous line if the current line contains ""Per End"": if search_key == ""Per End"": print ""previous line:"", lines[i-1] share|| follow |||| answered Apr 7 '17 at 13:12 smasseysmassey 5,1351919 silver badges3333 bronze badges So this works well. But only to a point. After adding a few more IF statements I get that a certain variable is ""not defined"" even though it is set up in the Dictionary and everything is coded the same way as the other items. Not sure what I am doing wrong. – Jason Jabbour Apr 17 '17 at 20:57 add a comment |"
54,"
Q: Python text extraction ","1 You need to track what section you are in as you are parsing the file. There are cleaner ways to write the state machine, but as a quick and simple example, you could do something like below. Basically, add all the lines for each section to a list for that section, then combine the lists and do whatever at the end. Note, I didn't test this, just psuedo-coding to show you the general idea. authors = [] title = [] section = None for line in articles: line = line.strip() # Check for start of new section, select the right list to add to if line.startswith(""AU""): line = line[3:] section = authors elif line.startswith(""TI""): line = line[3:] section = title # Other sections.. ... # Add line to the current section if line and section is not None: section.append(line) authors_str = ', '.join(authors) title_str = ' '.join(title) print authors_str, title_str share|| follow |||| edited Feb 4 '19 at 17:32 answered Feb 4 '19 at 16:37 Ryan WidmaierRyan Widmaier 4,71011 gold badge1818 silver badges2323 bronze badges With this code the output is empty atm, It seems like it can't search the txt file for lines starting with ""AU"" – André Kalmendal Feb 4 '19 at 16:59 section check needs to be ""section is not None"". I updated it, but as I stated in the description, it's intended as psuedo code to give you the general idea. You will need to adapt to fully implement your case. – Ryan Widmaier Feb 4 '19 at 17:31 add a comment |"
55,"
Q: Python text search question [closed] ","3 Let's see... return [x for x in document.split() if 'a' in x and sum((1 if y in 'abcdef' else 0 for y in x)) >= 3] split with no parameters acts as a ""words"" function, splitting on any whitespace and removing words that contain no characters. Then you check if the letter 'a' is in the word. If 'a' is in the word, you use a generator expression that goes over every letter in the word. If the letter is inside of the string of available letters, then it returns a 1 which contributes to the sum. Otherwise, it returns 0. Then if the sum is 3 or greater, it keeps it. A generator is used instead of a list comprehension because sum will accept anything iterable and it stops a temporary list from having to be created (less memory overhead). It doesn't have the best access times because of the use of in (which on a string should have an O(n) time), but that generally isn't a very big problem unless the data sets are huge. You can optimize that a bit to pack the string into a set and the constant 'abcdef' can easily be a set. I just didn't want to ruin the nice one liner. EDIT: Oh, and to improve time on the if portion (which is where the inefficiencies are), you could separate it out into a function that iterates over the string once and returns True if the conditions are met. I would have done this, but it ruined my one liner. EDIT 2: I didn't see the ""must have 3 different characters"" part. You can't do that in a one liner. You can just take the if portion out into a function. def is_valid(word, chars): count = 0 for x in word: if x in chars: count += 1 chars.remove(x) return count >= 3 and 'a' not in chars def parse_document(document): return [x for x in document.split() if is_valid(x, set('abcdef'))] This one shouldn't have any performance problems on real world data sets. share|| follow |||| edited Sep 22 '11 at 18:50 answered Sep 22 '11 at 16:46 Jonathan SternbergJonathan Sternberg 5,42344 gold badges3030 silver badges5656 bronze badges +1 for pedagogical value but I'd be surprised if this could handle a real world dataset with acceptable speed – Profane Sep 22 '11 at 17:55 It would actually work on a real world data set better than you think. Words are generally not that long and the in statement on 'abcdef' is very quick. If you used something like pypy, it would probably run at similar speeds to a properly optimized C program. – Jonathan Sternberg Sep 22 '11 at 18:45 add a comment |"
56,"
Q: python text parsing and splitting ","1 NOAA data is usually formatted pretty regularly. The best approach is to split the input into separate lines and then loop through line-by-line. Skip lines, unless they start with a phrase or keyword you're interested in. For example: for line in text.split('\n'): if any([re.match('^: [0-9]{4} [AP]M', line), # matches : 1249 PM line.startswith(': CITY OF'), # CITY OF... line.startswith('.A D')]): # .A D.... saveFile.write(line) (You'll need to modify the above base on what the actual, possible line values are.) share|| follow |||| answered Mar 5 '17 at 21:03 pbuckpbuck 2,88422 gold badges1515 silver badges2525 bronze badges Thank you! Very useful – Behi Mar 5 '17 at 21:31 add a comment |"
57,"
Q: Add text to Existing PDF using Python ","84 I know this is an older post, but I spent a long time trying to find a solution. I came across a decent one using only ReportLab and PyPDF so I thought I'd share: read your PDF using PdfFileReader(), we'll call this input create a new pdf containing your text to add using ReportLab, save this as a string object read the string object using PdfFileReader(), we'll call this text create a new PDF object using PdfFileWriter(), we'll call this output iterate through input and apply .mergePage(*text*.getPage(0)) for each page you want the text added to, then use output.addPage() to add the modified pages to a new document This works well for simple text additions. See PyPDF's sample for watermarking a document. Here is some code to answer the question below: packet = StringIO.StringIO() can = canvas.Canvas(packet, pagesize=letter) <do something with canvas> can.save() packet.seek(0) input = PdfFileReader(packet) From here you can merge the pages of the input file with another document. share|| follow |||| edited Dec 10 '18 at 17:43 user8554766 answered Feb 1 '10 at 23:28 dwelchdwelch 2,47822 gold badges1818 silver badges1111 bronze badges 2 ""create a new pdf containing your text to add using ReportLab, save this as a string object"" How do you do that? Its a canvas instance. – Lakshman Prasad Apr 16 '10 at 8:23 1 I've added some sample code above to answer Lakshman's question. – dwelch Dec 1 '10 at 15:58 I recommend using PyPDF2 since it is more updated, also check their sample code: github.com/mstamy2/PyPDF2/blob/… – blaze Apr 23 '15 at 4:06 2 This code will create a new pdf file and will skip all metadata. So it's not appending to existing pdf. – Anton Kukoba Apr 23 '18 at 11:56 add a comment |"
58,"
Q: Python text to HTML link [closed] ","2 As an alternative, if you wanted to open links from the shell, you can use the webbrowser module. import webbrowser urls = ['http://www.xkcd.org/', 'http://stackoverflow.com/questions/22213479/python-text-to-html-link', 'http://docs.python.org/2/library/webbrowser.html'] for url in urls: webbrowser.open_new_tab(url) share|| follow |||| answered Mar 6 '14 at 2:50 user764357user764357 ....Thanks a lot-Both Useful answers!! – ashwin shanker Mar 7 '14 at 9:01 add a comment |"
59,"
Q: Python text parser for C++ program? ","2 Yes you can. You have to embed the python interpreter in your application. Your calling code would seem something like: Py_Initialize(); PyRun_SimpleString(""import parser\n"" ""parse("" + program_code + "")\n""); There is a whole section in the docs dedicated to embedding Python. share|| follow |||| answered Jun 26 '18 at 6:30 BaltasarqBaltasarq 10.6k11 gold badge3232 silver badges4949 bronze badges add a comment |"
60,"
Q: How to erase the file contents of text file in Python? ","258 In python: open('file.txt', 'w').close() Or alternatively, if you have already an opened file: f = open('file.txt', 'r+') f.truncate(0) # need '0' when using r+ In C++, you could use something similar. share|| follow |||| edited Jun 28 '18 at 20:38 Ctrl S 8911010 silver badges2929 bronze badges answered May 4 '10 at 21:27 ondraondra 6,92611 gold badge2121 silver badges3333 bronze badges #include<fstream> and then std::ofstream(""file.txt""); about as short as in Python. :) – wilhelmtell May 4 '10 at 21:44 7 the reason this works (in both C++ and python) is because by default when you open a file for writing, it truncates the existing contents. So really it's sorta a side effect, and thus I would prefer the explicit call to truncate() for clarity reasons, even though it is unnecessary. – rmeador May 4 '10 at 22:05 7 if you have opened the file using ""r+"", use truncate(0) if you have parsed file, which you likely has. Add this to the answer above to have it complete! – Johan Engblom Apr 20 '17 at 7:57 3 use f.seek(0) to move the pointer to the beginning of the file – Johan Engblom Apr 20 '17 at 8:05 add a comment |"
61,"
Q: Writing Python text file ","1 you need to do this- import string alphabet= string.ascii_lowercase for letter in alphabet: with open(letter+"".txt"",'w') as file: file.write(letter) share|| follow |||| answered Feb 18 '17 at 13:45 MD. Khairul BasarMD. Khairul Basar 3,7171010 gold badges3030 silver badges4848 bronze badges add a comment |"
62,"
Q: Python text to xml converting solutions ","0 from your code I would say, its just a typo - you are looking for 'duration', but the file only holds 'Duration', with a capital D. Thus, g is never set. You can avoid this behavior by initializing your variables at the beginning by setting them to an empty string. On a side note, for such an easy task like checking for a word within a line you do not need re, try the - I guess - more ""pythonic"" way: if 'Duration' in line: #do something share|| follow |||| answered Sep 12 '13 at 10:52 FaultierFaultier 96611 gold badge1010 silver badges2020 bronze badges add a comment |"
63,"
Q: Python text file append error ","2 As said in a different answer the problem is your string handling when writing to the file. I would recommend to use string formatting: hp.write(""\n'%s': ['%s', '%s']"" % (key, usr, psw)) See https://pyformat.info/ Recommended code: # Ask for variables to add key = raw_input(""Which app: "") usr = raw_input(""Username: "") psw = raw_input(""Password: "") # Open file with open(""Passwords.txt"", ""a"") as hp: # Add line with same format as the rest of lines hp.write(""\n'%s': ['%s', '%s']"" % (key, usr, psw)) If you use the with open(...) as ...: you don't have to call the close method, it's called automatically when you exit the with's scope. share|| follow |||| answered Jun 6 '17 at 12:57 Paco H.Paco H. 1,69533 silver badges1515 bronze badges add a comment |"
64,"
Q: How to determine the encoding of text? ","219 Correctly detecting the encoding all times is impossible. (From chardet FAQ:) However, some encodings are optimized for specific languages, and languages are not random. Some character sequences pop up all the time, while other sequences make no sense. A person fluent in English who opens a newspaper and finds “txzqJv 2!dasd0a QqdKjvz” will instantly recognize that that isn't English (even though it is composed entirely of English letters). By studying lots of “typical” text, a computer algorithm can simulate this kind of fluency and make an educated guess about a text's language. There is the chardet library that uses that study to try to detect encoding. chardet is a port of the auto-detection code in Mozilla. You can also use UnicodeDammit. It will try the following methods: An encoding discovered in the document itself: for instance, in an XML declaration or (for HTML documents) an http-equiv META tag. If Beautiful Soup finds this kind of encoding within the document, it parses the document again from the beginning and gives the new encoding a try. The only exception is if you explicitly specified an encoding, and that encoding actually worked: then it will ignore any encoding it finds in the document. An encoding sniffed by looking at the first few bytes of the file. If an encoding is detected at this stage, it will be one of the UTF-* encodings, EBCDIC, or ASCII. An encoding sniffed by the chardet library, if you have it installed. UTF-8 Windows-1252 share|| follow |||| edited Apr 3 '17 at 15:54 davidism 81.7k1616 gold badges248248 silver badges243243 bronze badges answered Jan 12 '09 at 17:45 nosklonosklo 176k5151 gold badges265265 silver badges279279 bronze badges 1 Thanks for the chardet reference. Seems good, although a bit slow. – Craig McQueen Jan 28 '10 at 5:15 Not been able to detect the encoding all the time...isn't this a flaw in the encoding standard? shouldn't this be allways predictable? – Geomorillo Dec 1 '13 at 21:40 14 @Geomorillo: There's no such thing as ""the encoding standard"". Text encoding is something as old as computing, it grew organically with time and needs, it wasn't planned. ""Unicode"" is an attempt to fix this. – nosklo Dec 2 '13 at 14:34 1 And not a bad one, all things considered. What I would like to know is, how do I find out what encoding an open text file was opened with? – holdenweb Mar 14 '14 at 6:27 1 @dumbledad what I said is that correctly detecting it all times is impossible. All you can do is a guess, but it can fail sometimes, it won't work every time, due to encodings not being really detectable. To do the guess, you can use one of the tools I suggested in the answer – nosklo Apr 20 '18 at 15:41 | show 7 more comments"
65,"
Q: Very Basic Python Text Adventure ","2 As @dave mankoff suggested, you should use a ""state machine"". Here is more detail about that. For a basic state machine, you need a ""state variable"", a variable that keeps track of the current state. Then, as the user makes choices, the state variable gets updated to reflect the new state. You can call the state variable anything, but I suggest calling it state. That's traditional and pretty straightforward. You can use any set of unique values to keep track of the state, as long as you can map the unique values onto the states you want. In most languages you would use an ""enum"", an enumerated constant, but Python has historically not had enums. (They are being added as of Python 3.4, which at this time is in alpha release.) A simple way is to use strings for the states, and have the strings describe the states. So for your game, most of the states will be places the player can go: ""library"", ""kitchen"", ""potion_storage_room"" or whatever. But you might also have states that don't strictly map to single places, such as: ""wandering_around"" (in a daze after drinking the wrong potion?). You can also use integers, something like this: LIBRARY, KITCHEN, POTION_STORAGE_ROOM, WANDERING_AROUND = range(4) Now LIBRARY is set to integer 0, KITCHEN is set to integer 1, and so on. I think strings might be easier, but if you search for examples of state machines, you will probably see the above trick being used for integer states for the state machine. You might want to study the book/website Learn Python the Hard Way. Exercise 43 shows a simple text game. It uses a state machine with strings to represent the state. There is example code for a simple game, and you could run the code and play the game to see how it works. (It doesn't look like a very fun game, but I think the author of that book wants to encourage you to do a better one!) http://learnpythonthehardway.org/ http://learnpythonthehardway.org/book/ex43.html share|| follow |||| edited Oct 22 '13 at 18:38 answered Oct 21 '13 at 19:54 stevehasteveha 62.1k1616 gold badges8080 silver badges107107 bronze badges add a comment |"
66,"
Q: Python Text to Speech in Macintosh ","36 Wouldn't it be much simpler to do this? from os import system system('say Hello world!') You can enter man say to see other things you can do with the say command. However, if you want some more advanced features, importing AppKit would also be a possibility, although some Cocoa/Objective C knowledge is needed. from AppKit import NSSpeechSynthesizer speechSynthesizer = NSSpeechSynthesizer.alloc().initWithVoice_(""com.apple.speech.synthesis.voice.Bruce"") speechSynthesizer.startSpeakingString_('Hi! Nice to meet you!') If you would like to see more things you can do with NSSpeechSynthesizer take a look at Apple's documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSpeechSynthesizer_Class/Reference/Reference.html share|| follow |||| edited Jan 24 '16 at 12:52 Frederik 12k77 gold badges3939 silver badges5151 bronze badges answered Oct 6 '12 at 16:12 rien333rien333 9971010 silver badges1515 bronze badges 1 if hello world is in a variable how do i make say speak it? I tried AppKit but it's not available. `easy_install' was not able to do it either. how do i do that? – VeilEclipse Oct 6 '12 at 20:04 If you want to pass a variable to the say command, you could do this: str = ""Test"" system('say %s' % (str)) – rien333 Oct 6 '12 at 20:15 The code snippet in this answer is slightly off. .alloc is a function, so it should be .alloc() – Ben Keating Jan 7 '15 at 15:06 Including an apostrophe in the string may cause this to not work. For example trying to do system(""Can't touch this"") will run without an error, but not actually say anything out loud. – Pro Q Oct 3 '18 at 3:21 @ProQ normal shell quoting rules apply of course. You could embed that string in literal quotes for example (like you would normally have to) – rien333 Oct 3 '18 at 16:03 | show 3 more comments"
67,"
Q: Python Text Menu Infinite Loop ","4 You're missing a line in menu, or else missing an input elsewhere. You're not actually accepting a choice from your user yet. If you want to keep your current structure, menu should look like: def menu(): print 'options...' return int(raw_input()) Or, a little cleaner (same effect): def menu(): return int(raw_input('options...')) Otherwise, you can just call menu() and then separately accept the user's selection: while loop == 1: menu() choice = int(raw_input()) Note that I've changed your input() calls to int(raw_input()). This is a much safer way of accepting input from your user, since it prevents them from embedding arbitrary Python code in their input! Not-quite-on-topic: Now, just because I happened to notice it, I'm also going to mention that loop is a potentially-misleading variable name. Since you're just using it as a boolean, you could rewrite the while loop like this: loop = 1 while loop: # ! #do stuff This is kind of funny, but not very intuitive to read. It's usually better to simply loop indefinitely, and use break when your end condition is met: while True: # Do stuff if choice == 5: #Or whatever end condition break share|| follow |||| edited May 30 '13 at 15:09 answered May 30 '13 at 15:04 Henry KeiterHenry Keiter 14.4k66 gold badges3838 silver badges6868 bronze badges Ah thank you. I guess that would be important for them to make a selection, d'oh! – user2433340 May 30 '13 at 15:05 add a comment |"
68,"
Q: “Unicode Error ”unicodeescape"" codec can't decode bytes… Cannot open text files in Python 3 [duplicate] ","556 The problem is with the string ""C:\Users\Eric\Desktop\beeline.txt"" Here, \U in ""C:\Users... starts an eight-character Unicode escape, such as \U00014321. In your code, the escape is followed by the character 's', which is invalid. You either need to duplicate all backslashes: ""C:\\Users\\Eric\\Desktop\\beeline.txt"" Or prefix the string with r (to produce a raw string): r""C:\Users\Eric\Desktop\beeline.txt"" share|| follow |||| edited Jul 25 '19 at 6:20 QA Collective 1,17899 silver badges2121 bronze badges answered Aug 28 '09 at 15:46 Martin v. LöwisMartin v. Löwis 107k1616 gold badges177177 silver badges223223 bronze badges 37 Hahaha... What an easy trap to fall into, like I just have. Awesome answer. – Humphrey Bogart Feb 24 '10 at 0:04 I found this error in a function docstring while porting a 2.x code to python3. – Sridhar Ratnakumar Apr 28 '10 at 20:16 6 I ran into this error when I used triple quote ''' comments around a section of code that contained a raw string with a \U in it. I.E. the string didn't give me an error until I tried to comment it out. For this reason the double backslash method might be preferred. – Chris Mueller May 22 '15 at 15:46 1 I am prefixing the string with r, but it is still giving me the error. – Bobort Oct 17 '16 at 21:01 2 A third solution (also proposed by @Adam Baylin Autuori) is to replace '\' with '/' in the path. Python understand both '\' (Windows-style) or '/' (Unix-style) as directory separator. – Jona Apr 17 '18 at 9:17 | show 2 more comments"
69,"
Q: Python text file formatting ","0 def adjustString(deviceNumber): newData = str.replace(data, deviceNumber, '\n' + deviceNumber + ' this was device number one') return newData with open('data.txt', 'r') as myfile: data=myfile.read().replace('\n', '') output = adjustString('0103') You can then write the new string to a file as needed. with open('data.txt') as f: f.write(output) share|| follow |||| edited Dec 15 '17 at 14:22 answered Dec 15 '17 at 14:12 chumbaloochumbaloo 17811 silver badge99 bronze badges Thank you chumbaloo, it works :) Is it posible to find a string in a file and then navigate four characters more (leave the four original characters in a file) and put some text after? – cwanish Dec 18 '17 at 6:22 Hi, that is what the str.replace function does. It finds the string match in the example above the deviceNumber, then adds a new line, replaces the 4 characters with the original 4 characters, and then adds text.) So just change what you want in the str.replace function to get the desired output. if you wanted to do more fancy stuff, have a look at the re module in python. If this was helpful please accept the answer, and happy to have helped :) – chumbaloo Dec 18 '17 at 6:36 add a comment |"
70,"
Q: python text translation to speech ","0 SAPI only supports voices that are installed. Since Microsoft doesn't have a Hindi TTS voice, you will need to look elsewhere for either a SAPI-compliant Hindi TTS voice, or a web service that supports Hindi TTS. A web search will help you with your selection. share|| follow |||| answered May 18 '18 at 17:34 Eric BrownEric Brown 13k55 gold badges2626 silver badges6565 bronze badges add a comment |"
71,"
Q: Python Text-based Game Equip Armor Stats ","1 It sounds like each character has a base attribute score, but the effective score is the base modified by various pieces of equipment. This is a good use case for a property. class Player: def __init__(self, name): # ... self.base_stre = 1 # ... @property def stre(self): stre = self.base_stre if self.weap == ""Rusty Sword"": stre += 10 # etc return str share|| follow |||| answered Aug 11 '15 at 19:31 chepnerchepner 329k4343 gold badges324324 silver badges439439 bronze badges After I add the @property and change stre to a method in Player how would I call upon it for use in something like viewable stats so the player knows how much attack or defense they have? Basically I'm asking how would I print stre? – King Arthur Aug 11 '15 at 19:53 Properties are accessed just like plain attributes (print p.stre); the only difference is that instead of being a simple lookup (p.__dict__['stre']), it's now the return value of a method call. – chepner Aug 11 '15 at 20:37 add a comment |"
72,"
Q: Python text file in string to dictionary ","0 import re lines=[] keys=[] values=[] with open('data', 'r') as f: lines=list(map(lambda line:line.strip(), f.readlines())) d, flag={}, False for line in lines: if re.match(r'^-+$', line): flag=True continue if flag: values.append(list(map(lambda x:x.strip(), line.split()))) else: keys=list(map(lambda x:x.strip(), line.split())) for i in range(len(keys)): d[keys[i]]=[value[i] for value in values] print(d) and here is the output: $ cat data Age Sex ------------ GT20 M LT20 F $ python3 p.py {'Sex': ['M', 'F'], 'Age': ['GT20', 'LT20']} share|| follow |||| answered May 17 '16 at 7:15 riteshtchriteshtch 7,93944 gold badges1818 silver badges3535 bronze badges add a comment |"
73,"
Q: cleaning text files in python 2 : TypeError: coercing to Unicode: ","0 import nltk form nltk import word_tokenize from nltk.corpus import stopwords #nltk.download() import string from string import digits import glob import re def cleanupDoc(s): #s = s.translate(None,digits) #s = s.rstrip('\n') stopset = set(stopwords.words('english')) tokens = nltk.word_tokenize(s) cleanup = "" "".join(filter(lambda word: word not in stopset, s.split())) return cleanup flist=glob.glob('/home/uiucinfo/Desktop/*txt') mylist=[] for fname in flist: tfile = open(fname, 'r+') line = tfile.readlines() #line = cleanupDoc(line) mylist.append(line) for fdoc in mylist: # remove \n or digit from fdoc fdoc = [re.sub(r'[\""\n]|\d', '', x) for x in fdoc] # convert list to string fdoc = ''.join(fdoc) print fdoc newDoc = cleanupDoc(fdoc) print "" newDoc: "" , newDoc share|| follow |||| answered Dec 15 '14 at 20:34 Sadegh ZahedianSadegh Zahedian 3622 bronze badges It would be better if you described how you fixed the issue and what you changed, rather just posting the code. – Ismail Badawi Dec 15 '14 at 20:37 add a comment |"
74,"
Q: How to modify a text file? ","134 Unfortunately there is no way to insert into the middle of a file without re-writing it. As previous posters have indicated, you can append to a file or overwrite part of it using seek but if you want to add stuff at the beginning or the middle, you'll have to rewrite it. This is an operating system thing, not a Python thing. It is the same in all languages. What I usually do is read from the file, make the modifications and write it out to a new file called myfile.txt.tmp or something like that. This is better than reading the whole file into memory because the file may be too large for that. Once the temporary file is completed, I rename it the same as the original file. This is a good, safe way to do it because if the file write crashes or aborts for any reason, you still have your untouched original file. share|| follow |||| edited Oct 9 '12 at 12:11 meagar♦ 196k3434 gold badges294294 silver badges306306 bronze badges answered Sep 24 '08 at 6:57 Adam PierceAdam Pierce 29.2k2121 gold badges6565 silver badges8585 bronze badges 3 Do unix tools like awk/sed do something similar in their code? – Manish Gill Mar 22 '13 at 19:16 It's not true that this is the same in all languages. In ActionScript: fileStream.openAsync(filename,FileMode.UPDATE); Then I can go anywhere in the file I want and change anything. – AndrewBenjamin Jul 11 '14 at 3:58 2 @AndrewBenjamin Do you know what system calls ActionScript is making? Is there a possibility that openAsync reads the file and writes a new one after the call? – AlexLordThorsen Dec 2 '14 at 22:23 @Rawrgulmuffins I do not. However, I do know that it isn't reading the entire file into memory, as I have used it to handle filesizes of several GB. I suspect it's the same as writing with C# streamwriter. I view python as a tool for doing small things quickly, rather than large scale development and file manipulation. – AndrewBenjamin Mar 26 '15 at 21:48 3 @AndrewBenjamin, the user isn't asking about seeking around in the file and changing it (every language I know of can do that); he is asking about inserting text, which is different than simply changing/overwriting what is already in the file. Maybe in practical application it is different, but nothing I can find in the ActionScript API indicates that it behaves any different from any other language in this regard. – eestrada Jul 22 '15 at 14:44 | show 1 more comment"
75,"
Q: Python Text Based Game Display ","2 You clear the screen immediately after drawing, rather than before drawing. Thus, the thing you just drew is erased. def Draw(): frog.Draw() os.system('cls') Try switching the order: def Draw(): os.system('cls') frog.Draw() share|| follow |||| answered Sep 30 '12 at 5:08 AmberAmber 404k6666 gold badges566566 silver badges512512 bronze badges add a comment |"
76,"
Q: Sublime Text shows nothing on Python build ","0 Try make sure you have Python in your system path: Windows key + Pause/Break key to shortcut to System. Click on Advanced System Settings. Scroll down in System Variables until you find ""Path"", click ""edit"" Look for C:\Python34 in the string; if it's there don't worry, if not, add it in as follows: someDirectory\lastItem;C:\Python34. Seperate with semi-colon. (for windows). Now, you can change your Python.sublime-build file as follows: { ""cmd"": [""C://Python34/python.exe"", ""-u"", ""$file""], ""file_regex"": ""^[ ]*File \""(...*?)\"", line ([0-9]*)"", ""selector"": ""source.python"" } If needed, change the directory for Python, or change ""34"" to whichever version you use. Should do the trick. EDIT: Feel free to leave in your encoding, I just copy/pasted my sublime-build. share|| follow |||| edited Jun 16 '15 at 0:50 answered Jun 16 '15 at 0:44 maccartmmaccartm 1,67077 silver badges1616 bronze badges Thanks for the reply. Still not working... It's really odd... Seems that the program is building, but it isn't showing nothing. I already tried many other codes on the sublime-build file... – Anderson Anizio Jun 16 '15 at 3:26 stackoverflow.com/questions/23065859/… If you follow the steps in this link, does anything change? – maccartm Jun 16 '15 at 15:05 Still not working... I tried also this one: stackoverflow.com/questions/13110863/… May have some bug in the Sublime... I'll try to update or reinstall... – Anderson Anizio Jun 17 '15 at 4:00 add a comment |"
77,"
Q: python text rpg inventory system ","1 Well, I have an old text game I made and the inventory system I use is just a list named 'inv'. Then all you have to do when you gain an item of whatever type is to append the inventory list with the string. inv = [] inv.append('Item1') Then you can create a dictionary with the key as the item name and the value be a list you can reference. Try this: itemvalues = {'Item1':[0,0,0] } And the list can be a bunch of different attributes such as buy sell price, strength or what have you. Hope this helps. share|| follow |||| answered Jan 11 '13 at 6:46 ShroomBanditShroomBandit 42111 gold badge55 silver badges1212 bronze badges add a comment |"
78,"
Q: Python text game save feature ","2 The first issue with this code is that you never close the file which can (and will) lead to memory issues because the stream will stay open, so never forget to close it using savegame.close() The second problem lies in the way the file is being opened. Pickle reads and writes in binary so the file should be read and written using binary mode (wb and rb instead of w and r). Next up, the pickling, this section is more of a suggestion and is not necessary to solve the issue that you are having. It is possible to Pickle multiple items into one file, but an easier solution might be to just put them into one object, for example a tuple containing all 3 values: def do_save(self, arg): saveGame = open('savegame.txt', 'wb') saveValues = (inventory, gold, location) pickle.dump(saveValues, saveGame) saveGame.close() def do_load(self, arg): loadGame = open('savegame.txt', 'rb') loadValues = pickle.load(loadGame) inventory = loadValues[0] gold = loadValues[1] location = loadValues[2] loadGame.close() This solution is less resource heavy considering it only reads and writes to the file once, and it allows the file stream to be open for a shorter duration. share|| follow |||| edited Dec 27 '17 at 19:47 answered Dec 27 '17 at 11:01 yarwestyarwest 67255 silver badges1818 bronze badges add a comment |"
79,"
Q: Get exception description and stack trace which caused an exception, all as a string ","591 See the traceback module, specifically the format_exc() function. Here. import traceback try: raise ValueError except ValueError: tb = traceback.format_exc() else: tb = ""No error"" finally: print tb share|| follow |||| edited Jan 16 '19 at 7:55 ianace 1,60622 gold badges1717 silver badges3131 bronze badges answered Dec 30 '10 at 17:05 kindallkindall 145k2525 gold badges219219 silver badges273273 bronze badges 1 Does this only work with the last error? What happens if you start passing the error around to other bits of code? I'm writing a log_error(err) function. – AnnanFay Oct 4 '19 at 1:23 It works with the error that was caught and handled. – kindall Oct 4 '19 at 16:42 add a comment |"
80,"
Q: Python Text file searching ","0 Are you looking for something like this? for Line in file(""Text.txt"", ""r""): if Line.find(""sum"") >= 0: print Line.split()[0] Line.split() will split the line up into words, and then you can select with an index which starts from 0, i.e. to select the 2nd word use Line.split()[1] share|| follow |||| edited Mar 8 '12 at 11:00 answered Mar 8 '12 at 8:52 Ben RussellBen Russell 1,2631111 silver badges1515 bronze badges 1 if 'sum' in Line: ... will work as well. Also, according to the documentation, open is more preferable than file. – bereal Mar 8 '12 at 8:54 great thanks alot. If I want to print the nest value next to aaaa how would I print the values for 0 and 1. print Line.split()[0,1]??? I get an error with this – Trying_hard Mar 8 '12 at 9:33 Line.split()[1] should do it. – Ben Russell Mar 8 '12 at 10:59 add a comment |"
81,"
Q: Python: text file to Excel file ","0 Think, i found the solution to it. The problem came from the argument used in the split function, i.e split_Line = space_remove.split("" "") it was supposed to be like this split_Line = space_remove.split("","") The split needed the (,) to split in the right way. Thanks to all of you who tried to help me. Bye. share|| follow |||| answered Jul 18 '14 at 9:08 Emack333Emack333 42044 silver badges1010 bronze badges add a comment |"
82,"
Q: Bug in simple text editor (Python) ","1 First, file is a predefined package; please don't use it for a variable name, or you may have trouble getting to some of the facilities. Try my_file or just the C-language fp (for ""file pointer""). After you write new information to the file, your position pointer (bookmark) is likely at the end of the file. Reading more will get you nowhere. You need to either close and reopen the file, or call fp.seek() to get to the desired location. For instance, fp.seek(0) will reset the pointer to the start of the file. share|| follow |||| answered Jul 15 '16 at 0:10 PrunePrune 59.5k1414 gold badges4444 silver badges6666 bronze badges I'd say using file as a variable name is fine. It's rarely used and it only affects the scope it's used in. – Dietrich Epp Jul 15 '16 at 0:13 add a comment |"
83,"
Q: Python text into dictionary ","1 Just very straight-forward. As you are iterating over each line, you take the first character of each line—that’s the letter—and the second character—that’s the symbol—to fill your dictionary. dictionary = {} with open('filename') as f: for line in f: dictionary[line[0]] = line[1] share|| follow |||| answered Feb 11 '14 at 11:59 pokepoke 265k5656 gold badges411411 silver badges461461 bronze badges Thanks for that, I was making alsorts of silly errors. – MrC Feb 11 '14 at 12:15 add a comment |"
84,"
Q: Trouble with Python Text Game ","0 To answer my own question, the loop in Engine.main() works just as its supposed to, but I took self.room.describe() out of the while statement: def main(self): self.room.describe() #Describes the current room. while True: # Ask for user prompt input = raw_input(self.room.prompt) cmd = input.lower() ...etc, etc, etc... Then I changed the GoCommand to: class GoCommand(Command): def __init__(self, direction): self.direction = direction def execute(self): # 'E' is the instantiation of the Engine class, declared within engine.py from engine import E # self.direction is 'north' in our example if self.direction in E.room.map.keys(): print ""You head %s\n"" % self.direction # Pretty explanatory E.room = E.room.map[self.direction] print E.room.describe() else: print ""You can't go that way."" And everything works as its supposed to. share|| follow |||| answered Dec 16 '12 at 5:44 Acour83Acour83 33522 gold badges33 silver badges1616 bronze badges add a comment |"
85,"
Q: Change The Colour Of Python Text ","0 I think the way you are using is aimed at Windows. You may use termcolor: from termcolor import colored print colored('hello', 'red'), colored('world', 'green') You may use also color formatting codes listed here, use them like that: >>> text = ""The quick brown fox jumps over the lazy dog."" >>> print('\033[1;32m'+text+'\033[1;m') The quick brown fox jumps over the lazy dog. Output would be like: share|| follow |||| edited Dec 21 '15 at 20:04 answered Dec 21 '15 at 19:21 BigOtherBigOther 8,42544 gold badges3939 silver badges7878 bronze badges Does not work but thanks anyway. I will still mark this as the best answer. – user5700251 Dec 21 '15 at 20:02 add a comment |"
86,"
Q: python text file reading is slow ","0 Read the single-column file into a list with splitlines(): def load_words(): with open(""words.txt"", 'r') as f: wordlist = f.read().splitlines() return wordlist You can benchmark it with timeit: from timeit import timeit timeit('load_words()', setup=setup, number=3) # Output: 0.1708553659846075 seconds As for how to implement what looks like a fuzzy matching algorithm, you might try fuzzywuzzy: # pip install fuzzywuzzy[speedup] from fuzzywuzzy import process wordlist = load_words() process.extract(""eabauea"", wordlist, limit=10) Output: [('-a', 90), ('A', 90), ('A.', 90), ('a', 90), (""a'"", 90), ('a-', 90), ('a.', 90), ('AB', 90), ('Ab', 90), ('ab', 90)] The results are more interesting if you filter for the longer matches: results = process.extract(""eabauea"", wordlist, limit=100) [x for x in results if len(x[0]) > 4] Output: [('abaue', 83), ('Ababua', 77), ('Abatua', 77), ('Bauera', 77), ('baulea', 77), ('abattue', 71), ('abature', 71), ('ablaqueate', 71), ('bauleah', 71), ('ebauche', 71), ('habaera', 71), ('reabuse', 71), ('Sabaean', 71), ('sabaean', 71), ('Zabaean', 71), ('-acea', 68)] But with 470K+ rows it does take awhile: timeit('process.extract(""eabauea"", wordlist, limit=3)', setup=setup, number=3) # Output: 384.97334043699084 seconds share|| follow |||| edited Apr 5 '17 at 13:57 answered Apr 5 '17 at 0:24 brennanbrennan 2,3661717 silver badges3333 bronze badges add a comment |"
87,"
Q: Python text to tree structure creation ","1 Given the homogeneity of your input, a regular expression may be the preferred solution here. import re linere = re.compile(r""^(?P<value>[0-9a-fA-F]+)h; ParIsa\.(?P<name>[a-zA-Z.]+)\[(?P<index>\d+)\]\.(?P<attribute>[0-9a-zA-Z_]+) \(BYTE\)$"") Then you can apply the compiled regular expression to every line and access the portions of interest by name on the .groupdict() result of a match object. share|| follow |||| answered Nov 25 '14 at 11:35 Helmut GrohneHelmut Grohne 5,1521717 silver badges4848 bronze badges add a comment |"
88,"
Q: python: read lines from compressed text files ","20 Have you tried using gzip.GzipFile? Arguments are similar to open. share|| follow |||| answered May 12 '12 at 19:04 jrenniejrennie 1,7271111 silver badges1414 bronze badges add a comment |"
89,"
Q: Python, text detection OCR ","0 I think you have the answer already in your own post. I did recently something similar and this is how I did it: //id_image was loaded with cv2.imread temp_image = id_image[start_y:end_y,start_x:end_x] img = Image.fromarray(temp_image) text = pytesseract.image_to_string(img, config=""-psm 7"") So basically, if your format is predefined, you just need to know the location of the fields that you want the text of (which you already know), crop it, and then apply the ocr (tesseract) extraction. In this case you need import pytesseract, PIL, cv2, numpy. share|| follow |||| answered Aug 17 '17 at 23:03 roccolockoroccolocko 10499 bronze badges add a comment |"
90,"
Q: Adding Python Text Area To Existing Program ","1 I want the last grid labeled ""description"" to be a small text area instead, As it would be used for a few sentences rather than a few words. As the Entry docs tell you: To enter multiple lines of text, use the Text widget. So: text = Text(master) text.grid(row=whatever, etc.) Also im going to need to ""get()"" this input to use as a variable. I'm not sure what you mean here. If you mean you wanted to attach a StringVar, you can't do that with Text, sadly, only with Entry. But if you want to be able to fetch the contents in some way, you don't care which way, then that's easy; the Text docs will show you how a variety of different ways, but I think text.get(1.0, END) is the one you'll probably want. share|| follow |||| answered Aug 11 '14 at 22:50 abarnertabarnert 285k2828 gold badges432432 silver badges527527 bronze badges This is a great response. I can appreciate this. – user2631279 Aug 11 '14 at 22:59 Seems like this is what i was looking for. Will keep your comments in mind on my next post. – user2631279 Aug 11 '14 at 23:06 add a comment |"
91,"
Q: Python text validation: a-z and comma (“,”) ","14 import string allowed = set(string.lowercase + ',') if set(text) - allowed: # you know it has forbidden characters else: # it doesn't have forbidden characters Doing it with sets will be faster than doing it with for loops (especially if you want to check more than one text) and is all together cleaner than regexes for this situation. an alternative that might be faster than two sets, is allowed = string.lowercase + ',' if not all(letter in allowed for letter in text): # you know it has forbidden characthers here's some meaningless mtimeit results. one is the generator expression and two is the set based solution. $ python -mtimeit -s'import scratch3' 'scratch3.one(""asdfas2423452345sdfadf34"")' 100000 loops, best of 3: 3.98 usec per loop $ python -mtimeit -s'import scratch3' 'scratch3.two(""asdfas2423452345sdfadf34"")' 100000 loops, best of 3: 4.39 usec per loop $ python -mtimeit -s'import scratch3' 'scratch3.two(""asdfasasdfadsfasdfasdfdaf"")' 100000 loops, best of 3: 3.51 usec per loop $ python -mtimeit -s'import scratch3' 'scratch3.one(""asdfasasdfadsfasdfasdfdaf"")' 100000 loops, best of 3: 7.7 usec per loop You can see that the setbased one is significantly faster than the generator expression with a small expected alphabet and success conditions. the generator expression is faster with failures because it can bail. This is pretty much whats to be expected so it's interesting to see the numbers back it up. another possibility that I forgot about is the hybrid approach. not all(letter in allowed for letter in set(text)) $ python -mtimeit -s'import scratch3' 'scratch3.three(""asdfasasdfadsfasdfasdfdaf"")' 100000 loops, best of 3: 5.06 usec per loop $ python -mtimeit -s'import scratch3' 'scratch3.three(""asdfas2423452345sdfadf34"")' 100000 loops, best of 3: 6.71 usec per loop it slows down the best case-ish but speeds up the worst case-ish. All in all, you'd have to test the different possibilities over a sample of your expected input. the broader the sample, the better. share|| follow |||| edited Sep 22 '10 at 9:46 answered Sep 22 '10 at 7:43 aaronasterlingaaronasterling 55.7k1515 gold badges112112 silver badges124124 bronze badges I personally like the second approach better. – mpen Sep 22 '10 at 7:52 @Mark but see the timings. It depends on expected inputs. – aaronasterling Sep 22 '10 at 8:05 nice, i really like the second approach – Rohan Monga Sep 22 '10 at 8:29 1 @Michał Niklas: This is exactly the kind of job regexes are made for. As you can see from Aaron's comment to Emile's answer, the regex version is several times faster. – Tim Pietzcker Sep 22 '10 at 9:02 @Tim It's true but in this case I still think that not all(letter in allowed for letter in letters) is better then regexes. I mean, that's just plain english pretty much. It's incredibly clear. If it turned out to be too slow for the application, then I would drop it in a heart beat and use a regex. We're talking microseconds here. – aaronasterling Sep 22 '10 at 9:22 | show 2 more comments"
92,"
Q: python text files ","4 Excel regulary is using the 'tab' as separtor sign for saving in 'txt' format. So you should try something like this: lines = [] with open('jag.txt') as f: lines = [ line.split('\t') for line in f.read().splitlines() ] print(lines) and should get something like this [ ['A1', 'A2', ...], ['B1', 'B2'], ... ] Why not only ""f.readlines()""? Because using this, your last cell will also contain the carriage return sign ('\n'). Why using the with statement? With will close the file finally, and this is a good election in any case. An alternative way to parse your text file could be the python (included) csv module. Using the csv.reader can be a very convenient way to parse character separated files/structures: with open('jag.txt') as f: lines = [ line for line in csv.reader(f, delimiter='\t') ] -Colin- share|| follow |||| answered Sep 3 '12 at 13:24 Colin O'CoalColin O'Coal 1,05999 silver badges1010 bronze badges add a comment |"
93,"
Q: python text to second y axis ","0 The main ingredient you still need to position the labels at the edge of the axes, is the coordinate of the edge of the axes. You can get those via plt.gca().get_xlim(). The upper limit can then be used as x position of the text label. import matplotlib.pyplot as plt import numpy as np X = np.random.normal(size=(12)) Y = np.random.normal(size=(12)) plt.figure() plt.scatter(X, Y, color='k') plt.axhline(Y.mean(), color='black', linestyle='-', lw=3) plt.axhline(Y.mean() + 1.96*Y.std(), color='black', linestyle='--') plt.axhline(Y.mean() - 1.96*Y.std(), color='black', linestyle='--') plt.axhline(0, color='black', linestyle='--') txt1=(' +1.96 SD') txt2=(' -1.96 SD') txt3 =(' Mean') x0,x1 = plt.gca().get_xlim() plt.text(x1, Y.mean() + 1.96*Y.std(), txt1, ha='left', va='center', fontweight='bold') plt.text(x1, Y.mean() - 1.96*Y.std(), txt2, ha='left', va='center', fontweight='bold') plt.text(x1, 0, txt3, ha='left', va='center',) # make more space on right side to host the labels plt.subplots_adjust(right=0.8) plt.show() Note that this is the very basic (but also quite understandable) version. If you're comfortable with transformations, you can use the technique in this question: Add a label to y-axis to show the value of y for a horizontal line in matplotlib share|| follow |||| edited May 23 '17 at 12:02 Community♦ 111 silver badge answered Apr 28 '17 at 14:04 ImportanceOfBeingErnestImportanceOfBeingErnest 199k2121 gold badges306306 silver badges384384 bronze badges add a comment |"
94,"
Q: Python and text manipulation ","19 Beyond regular expressions here are some important features: Generators, see Generator Tricks for Systems Programmers by David Beazley for a lot of great examples to pipeline unlimited amounts of text through generators. For tools, I recommend looking at the following: Whoosh, a pure Python search engine that will give you some nice real life examples of parsing text using pyparsing and text processing in Python in general. Ned Batcheldor's nice reviews of various Python parsing tools. mxTextTools Docutils source code for more advanced text processing in Python, including a sophisticated state machine. Edit: A good links specific to text processing in Python: Text Processing in Python by David Mertz. I think the book is still available, although it's probably a bit dated now. share|| follow |||| edited Mar 24 '09 at 6:02 answered Mar 24 '09 at 5:51 Van GaleVan Gale 40.1k99 gold badges6565 silver badges7878 bronze badges add a comment |"
95,"
Q: Python text extraction from letter - index ","0 Your loop gives you the last occurrence of an opening sequence. You should separate the read part into two loops, like this: with open(filename, 'r', encoding=""utf-8"") as infile: text = infile.read() lines = text.strip().split(""\n"") target_start_idx = None target_end_idx = None for index, line in enumerate(lines): line = line.lower() if any(beg in line for beg in letter_begin): target_start_idx = index break for index, line in enumerate(lines): if any(end in line for end in letter_end): target_end_idx = index + 3 continue In this way, you exit the loop when the first occurrence of an opening sequence appears. share|| follow |||| edited Nov 20 '18 at 14:12 answered Nov 20 '18 at 14:08 Yakov DanYakov Dan 1,57999 silver badges1919 bronze badges thanks a lot! your solution captures the first occurence in both cases, correct? But I want to have the last occurence in the second case (letter_end) - any idea how to do this? – Dominik Scheld Nov 20 '18 at 14:11 Yes, the idea is that the loop should continue running until all lines are compared against letter_end. I've edited the code to reflect this. – Yakov Dan Nov 20 '18 at 14:13 add a comment |"
96,"
Q: Python text processing with regular expression ","1 This one-liner does the entire transformation: str = re.sub(r'(options) (\S+)', r'\2\n \1', str.replace('/path/', '/root/directory/path/') See a live demo of this code share|| follow |||| edited Sep 29 '13 at 21:43 answered Sep 29 '13 at 16:51 Bohemian♦Bohemian 335k7272 gold badges480480 silver badges609609 bronze badges add a comment |"
97,"
Q: Python text formatting ","1 Use string formatting: In [48]: def solve(a,b): a,b=str(a),str(b) spaces=len(a.split())-1 return ""{0} {1} {2}"".format(a,"".""*(68-len(a)-len(b)-spaces),b) ....: In [49]: print solve(a,b);print solve(p,q) Hello .......................................................... False Python rocks ................................................... True share|| follow |||| edited Apr 24 '13 at 18:42 answered Apr 24 '13 at 17:53 Ashwini ChaudharyAshwini Chaudhary 199k4343 gold badges378378 silver badges425425 bronze badges @user2248790 they're aligned, see you expected output you posted in the question body. – Ashwini Chaudhary Apr 24 '13 at 18:16 No, your output does not match your function. Replace (68 - len(a) - len(b) with (69 - len(a)) instead. – Martijn Pieters♦ Apr 24 '13 at 18:18 @MartijnPieters OP said that the total length of the string is fixed , but even his expected output contains unequal number of characters. He must clarify this first. – Ashwini Chaudhary Apr 24 '13 at 18:24 True, the wording is ambiguous, but the output formatting example is clear enough. – Martijn Pieters♦ Apr 24 '13 at 18:27 Accepted this as answer: simple. does the job and pythonic! – user2248790 Apr 25 '13 at 15:45 add a comment |"
98,"
Q: Unexpected read/write behaviour in 'r+' mode in python text files ","2 This definitely looks like a bug. A workaround is to explicitly seek the current file position before you write: fo = open(""C:/Users/Dell/Desktop/files/new.txt"",'r+') fo.read(5) fo.seek(fo.tell()) fo.write('random') fo.close() EDIT: As noted by @Blckknght, this is a known issue rooted from the C-level implementation of Windows. You can refer to Beginner Python: Reading and writing to the same file for references and discussions, although that linked question pertains to Python 2, where the behavior of the same code is different (the write either does nothing or produces an OSError). share|| follow |||| edited Apr 5 '19 at 18:23 answered Apr 5 '19 at 17:05 blhsingblhsing 63.7k66 gold badges2525 silver badges5858 bronze badges I believe this is documented (mis-)behavior. On some platforms, it's undefined what happens if you both read and write a file without putting a seek in between. – Blckknght Apr 5 '19 at 17:13 Thanks. I've found the prior discussions and have updated my answer accordingly. I'm not marking this question as a duplicate only because that question is for Python 2, where the behavior is different. – blhsing Apr 5 '19 at 17:59 add a comment |"
99,"
Q: Python text with alpha channel background ","4 The problem is that wm has the same color as the text, so you won't see anything if you draw the text onto it. Change the color of wm to transparent, like I did below (and remove the commas after initializing of width, height and opacity): from PIL import Image from PIL import ImageDraw from PIL import ImageFont from PIL import ImageEnhance width=854 height=480 opacity=0.8 text='copyright' filename = 'result.png' black = (0,0,0) white = (255,255,255) transparent = (0,0,0,0) font = ImageFont.truetype('verdana.ttf',15) wm = Image.new('RGBA',(width,height),transparent) im = Image.new('RGBA',(width,height),transparent) # Change this line too. draw = ImageDraw.Draw(wm) w,h = draw.textsize(text, font) draw.text(((width-w)/2,(height-h)/2),text,white,font) en = ImageEnhance.Brightness(wm) #en.putalpha(mask) mask = en.enhance(1-opacity) im.paste(wm,(25,25),mask) im.save(filename) share|| follow |||| edited May 27 '14 at 12:26 answered May 27 '14 at 11:04 miindlekmiindlek 3,24288 silver badges2323 bronze badges Thanks for your help, but your solution sadly doesn't work. I tested it, but the result is a completely black png file with the ""copyright"" string as grey text – gulden May 27 '14 at 12:12 Ok, I thought that is what you want. So, how should the picture look like? – miindlek May 27 '14 at 12:14 1 Sorry, I was a bit unclear. The background empty with nothing in there, alpha channel. Then a string in the center of it with a specified opacity. – gulden May 27 '14 at 12:17 1 Then you have to also set im transparent. Look at my update. – miindlek May 27 '14 at 12:25 add a comment |"
100,"
Q: Python text to image generation problems ","2 The Cairo cookbook has a number of examples for using Cairo, and the Python routines are almost mirror images of the C routines. share|| follow |||| answered Oct 27 '10 at 4:32 Ignacio Vazquez-AbramsIgnacio Vazquez-Abrams 645k118118 gold badges11581158 silver badges12341234 bronze badges add a comment |"
101,"
Q: Python text processing: NLTK and pandas ","11 The benefit of using a pandas DataFrame would be to apply the nltk functionality to each row like so: word_file = ""/usr/share/dict/words"" words = open(word_file).read().splitlines()[10:50] random_word_list = [[' '.join(np.random.choice(words, size=1000, replace=True))] for i in range(50)] df = pd.DataFrame(random_word_list, columns=['text']) df.head() text 0 Aaru Aaronic abandonable abandonedly abaction ... 1 abampere abampere abacus aback abalone abactor... 2 abaisance abalienate abandonedly abaff abacina... 3 Ababdeh abalone abac abaiser abandonable abact... 4 abandonable abandon aba abaiser abaft Abama ab... len(df) 50 txt = df.text.apply(word_tokenize) txt.head() 0 [Aaru, Aaronic, abandonable, abandonedly, abac... 1 [abampere, abampere, abacus, aback, abalone, a... 2 [abaisance, abalienate, abandonedly, abaff, ab... 3 [Ababdeh, abalone, abac, abaiser, abandonable,... 4 [abandonable, abandon, aba, abaiser, abaft, Ab... txt.apply(len) 0 1000 1 1000 2 1000 3 1000 4 1000 .... 44 1000 45 1000 46 1000 47 1000 48 1000 49 1000 Name: text, dtype: int64 As a result, you get the .count() for each row entry: txt = txt.apply(lambda x: nltk.Text(x).count('abac')) txt.head() 0 27 1 24 2 17 3 25 4 32 You can then sum the result using: txt.sum() 1239 share|| follow |||| answered Jan 14 '16 at 8:49 StefanStefan 29.4k88 gold badges5757 silver badges7171 bronze badges Thanks @Stefan, that just about resolves my problem however txt object is still a pandas data frame object which means that I can only use some of NLTK functions using apply, map or for loops. However, if I want to do something like nltk.Text(txt).concordance(""the"") I will run into problems. To resolve this I will still need to convert the entire text variable into a string and as we saw in my first example, that string will be truncated for some reason. Any thoughts on how to overcome this? Many thanks! – IVR Jan 16 '16 at 3:02 You can convert the entire text column into one list of words using: [t for t in df.text.tolist()] - either after creation or after .tokenize(). – Stefan Jan 18 '16 at 14:48 add a comment |"
102,"
Q: Python text encryption: rot13 ","1 First thing that may have caused you some problems - your string list has the n and the o switched, so you'll want to adjust that :) As for the algorithm, when you run: ccrypt = alph[ i+13 : i+14 ] Think of what happens when you get 25 back from the first iteration (for z). You are now looking for the index position alph[38:39] (side note: you can actually just say alph[38]), which is far past the bounds of the 26-character string, which will return '': In [1]: s = 'abcde' In [2]: s[2] Out[2]: 'c' In [3]: s[2:3] Out[3]: 'c' In [4]: s[49:50] Out[4]: '' As for how to fix it, there are a number of interesting methods. Your code functions just fine with a few modifications. One thing you could do is create a mapping of characters that are already 'rotated' 13 positions: alph = 'abcdefghijklmnopqrstuvwxyz' coded = 'nopqrstuvwxyzabcdefghijklm' All we did here is split the original list into halves of 13 and then swap them - we now know that if we take a letter like a and get its position (0), the same position in the coded list will be the rot13 value. As this is for an assignment I won't spell out how to do it, but see if that gets you on the right track (and @Makoto's suggestion is a perfect way to check your results). share|| follow |||| answered Dec 12 '12 at 23:44 RocketDonkeyRocketDonkey 30.5k77 gold badges6969 silver badges8080 bronze badges add a comment |"
103,"
Q: XOR Python Text Encryption/Decryption ","9 Here's a variation of the code example from XOR Cipher Wikipedia article: def xor(data, key): return bytearray(a^b for a, b in zip(*map(bytearray, [data, key]))) Example (Python 2): >>> one_time_pad = 'shared secret' >>> plaintext = 'unencrypted' >>> ciphertext = xor(plaintext, one_time_pad) >>> ciphertext bytearray(b'\x06\x06\x04\x1c\x06\x16Y\x03\x11\x06\x16') >>> decrypted = xor(ciphertext, one_time_pad) >>> decrypted bytearray(b'unencrypted') >>> plaintext == str(decrypted) True share|| follow |||| answered Dec 13 '13 at 16:14 jfsjfs 311k120120 gold badges768768 silver badges13801380 bronze badges This will truncate data so you need something like itertools.cycle for the secret key. – HTF Mar 13 '19 at 14:57 @HTF no. Think, why the variable is called one_time_pad. To be explicit, a ValueError may be raised if len(data)>len(key) – jfs Mar 13 '19 at 15:10 add a comment |"
104,"
Q: Making python text game invalid syntax error ","0 You have a lot of syntax errors... First, you forgot to indent all the code after the method definition. Second, you forgot a closing parentheses for this line of code, attack = input(""would you like to "" + a1 + a2 + a3 + a4 + ""or "" + run Next, you forgot a colon after this if statement, if attack == a1 Then, you forgot a lot of closing double quotes in these lines of code after HP!, print(""You dealt"" + d1 + "" damage! the enemy now has "" + enemy + ""HP!) print(""You dealt"" + d2 + "" damage! the enemy now has "" + enemy + ""HP!) print(""You dealt"" + d3 + "" damage! the enemy now has "" + enemy + ""HP!) print(""You dealt"" + d4 + "" damage! the enemy now has "" + enemy + ""HP!) Lastly, this line of code is indented one tab too much, print(""the monster de_stroyed you, and your blood will be painted in its lair."") So the lesson to be learned here is to be careful and check your code more often. After all the proper indentation is added, it should look like this, def fight(playerhp, a1, a2, a3, a4, run, d1, d2, d3, d4, enemy, edamage, armor, attack, enemytype, reward): print(""OH NO YOUVE ENCOUNTERED "" + enemy + ""WHAT DO YOU DO?"") time.sleep(1) while enemy > 0 and playerhp > 0: attack = input(""would you like to "" + a1 + a2 + a3 + a4 + ""or "" + run) if attack == a1: enemy = enemy-d1 print(""You dealt"" + d1 + "" damage! the enemy now has "" + enemy + ""HP!"") elif attack == a2: enemy = enemy-d2 print(""You dealt"" + d2 + "" damage! the enemy now has "" + enemy + ""HP!"") elif attack == a3: enemy = enemy - d3 print(""You dealt"" + d3 + "" damage! the enemy now has "" + enemy + ""HP!"") elif attack == a4: enemy = enemy-d4 print(""You dealt"" + d4 + "" damage! the enemy now has "" + enemy + ""HP!"") print(""the "" + enemytype + ""attacks! It deals "" + edamage + "" damage! you now have"" + playerhp + ""health!"") if enemy <=0: print(""the monster was slayn and the guts went everywhere :D. In its carcass you found "" + reward + ""gold!"") if playerhp <=0: print(""the monster de_stroyed you, and your blood will be painted in its lair."") share|| follow |||| edited Aug 1 '16 at 17:27 answered Aug 1 '16 at 2:21 Chris GongChris Gong 6,34544 gold badges2222 silver badges4040 bronze badges ok so i fixed all of that stuff, but the error is still there! it's on the line of enemy = enemy - d1 :( – bobbymg Aug 1 '16 at 2:47 @bobbymg can you edit the question to include your new and revised code? – Chris Gong Aug 1 '16 at 3:03 @bobbymg I addressed the edit in your question, and when I put the revised code in my answer into an IDE, there are no errors. Try copying my code snippet to see if there's still a problem. – Chris Gong Aug 1 '16 at 17:28 add a comment |"
105,"
Q: Parameter Error in Python - Text Adventure Game ","1 This question is posed in a way that is very difficult to answer, but from the looks of it, I would say that the error is here: class TextAdventureGame(game): It's saying module.__init__(), not TextAdventureGame.__init__(), which makes me think that game is a module that is getting used in an odd way. But without knowing any else about your code, what game is, or seeing a stack trace, we can't really do much more. share|| follow |||| answered Apr 25 '13 at 1:30 mattgmattg 1,48111 gold badge1010 silver badges1717 bronze badges add a comment |"
106,"
Q: Writing Unicode text to a text file? ","319 Deal exclusively with unicode objects as much as possible by decoding things to unicode objects when you first get them and encoding them as necessary on the way out. If your string is actually a unicode object, you'll need to convert it to a unicode-encoded string object before writing it to a file: foo = u'Δ, Й, ק, م, ๗, あ, 叶, 葉, and 말.' f = open('test', 'w') f.write(foo.encode('utf8')) f.close() When you read that file again, you'll get a unicode-encoded string that you can decode to a unicode object: f = file('test', 'r') print f.read().decode('utf8') share|| follow |||| answered May 18 '11 at 16:49 quasistoicquasistoic 4,37911 gold badge1313 silver badges1010 bronze badges Thanks. This runs without an error, but then if I open the text file, I see a bunch of weird symbols :) I need to copy and paste the text into a Wordpress page (don't ask). Is there any way I can actually print the symbols that are there? I guess not to a txt file, right, but maybe to something else? – simon May 18 '11 at 16:55 1 What are you using to open the text file? I'm guessing you're on Windows, and you're opening it in Notepad, which isn't too intelligent with encodings. What happens when you open it in Wordpad? – quasistoic May 18 '11 at 17:37 @quasistoic where does the file method come form? – Omar Cusma Fait Oct 3 '19 at 10:09 add a comment |"
107,"
A: CSV file written with Python has blank lines between each row
","862 In Python 2, open outfile with mode 'wb' instead of 'w'. The csv.writer writes \r\n into the file directly. If you don't open the file in binary mode, it will write \r\r\n because on Windows text mode will translate each \n into \r\n. In Python 3 the required syntax changed (see documentation links below), so open outfile with the additional parameter newline='' (empty string) instead. Examples: # Python 2 with open('/pythonwork/thefile_subset11.csv', 'wb') as outfile: writer = csv.writer(outfile) # Python 3 with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile: writer = csv.writer(outfile) Documentation Links https://docs.python.org/2/library/csv.html#csv.writer https://docs.python.org/3/library/csv.html#csv.writer share|| follow |||| edited Jan 17 at 7:16 answered Jul 27 '10 at 22:55 Mark TolonenMark Tolonen 113k1414 gold badges130130 silver badges193193 bronze badges 1 Anyway the @Mark Tolonen's answer did resolved many questions related to the extra line(s) added when saving a standard (no csv used) text file. – dlewin Sep 24 '15 at 9:32 1 For compatibility between 2.6/2.7 and 3, you can use io.open with the newlines argument. If you're still writing in 2.x, that seems like a better choice anyway since it's forward compatible. – jpmc26 Sep 27 '17 at 18:49 @jpmc26 Normally that's good advice, but the csv module doesn't work properly with io.open. There is a unicodecsv 3rd party module for Python 2.7 that works better. – Mark Tolonen Feb 26 '18 at 22:26 Any idea why the newline='' trick doesn't work in python3 with StringIO or TemporaryFile? – fmoo Apr 18 at 3:17 @fmoo define ""doesn't work"". They both work they way I expect. StringIO buffers the same code points that would be encoded to a file, and TemporaryFile supports the newline parameter, so it can be opened as with open. Ask a question with a sample program that isn't working. – Mark Tolonen Apr 18 at 9:27 | show 4 more comments"
108,"
Q: Python Imaging Library - Text rendering ","62 I came up with my own solution that I find acceptable. What I did was render the text large, like 3x the size it needs to be then scale it resize it down with antialiasing, it's not 100% perfect, but it's a hell of a lot better than default, and doesn't require cairo or pango. for example, image = Image.new(""RGBA"", (600,150), (255,255,255)) draw = ImageDraw.Draw(image) font = ImageFont.truetype(""resources/HelveticaNeueLight.ttf"", fontsize) draw.text((10, 0), txt, (0,0,0), font=font) img_resized = image.resize((188,45), Image.ANTIALIAS) and you endup with this result, which is a lot better than what I was getting before with the same font. share|| follow |||| answered Mar 25 '11 at 8:29 exivaexiva 1,12711 gold badge1010 silver badges1313 bronze badges I need the text in center, not left aligned. Is there a way to do that using the above solution? – Aarohi Kulkarni Dec 1 '15 at 7:04 3 Keep in mind though that fonts usually provide different shapes for different sizes (not just scaling up and down one shape). This is especially noticeable in smaller target sizes. – Tamás Szelei Feb 13 '16 at 20:29 Another way to put it: don't assume that you need to do this. I've found that rendering text using this function is quite slow, and this could add unnecessary slowdown. – Chris Anderson Jul 18 '17 at 13:48 add a comment |"
109,"
Q: Python Text Field in Maya ","1 This format won't work: cmds.setAttr('query=""focusdistacne.tx""') And default maya camera's don't have a focusdistance attribute -- there's centerOfInterest which is the target that the camera orbits around, and focalLength which affects FOV. I'm going to keep focusdistance below on the assumption that you're working with some kind of special camera, but you should check the maya docs the make sure you've got the right setting You need to save the name of the textfield you've created so the focusdistance function can ask it questions. You also need to fix that setAttr. I think you're looking for something like this: # in the part of the class where you create the textfield: self.focal_dist_field = cmds.textField(""focusdistance"", parent=""Extra"", enterCommand=self.focusdistance) and, when you've got the textField, you'll probably have to convert its contents to a number to be able to set them on a numeric attribute (unless your focusDistance attribute is a string?) def focusdistance(self): target = cmds.ls(sl=True)[0] # get the text, or a '0' if there's nothing entered value = cmds.textField(self. focal_dist_field, q=True, text=True) or ""0"" # convert the text to a number if possible # in practice you probably want a FloatField control # instead of a TextField so you can skip this try: numeric_value = float(value) except ValueError: numeric_value = 0 cmds.setAttr(target + "".focusdistance"", numeric_value) share|| follow |||| answered Aug 2 '17 at 21:49 theodoxtheodox 10.8k22 gold badges1717 silver badges3535 bronze badges Hello, I had the feeling that format wouldn't work which is why I came here. I went and triple checked the Maya docs/ Python Commands/ Maya itself and there is a focusDistance attribute that can be manipulated, but the argument type is linear as well. I went around and messed with what you mentioned and it all ended up working well; thank you for that! I did swap out the textField for a floatField and that helped shorten up everything. The only reason I wasn't using one earlier is because I didn't know they exist. In the end my code looked like: – Davis Fischer-Walker Aug 3 '17 at 0:33 add a comment |"
110,"
Q: Python: data vs. text? ","18 In a nutshell, the way text and data is handled in Py3k may arguably be the most ""breaking"" change in the language. By knowing and avoiding,when possible, the situations where some Python 2.6 logic will work differently than in 3.x, we can facilitate the migration when it happens. Yet we should expect that some parts of the 2.6 logic may require special attention and modifications for example to deal with distinct encodings etc. The idea behind BDFL's suggestion on slide 14 is probably to start ""using"" the same types which Py3k supports (and only these), namely unicode strings for strings (str type) and 8-bits byte sequences for ""data"" (bytes type). The term ""using"" in the previous sentence is used rather loosely since the semantics and associated storage/encoding for these types differs between the 2.6 and 3.x versions. In Python 2.6, the bytes type and the associated literal syntax (b'xyz') simply map to the str type. Therefore # in Py2.6 >>'mykey' == b'mykey' True b'mykey'.__class__ <class 'str'> # in Py3k >>>'mykey' == b'mykey' False b'mykey'.__class__ <class 'bytes'> To answer your question [in the remarks below], in 2.6 whether you use b'xyz' or 'xyz', Python understands it as the same and one thing : an str. What is important is that you understand these as [potentially/in-the-future] two distinct types with a distinct purpose: str for text-like info, and bytes for sequences of octets storing whatever data at hand. For example, again speaking close to your example/question, in Py3k you'll be able to have a dictionary with two elements which have a similar keys, one with b'mykey' and the other with 'mykey', however under 2.6 this is not possible, since these two keys are really the same; what matters is that you know this kind of things and avoid (or explicitly mark in a special fashion in the code) the situations where the 2.6 code will not work in 3.x. In Py3k, str is an abstract unicode string, a sequence of unicode code points (characters) and Python deals with converting this to/from its encoded form whatever the encoding might be (as a programmer you do have a say about the encoding but at the time you deal with string operations and such you do not need to worry about these details). In contrast, bytes is a sequence of 8-bits ""things"" which semantics and encoding are totally left to the programmer. So, even though Python 2.6 doesn't see a difference, by explicitly using bytes() / b'...' or str() / u'...', you... ... prepare yourself and your program to the upcoming types and semantics of Py3k ... make it easier for the automatic conversion (2to3 tool or other) of the source code, whereby the b in b'...' will remain and the u of u'...' will be removed (since the only string type will be unicode). For more info: Python 2.6 What's new (see PEP 3112 Bytes Literals) Python 3.0 What's New (see Text Vs. Data Instead Of Unicode Vs. 8-bit near the top) share|| follow |||| edited Nov 16 '09 at 16:25 answered Nov 15 '09 at 1:35 mjvmjv 63.8k1212 gold badges9292 silver badges144144 bronze badges 2 It's also good practice to make you really think about whether you mean a string of characters or a string of bytes! Only problem is of course you lose compatibility with Python 2.5 and earlier. – bobince Nov 15 '09 at 2:05 I understand why it is a good idea to do this, but I am still not clear as to how exactly. Unless a function explicitly expects a string of bytes, when do I use a string of bytes vs a string of characters? my_dict[b'mykey'] or my_dict[u'mykey']? When is it considered data, when is it considered text? – cschol Nov 15 '09 at 3:32 @cschol: I would suggest it's always a (unicode) string unless it's not. That is, always use unicode strings. When you find that that doesn't work for a specific problem, use the string of bytes. – Bryan Oakley Nov 16 '09 at 16:34 @Bryan Oakley ...and prey. ;) – mlvljr May 25 '11 at 21:27 add a comment |"
111,"
Q: Remove double and single square brackets from text file generated from python [closed] ","4 If you just want to remove all instances of square brackets from a string, you can do the following: s = ""[[ hello] [there]]"" s = s.replace(""["", """") s = s.replace(""]"", """") UPDATE: If you want the code to import the file contents, and make the changes: with open('/path/to/my_file.txt', 'r') as my_file: text = my_file.read() text = text.replace(""["", """") text = text.replace(""]"", """") # If you wish to save the updates back into a cleaned up file with open('/path/to/my_file_clean.txt', 'w') as my_file: my_file.write(text) share|| follow |||| edited Jan 22 '16 at 1:15 answered Jan 22 '16 at 0:57 ronrestronrest 1,08688 silver badges1414 bronze badges I am not sure how to use this. I already have a text file with square brackets – narayan.p Jan 22 '16 at 1:07 I updated my post to show you how to use it on your file – ronrest Jan 22 '16 at 1:15 Thanks a lot. This worked like a charm. I am accepting your answer. – narayan.p Jan 22 '16 at 1:22 Great! I'm happy it worked for you :) – ronrest Jan 22 '16 at 1:25 add a comment |"
112,"
Q: Python text game ","6 Replace your while loop with a for loop iterating over the string you want to print. That'll give you each letter in turn and stop your loop at the end. I also recommend placing this behaviour in a function like so: def typeText(text, delay=0.05): for character in text: print character, time.sleep(delay) typeText(""Hello, this is a text"") To solve the problem of the spaces, you then have 3 options, in order from most to least side effects: Switch to python3 which uses print as a function with an end argument that you can set to the empty string; from __future__ import print_function which will give you the same print function without all the other caveats from python3; replace print by sys.stdout.write(). This function is what print wraps around by default share|| follow |||| edited Sep 25 '13 at 16:41 answered Sep 25 '13 at 15:32 3Doubloons3Doubloons 1,8751313 silver badges2525 bronze badges Are you using either Python 3.x or the from __future__... statement? If so, you'll need to change that line for print(character, end='') – 3Doubloons Sep 25 '13 at 15:48 I'm using 2.7, i added in the print_function and it seems to work now – Matt Walker Sep 25 '13 at 15:49 One last thing, the >>> from the interpreter appears at the right side of the string instead of underneath, any idea why? – Matt Walker Sep 25 '13 at 15:50 That would be because we're not printing any newlines after printing. Just add a call to print() after the loop – 3Doubloons Sep 25 '13 at 15:53 add a comment |"
113,"
Q: Python text file trim colon ","2 Just store the data in a list, a sublist for each line split into firat name, last name and id. with open(""somefile.txt"", 'r') as f: data = [line.strip().split("":"") for line in f] You could also use a dict but there would be no real advantage unless you wanted to do lookups by name but even then names would need to be unique, if you just want to use each piece of data in a loop and not store all then use as your iterate: with open(""somefile.txt"", 'r') as f: for line in f: fn, ln , _id = line.strip().split("":"") share|| follow |||| answered Sep 26 '15 at 14:10 Padraic CunninghamPadraic Cunningham 148k1717 gold badges164164 silver badges243243 bronze badges No prob, you're weclome. – Padraic Cunningham Sep 26 '15 at 14:24 add a comment |"
114,"
Q: Python Text Extraction from parsed web pages ","1 I have done a little bit of this and I recommend the combination of Mechanize and BeautifulSoup. I would recommend parsing the HTML tree with beautiful soup and looking for a distinctive tag that identifies the content, perhaps: <div id=""article""> Then you can just take that node from the ""soup"". share|| follow |||| answered Aug 31 '12 at 13:11 jsjjsj 7,0571212 gold badges4848 silver badges8484 bronze badges 1 Yes, BeautifulSoup is the way to go as it also works with improper XHTML. – Dr. Jan-Philip Gehrcke Aug 31 '12 at 13:18 ok, great suggestion, and I already have this pulling the html page and I have masses of html, each page might be different or not have distinct markings of where the content is....I don't know each page and a lot is going to be done as automatically & algorithmic as possible without having to focus energy on finding out each page's idiosyncrasies. – blueblank Aug 31 '12 at 13:28 add a comment |"
115,"
Q: Clustering text in Python [closed] ","44 The quality of text-clustering depends mainly on two factors: Some notion of similarity between the documents you want to cluster. For example, it's easy to distinguish between newsarticles about sports and politics in vector space via tfidf-cosine-distance. It's a lot harder to cluster product-reviews in ""good"" or ""bad"" based on this measure. The clustering method itself. You know how many cluster there'll be? Ok, use kmeans. You don't care about accuracy but want to show a nice tree-structure for navigation of search-results? Use some kind of hierarchical clustering. There is no text-clustering solution, that would work well under any circumstances. And therefore it's probably not enough to take some clustering software out of the box and throw your data at it. Having said that, here's some experimental code i used some time ago to play around with text-clustering. The documents are represented as normalized tfidf-vectors and the similarity is measured as cosine distance. The clustering method itself is majorclust. import sys from math import log, sqrt from itertools import combinations def cosine_distance(a, b): cos = 0.0 a_tfidf = a[""tfidf""] for token, tfidf in b[""tfidf""].iteritems(): if token in a_tfidf: cos += tfidf * a_tfidf[token] return cos def normalize(features): norm = 1.0 / sqrt(sum(i**2 for i in features.itervalues())) for k, v in features.iteritems(): features[k] = v * norm return features def add_tfidf_to(documents): tokens = {} for id, doc in enumerate(documents): tf = {} doc[""tfidf""] = {} doc_tokens = doc.get(""tokens"", []) for token in doc_tokens: tf[token] = tf.get(token, 0) + 1 num_tokens = len(doc_tokens) if num_tokens > 0: for token, freq in tf.iteritems(): tokens.setdefault(token, []).append((id, float(freq) / num_tokens)) doc_count = float(len(documents)) for token, docs in tokens.iteritems(): idf = log(doc_count / len(docs)) for id, tf in docs: tfidf = tf * idf if tfidf > 0: documents[id][""tfidf""][token] = tfidf for doc in documents: doc[""tfidf""] = normalize(doc[""tfidf""]) def choose_cluster(node, cluster_lookup, edges): new = cluster_lookup[node] if node in edges: seen, num_seen = {}, {} for target, weight in edges.get(node, []): seen[cluster_lookup[target]] = seen.get( cluster_lookup[target], 0.0) + weight for k, v in seen.iteritems(): num_seen.setdefault(v, []).append(k) new = num_seen[max(num_seen)][0] return new def majorclust(graph): cluster_lookup = dict((node, i) for i, node in enumerate(graph.nodes)) count = 0 movements = set() finished = False while not finished: finished = True for node in graph.nodes: new = choose_cluster(node, cluster_lookup, graph.edges) move = (node, cluster_lookup[node], new) if new != cluster_lookup[node] and move not in movements: movements.add(move) cluster_lookup[node] = new finished = False clusters = {} for k, v in cluster_lookup.iteritems(): clusters.setdefault(v, []).append(k) return clusters.values() def get_distance_graph(documents): class Graph(object): def __init__(self): self.edges = {} def add_edge(self, n1, n2, w): self.edges.setdefault(n1, []).append((n2, w)) self.edges.setdefault(n2, []).append((n1, w)) graph = Graph() doc_ids = range(len(documents)) graph.nodes = set(doc_ids) for a, b in combinations(doc_ids, 2): graph.add_edge(a, b, cosine_distance(documents[a], documents[b])) return graph def get_documents(): texts = [ ""foo blub baz"", ""foo bar baz"", ""asdf bsdf csdf"", ""foo bab blub"", ""csdf hddf kjtz"", ""123 456 890"", ""321 890 456 foo"", ""123 890 uiop"", ] return [{""text"": text, ""tokens"": text.split()} for i, text in enumerate(texts)] def main(args): documents = get_documents() add_tfidf_to(documents) dist_graph = get_distance_graph(documents) for cluster in majorclust(dist_graph): print ""========="" for doc_id in cluster: print documents[doc_id][""text""] if __name__ == '__main__': main(sys.argv) For real applications, you would use a decent tokenizer, use integers instead of token-strings and don't calc a O(n^2) distance-matrix... share|| follow |||| edited Apr 22 '14 at 10:49 answered Nov 24 '09 at 15:58 ephesephes 1,3411010 silver badges1616 bronze badges add a comment |"
116,"
Q: Python display text with font & color? ","56 Yes. It is possible to draw text in pygame: # initialize font; must be called after 'pygame.init()' to avoid 'Font not Initialized' error myfont = pygame.font.SysFont(""monospace"", 15) # render text label = myfont.render(""Some text!"", 1, (255,255,0)) screen.blit(label, (100, 100)) share|| follow |||| edited Mar 3 '14 at 12:45 answered Apr 9 '12 at 18:18 veisetveiset 1,6331515 silver badges1515 bronze badges 1 Alright, can I install a font if there isn't one installed on the computer somehow? – maxhud Apr 9 '12 at 18:28 2 I am not sure, but it looks like you can. According to the documentation of font.SysFont it loads the font from the system fonts (pygame.org/docs/ref/font.html#pygame.font.SysFont). – veiset Apr 9 '12 at 18:44 9 @maxhud: yes. Use pygame.font.Font(""path/to/font.ttf"",size). – imallett Mar 1 '14 at 19:41 add a comment |"
117,"
Q: Python Text And Row reading error ","0 You have probably an empty line at the end of classA.txt. Use for row in csv_a: if row: # or better if len(row)==4: row[2] = int(row[2]) print(row[2]) to check if the line is not empty, before you access the second/third entry. share|| follow |||| answered Nov 26 '15 at 11:42 jofeljofel 2,8891313 silver badges2626 bronze badges Thankyou I have sorted that error and now onto the next – Roodolpha Nov 26 '15 at 11:53 add a comment |"
118,"
Q: Python recursive folder read ","334 Make sure you understand the three return values of os.walk: for root, subdirs, files in os.walk(rootdir): has the following meaning: root: Current path which is ""walked through"" subdirs: Files in root of type directory files: Files in root (not in subdirs) of type other than directory And please use os.path.join instead of concatenating with a slash! Your problem is filePath = rootdir + '/' + file - you must concatenate the currently ""walked"" folder instead of the topmost folder. So that must be filePath = os.path.join(root, file). BTW ""file"" is a builtin, so you don't normally use it as variable name. Another problem are your loops, which should be like this, for example: import os import sys walk_dir = sys.argv[1] print('walk_dir = ' + walk_dir) # If your current working directory may change during script execution, it's recommended to # immediately convert program arguments to an absolute path. Then the variable root below will # be an absolute path as well. Example: # walk_dir = os.path.abspath(walk_dir) print('walk_dir (absolute) = ' + os.path.abspath(walk_dir)) for root, subdirs, files in os.walk(walk_dir): print('--\nroot = ' + root) list_file_path = os.path.join(root, 'my-directory-list.txt') print('list_file_path = ' + list_file_path) with open(list_file_path, 'wb') as list_file: for subdir in subdirs: print('\t- subdirectory ' + subdir) for filename in files: file_path = os.path.join(root, filename) print('\t- file %s (full path: %s)' % (filename, file_path)) with open(file_path, 'rb') as f: f_content = f.read() list_file.write(('The file %s contains:\n' % filename).encode('utf-8')) list_file.write(f_content) list_file.write(b'\n') If you didn't know, the with statement for files is a shorthand: with open('filename', 'rb') as f: dosomething() # is effectively the same as f = open('filename', 'rb') try: dosomething() finally: f.close() share|| follow |||| edited Aug 21 '14 at 7:50 answered Feb 6 '10 at 9:48 AndiDogAndiDog 57.2k1515 gold badges143143 silver badges194194 bronze badges 4 Superb, lots of prints to understand what's going on and it works perfectly. Thanks! +1 – Brock Woolf Feb 6 '10 at 9:52 16 Heads up to anyone as dumb/oblivious as me... this code sample writes a txt file to each directory. Glad I tested it in a version controlled folder, though everything I need to write a cleanup script is here too :) – Steazy Sep 24 '14 at 23:56 that second (longest) code snippet worked very well, saved me a lot of boring work – amphibient Apr 24 '18 at 21:01 1 Since speed if obviously the most important aspect, os.walk is not bad, though I came up with an even faster way via os.scandir. All glob solutions are a lot slower than walk & scandir. My function, as well as a complete speed analysis, can be found here: stackoverflow.com/a/59803793/2441026 – user136036 Jan 18 at 18:57 add a comment |"
119,"
Q: Python Text File To List ","1 Try: all_words = word.split() print(all_words) split returns a list, it does not change the original string. Update (to get all words in a single list): RanTxtString = open(""randomText.txt"", ""r"") all_words=[] for word in RanTxtString: all_words = all_words+word.split() print(all_words) share|| follow |||| edited Jun 22 '18 at 10:41 answered Jun 22 '18 at 10:23 Taohidul IslamTaohidul Islam 4,00611 gold badge1111 silver badges2929 bronze badges Thanks, it is now breaking down each line into individual strings. However it stores each line separately. c:\Scripts>python TxtToList.py ['Lorem', 'ipsum', 'dolor', 'sit', 'amet,', 'Word1', 'consectetur', 'adipiscing', 'elit.', 'Nunc', 'fringilla', 'arcu', 'congue', 'metus', 'aliquam', 'mollis.'] ['Mauris', 'nec', 'maximus', 'purus.', 'Maecenas', 'sit', 'amet', 'pretium', 'tellus.', 'Praesent', 'Word3', 'sed', 'rhoncus', 'eo.', 'Duis', 'id', 'commodo', 'orci.'] ['Quisque', 'at', 'dignissim', 'lacus.'] – LockTheTaskBar Jun 22 '18 at 10:26 Unfortunately not, I am trying to store each one in a single list. – LockTheTaskBar Jun 22 '18 at 10:35 That means words of every different line will be stored in different list?Or a single list will contain all the words? – Taohidul Islam Jun 22 '18 at 10:38 1 Yes single list will contain all the words, thank you for this, greatly appreciated. – LockTheTaskBar Jun 22 '18 at 11:21 add a comment |"
120,"
Q: Python Text Dungeon Project ","0 You could create a user dict which has like items_in_hand which would be 0. whenever you try to pick up an item,check if items_in_hand is less than 3 then increment it. For better code quality you can add the index variable also in that user dict. so you code would be like user = {'index':1 , 'items_in_hand' : 0} if a =='grab': if user['items_in_hand'] < 3: print(""You grabbed the item"") user['items_in_hand']+=1 else : print(""you dont have space"") i havent added any error checking code. NOTE: the best approach would be to use objects but one step at a time. share|| follow |||| answered Mar 4 '16 at 6:52 GIRISH RAMNANIGIRISH RAMNANI 51955 silver badges1515 bronze badges given what the game looks like, I think it is wiser to assume this should remain simple procedural programming. – Mathieu B Mar 4 '16 at 7:03 1 I actually told that because i saw the line [""monster"", ""monster guard"", ""monster BOSS""] so the developer is interested in using different actors with overlaping behavior, thats why i suggested that. – GIRISH RAMNANI Mar 4 '16 at 7:08 add a comment |"
121,"
Q: Python : How to convert markdown formatted text to text ","43 This module will help do what you describe: http://www.freewisdom.org/projects/python-markdown/Using_as_a_Module Once you have converted the markdown to HTML, you can use a HTML parser to strip out the plain text. Your code might look something like this: from BeautifulSoup import BeautifulSoup from markdown import markdown html = markdown(some_html_string) text = ''.join(BeautifulSoup(html).findAll(text=True)) share|| follow |||| edited Apr 17 '09 at 19:40 answered Apr 17 '09 at 19:27 Jason CoonJason Coon 13.9k99 gold badges3434 silver badges4949 bronze badges 1 it seems like convert to html.. I need to convert to plain text.. like stackoverflow, in the homepage question summary, it removes the formatting – Krish Apr 17 '09 at 19:30 Thanks coonj.. Good to know about BeatifulSoup – Krish Apr 18 '09 at 1:35 add a comment |"
122,"
Q: python text encoding program ",1 you could also do something like: def run_length_encoder(str_): compressedString = '' countConsecutive = 0 strLen = len(str_) for i in range(strLen): countConsecutive += 1 if i + 1 >= strLen or str_[i] != str_[i + 1]: compressedString += '' + str_[i] + str(countConsecutive) countConsecutive = 0 return compressedString sample = 'aaabbac' result = list(run_length_encoder(sample)) print(result) share|| follow |||| answered Jun 12 '18 at 4:42 Sudhir BastakotiSudhir Bastakoti 90.5k1414 gold badges132132 silver badges144144 bronze badges if a letter is repeating it should be given twice with the number of times it is repeating.....for example aaa should be aa3 while a should just be a but aa should be aa2 – Anvesh Sunkara Jun 12 '18 at 10:34 add a comment |
123,"
Q: How to return focus to editor in VS Code macro sending Python text to Debug Console? ","0 This works, using a different extension: ""multiCommand.commands"": [ // requires vscode:extension/ryuta46.multi-command { // ctrl+enter, editorTextFocus && editorHasSelection && editorLangId == 'python' && inDebugMode ""command"": ""multiCommand.selectionToReplAndReturnToEditor"", ""sequence"": [ ""editor.debug.action.selectionToRepl"", ""workbench.action.focusActiveEditorGroup"", ] }, } share|| follow |||| answered Oct 11 '18 at 8:53 bersbers 1,9071313 silver badges3030 bronze badges add a comment |"
124,"
Q: Python: Text localiser using Tensorflow ","0 Great thing that you're getting started, I believe before thinking about the actual implementation using tensorflow or any other library, you should first try to understand the problem in regards with the basic domain of the problem itself. I'm not really sure what are you exactly trying to achieve but to a rough idea I'm guessing it's about trying to find is a statement turns out to be a benificial to the company or not, something like of semantic analysis type of problem. So I strongly believe that, first you should try to learn the various methodologies related to semantic analysis and find the most appropriate technique. In short theory/understanding before the actual code. Finally i would suggest you ask such theoratical questions on stack exchange of AI, here in SO we generally deal with code or something that of intermediate to code. I hope that makes sense? ;) drop a comment if any doubts. share|| follow |||| answered Nov 7 '19 at 10:35 P.hunterP.hunter 1,08022 gold badges1212 silver badges3232 bronze badges I don't want it to predict anything. I just want it to identify specific numbers or words (names) in the financial statements. The goal is to have the key information in the financial statements automatically organized in a spreadsheet without having to type it in manually. However, the name of the ceo or the amount of profit for example varies every time. Their place in the financial statement also varies. I need the neural network to figure out the right name or the right number by looking at the context, just like a human does. – Lohant00 Nov 8 '19 at 9:03 add a comment |"
125,"
Q: Python text document similarities (w/o libraries) [closed] ","0 Here some ideas: use new_str = str.upper() so beer and Beer will be same (if you need this) use list = str.split() to make a list of the words in your string. use set = set(list) to get rid of double words if needed. start with an empty word_list. Copy the first set in the word_list. In the following steps you can loop over the entries in your set and check if they are part of your word_list. for word in set: if word not in word_list: word_list.append(word) Now you can make a multi-hot vector from your sentence. (1 if word_list[i] in sentence else 0) Don't forget to make your multi-hot vectors longer (additional zeros) if you add a word to word_list. last step: make a matrix from your vectors. share|| follow |||| answered Nov 27 '18 at 16:19 tifi90tifi90 13899 bronze badges Hi tifi90, Thank you a lot for your help. We had already tried the first four steps, but the last 3 steps did the trick! Thanks! – Daniel N Nov 28 '18 at 12:22 add a comment |"
126,"
Q: Stop Condition for Python Text Parser ","1 I would change your first parser. Instead of doing line by line comparisons how it sounds like you are now, you could do something similar: Load the whole file into a string. Then, use the split method to turn it into a list. Then you can loop over the list and perform operations on different elements of the list, including on future elements of the list, which are like lines of the text file (although you probably shouldn't delete any elements of the list). Anyway, you can then join it back into a string and save the file. result=None with open(""myFile.txt"", ""rb"") as FILE: #This is for Python 3.x; for Python 2.x do ""r"" instead of ""rb"" result=FILE.read() result_list=result.split(""\n"") i=0 while i<len(result_list): #Perform your actions here and do result_list[i+1] to get the next line i+=1 result=""\n"".join(result_list) with open(""myFile.txt"", ""w"") as FILE: FILE.write(result) EDIT: The following question/answer might also help you: Reading specific lines only (Python) share|| follow |||| edited May 23 '17 at 12:06 Community♦ 111 silver badge answered Sep 9 '15 at 22:38 BrōtsyorfuzthrāxBrōtsyorfuzthrāx 2,87111 gold badge2222 silver badges4141 bronze badges add a comment |"
127,"
Q: Convert text-processing script to web app in Python ","1 I believe using bottle micro web framework whould be the easiest and fastest. you could also checkout flask microwebframework which is pretty much the same idea share|| follow |||| answered Nov 23 '11 at 10:30 alonisseralonisser 8,9641515 gold badges6868 silver badges113113 bronze badges add a comment |"
128,"
Q: Python Text Matrix using NumPy and pandas ","1 It seems that you are using BeautifulSoup to get tables from HTML. I have successfully used Pandas to extract tables from webpages. Using example from pandas doc (Here's the link): In [1]: import pandas as pd In [2]: tables = pd.read_html('http://www.fdic.gov/bank/individual/failed/banklist.html') In this case tables only has one table. You can do print tables[0] to view or tables[0].to_csv(local_csv_output) to save as CSV file. pd.read_html can read both URL (http...) and local HTML files. If you have multiple tables, just manually find out which one you need by printing them. Give it a try and let us know. If you already have variable a11,a11,... assigned (which seems weird -- they should be in list), then you can create pandas DataFrame: In [38]: df = pd.DataFrame ([[a11,a12,a13], [a21,a22,a23], [a31,a32,a33], [a41,a42,a44]], columns=['Col1','Col2','Col3']) In [39]: df Out[39]: Col1 Col2 Col3 0 1 a Text1 1 2 B Text2 2 3 c Text3 3 4 D Text4 And df.to_csv('output_csv_file.csv') will save it to csv. share|| follow |||| edited May 31 '14 at 19:10 answered May 31 '14 at 18:08 Happy001Happy001 4,20211 gold badge1616 silver badges1313 bronze badges Unfortunately, my web scraping stuff is more involved than just finding tables. I just did a simple example in my question. Plus, I already know BeautifulSoup, so would rather just use that to get my data and then put it into pandas. – bill999 May 31 '14 at 18:50 add a comment |"
129,"
Q: How do I read text from the (windows) clipboard from python? ","94 You can use the module called win32clipboard, which is part of pywin32. Here is an example that first sets the clipboard data then gets it: import win32clipboard # set clipboard data win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardText('testing 123') win32clipboard.CloseClipboard() # get clipboard data win32clipboard.OpenClipboard() data = win32clipboard.GetClipboardData() win32clipboard.CloseClipboard() print data An important reminder from the documentation: When the window has finished examining or changing the clipboard, close the clipboard by calling CloseClipboard. This enables other windows to access the clipboard. Do not place an object on the clipboard after calling CloseClipboard. share|| follow |||| edited Jun 14 '18 at 16:01 Graham 1,19799 silver badges2424 bronze badges answered Sep 19 '08 at 11:20 SakinSakin 2,78722 gold badges2020 silver badges2727 bronze badges 6 Do you know if there is a way to use the `with´ statement ? – Norfeldt Jul 11 '13 at 14:21 3 Worth noting, in py34, win7, SetClipboardText did not work without a preceding call to EmptyClipboard – CoderTao Jan 12 '15 at 21:57 This module is useful if you want to perform more complex operations, e.g. getting the HTML-formatted content out of clipboard. See stackoverflow.com/questions/17298897/… – xji Sep 8 '16 at 8:23 1 @Norfeldt If there is no native way, you can easily create your own custom object that supports ""with"" – Elijas Jun 28 '18 at 18:38 add a comment |"
130,"
Q: In Python, given a URL to a text file, what is the simplest way to read the contents of the … ","109 Edit 09/2016: In Python 3 and up use urllib.request instead of urllib2 Actually the simplest way is: import urllib2 # the lib that handles the url stuff data = urllib2.urlopen(target_url) # it's a file like object and works just like a file for line in data: # files are iterable print line You don't even need ""readlines"", as Will suggested. You could even shorten it to: * import urllib2 for line in urllib2.urlopen(target_url): print line But remember in Python, readability matters. However, this is the simplest way but not the safe way because most of the time with network programming, you don't know if the amount of data to expect will be respected. So you'd generally better read a fixed and reasonable amount of data, something you know to be enough for the data you expect but will prevent your script from been flooded: import urllib2 data = urllib2.urlopen(""http://www.google.com"").read(20000) # read only 20 000 chars data = data.split(""\n"") # then split it into lines for line in data: print line * Second example in Python 3: import urllib.request # the lib that handles the url stuff for line in urllib.request.urlopen(target_url): print(line.decode('utf-8')) #utf-8 or iso8859-1 or whatever the page encoding scheme is share|| follow |||| edited Jan 9 at 14:03 Simon Streicher 1,90511 gold badge1212 silver badges2323 bronze badges answered Sep 8 '09 at 11:04 e-satise-satis 477k103103 gold badges276276 silver badges314314 bronze badges add a comment |"
131,"
Q: Python text-to-speech module [duplicate] ","7 There is an official fix now. It had to do with an upstream dependency of gtts, gtts-token. It has been fixed in gtts-token==1.1.2 The issue was fixed after I did a fresh install of both gtts and gtts-token. Now it is working. Thanks to open source gods and @carrey-cole Link: https://github.com/pndurette/gTTS/issues/137 share|| follow |||| edited Nov 29 '18 at 12:32 Rarblack 3,55044 gold badges1515 silver badges2929 bronze badges answered Sep 28 '18 at 12:21 bipin_sbipin_s 33322 silver badges1010 bronze badges You used pip to do a fresh install? – arqam Oct 3 '18 at 5:25 yes. first uninstall gtts-token (version 1,1.1)using pip.then do fresh install of pip install gtts-token. – bipin_s Oct 4 '18 at 0:10 add a comment |"
132,"
Q: python text file variable search [closed] ","2 I suggest that you construct a dictionary mapping fruit-names to the amount and then go from there. >>> with open('example.txt') as f: ... result = {k:int(v) for k,v in re.findall('([^:\s]+):\s*(\d+)', f.read())} ... >>> result {'Grapes': 7, 'Plums': 15, 'Apples': 1, 'Oranges': 33} >>> 'Grapes' in result True >>> 'Bananas' in result False >>> result['Grapes'] 7 example.txt has the content Apples: 01, Oranges: 33, Grapes: 07, Plums: 15 share|| follow |||| answered Jan 12 '16 at 23:58 timgebtimgeb 55.7k1414 gold badges7777 silver badges107107 bronze badges This converts into a dictionary but when i call result['grapes'] i get grapes ""new line"" 7 – jumpman8947 Jan 13 '16 at 0:45 Never mind i had a print statement in my code that i did not remove. – jumpman8947 Jan 13 '16 at 1:29 add a comment |"
133,"
Q: Python - Text in form disappears ","0 string = self.request.get(""text"") since your form item name is text this harkens back to the fact that you should use good identifiers ... since you have no post variables named word ... trying to get that post variable gives an empty string share|| follow |||| answered Nov 6 '14 at 0:52 Joran BeasleyJoran Beasley 83.5k1111 gold badges106106 silver badges139139 bronze badges add a comment |"
134,"
Q: How to get Python exception text ","58 Well, I found out how to do it. Without boost (only error message, because code to extract info from traceback is too heavy to post it here): PyObject *ptype, *pvalue, *ptraceback; PyErr_Fetch(&ptype, &pvalue, &ptraceback); //pvalue contains error message //ptraceback contains stack snapshot and many other information //(see python traceback structure) //Get error message char *pStrErrorMessage = PyString_AsString(pvalue); And BOOST version try{ //some code that throws an error }catch(error_already_set &){ PyObject *ptype, *pvalue, *ptraceback; PyErr_Fetch(&ptype, &pvalue, &ptraceback); handle<> hType(ptype); object extype(hType); handle<> hTraceback(ptraceback); object traceback(hTraceback); //Extract error message string strErrorMessage = extract<string>(pvalue); //Extract line number (top entry of call stack) // if you want to extract another levels of call stack // also process traceback.attr(""tb_next"") recurently long lineno = extract<long> (traceback.attr(""tb_lineno"")); string filename = extract<string>(traceback.attr(""tb_frame"").attr(""f_code"").attr(""co_filename"")); string funcname = extract<string>(traceback.attr(""tb_frame"").attr(""f_code"").attr(""co_name"")); ... //cleanup here share|| follow |||| edited Apr 11 '13 at 20:23 JoshL 9,6981010 gold badges5050 silver badges5757 bronze badges answered Sep 13 '09 at 20:05 Anton KiselevAnton Kiselev 1,18011 gold badge99 silver badges1212 bronze badges 1 Awesome, this is exactly what I have been looking for... works great. – Kyle C Jan 4 '11 at 1:44 This is nice. I've discovered in some cases (for me, a boost;:python::import of something not in my PYTHONPATH) ptraceback will be 0, so I'd protect against usage of a ptraceback if it is 0. Also, can you comment on what we can do with extype? I suppose printing the text of the python exception type is meaningful. How do we do that? – D. A. Mar 6 '14 at 17:18 2 One additional question: aren't we leaking memory in above? What frees objects returned by PyErr_Fetch? (I am not sure about both CPython and boost::pythoon cases) – elmo Apr 10 '14 at 8:41 I would sure be interested in that non-boost code to extract the traceback. Or just some description of the structure, for which I can't seem to find documentation. – solublefish Feb 10 '15 at 16:43 According to doc, ptype pvalue ptraceback can be have no content, null, even the pyobject pointer itself is not null. – m. c. Dec 7 '16 at 21:50 add a comment |"
135,"
Q: Python: Text file into dictionary ","0 The following creates a dictionary from an arbitrary length version of the file format provided. Keep in mind that there can only be 1 value for each key, so repeated keys will be overwritten by later values. x = 'path\\to\\file' temp_dict = dict() chat_dict = dict() with open(x, 'r') as f: for line in f: splitLine = line.split() temp_dict[(splitLine[0])] = "" "".join(splitLine[2:]) # Dirty hack to remove timestamp temp_array = temp_dict.values() chat_dict.update(dict(s.split(':')[:2] for s in temp_array)) print(chat_dict) share|| follow |||| answered Aug 3 '17 at 17:18 Alan LeuthardAlan Leuthard 75855 silver badges1111 bronze badges I have repeating keys ! – Gaurav Saini Aug 4 '17 at 4:11 add a comment |"
136,"
Q: Python Text processing (str.contains) ","0 You can't. At least, not in a simple or standardized way. Even the basics, like how you define a ""word,"" are a lot more complex than you probably imagine. Both word parsing and lexical proximity (e.g. ""are two words within distance D of one another in sentence s?"") is the realm of natural language processing (NLP). NLP and proximity searches are not part of basic Pandas, nor of Python's standard string processing. You could import something like NLTK, the Natural Language Toolkit to solve this problem in a general way, but that's a whole 'nother story. Let's look at a simple approach. First you need a way to parse a string into words. The following is rough by NLP standards, but will work for simpler cases: def parse_words(s): """""" Simple parser to grab English words from string. CAUTION: A simplistic solution to a hard problem. Many possibly-important edge- and corner-cases not handled. Just one example: Hyphenated words. """""" return re.findall(r""\w+(?:'[st])?"", s, re.I) E.g.: >>> parse_words(""and don't think this day's last moment won't come "") ['and', ""don't"", 'think', 'this', ""day's"", 'last', 'moment', ""won't"", 'come'] Then you need a way to find all the indices in a list where a target word is found: def list_indices(target, seq): """""" Return all indices in seq at which the target is found. """""" indices = [] cursor = 0 while True: try: index = seq.index(target, cursor) except ValueError: return indices else: indices.append(index) cursor = index + 1 And finally a decision making wrapper: def words_within(target_words, s, max_distance, case_insensitive=True): """""" Determine if the two target words are within max_distance positiones of one another in the string s. """""" if len(target_words) != 2: raise ValueError('must provide 2 target words') # fold case for case insensitivity if case_insensitive: s = s.casefold() target_words = [tw.casefold() for tw in target_words] # for Python 2, replace `casefold` with `lower` # parse words and establish their logical positions in the string words = parse_words(s) target_indices = [list_indices(t, words) for t in target_words] # words not present if not target_indices[0] or not target_indices[1]: return False # compute all combinations of distance for the two words # (there may be more than one occurance of a word in s) actual_distances = [i2 - i1 for i2 in target_indices[1] for i1 in target_indices[0]] # answer whether the minimum observed distance is <= our specified threshold return min(actual_distances) <= max_distance So then: >>> s = ""and don't think this day's last moment won't come at last"" >>> words_within([""THIS"", 'last'], s, 2) True >>> words_within([""think"", 'moment'], s, 2) False The only thing left to do is map that back to Pandas: df = pd.DataFrame({'desc': [ 'My latest Data job was an Analyst', 'some day my prince will come', 'Oh, somewhere over the rainbow bluebirds fly', ""Won't you share a common disaster?"", 'job! rainbow! analyst.' ]}) df['ja2'] = df.desc.apply(lambda x: words_within([""job"", 'analyst'], x, 2)) df['ja3'] = df.desc.apply(lambda x: words_within([""job"", 'analyst'], x, 3)) This is basically how you'd solve the problem. Keep in mind, it's a rough and simplistic solution. Some simply-posed questions are not simply-answered. NLP questions are often among them. share|| follow |||| answered Dec 30 '17 at 18:01 Jonathan EuniceJonathan Eunice 14.2k44 gold badges5959 silver badges6565 bronze badges add a comment |"
137,"
Q: Python Text file layouts ","0 What you want is either a simple .ini file or a proper database file. A .ini file can look like this: [Team1] name=x foo=1 bar=2 [Team2] ... .ini files can be read in Python with the configparser module: >>> import configparser >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: print(key) ... Read more here: https://docs.python.org/3/library/configparser.html More information on database files and how to use them in Python: https://docs.python.org/3/library/sqlite3.html share|| follow |||| answered Mar 17 '17 at 20:13 dasdas 6577 bronze badges add a comment |"
138,"
Q: Python Text Classification Error - expected string or bytes-like object ","4 Try str(dataset['text'][i]). That'll convert it to a str object, from whatever type it was before. share|| follow |||| answered Jun 5 '17 at 16:29 cs95cs95 203k4545 gold badges323323 silver badges392392 bronze badges add a comment |"
139,"
Q: Python : Text to ASCII & ASCII to text converter program ","2 message2 = ord(ch) + 2 makes message2 an integer, and so of course you cannot then call split on it -- it's a single int representing a single character, why ever would you want to split it?! Plus, you're resetting encodedmessage to the empty string each time through the loop, so once you've fixed the split weirdness (by simply removing it!) you'll only see the last characted of course; move the encodedmessage = '' to before the loop instead. share|| follow |||| answered Aug 17 '10 at 18:17 Alex MartelliAlex Martelli 699k140140 gold badges11041104 silver badges13081308 bronze badges It's bad practice to use the += operator on strings in general, isn't it? You're creating a new string each time it's called. Perhaps it would be better to keep a list of characters and then do ''.join(whateverlist)? – Alex Bliskovsky Aug 17 '10 at 18:21 Alex: it is, and I considered this as well. But I think it would confuse the OP, so it's forgiveable on this case. – rbp Aug 17 '10 at 18:23 @Alex Bliskovsky: Although its often repeated that ''.join() is faster than string concatenation, I think with modern releases of Python, that is not necessarily true. In fact, in this microtest, paste.ubuntu.com/479529, python -mtimeit -s'import test' 'test.add_strings(1000) takes 201us, while python -mtimeit -s'import test' 'test.join_strings(1000) takes 250us per loop. I'd be interested to hear if @Alex Martelli would weigh in on this. – unutbu Aug 17 '10 at 18:34 Ah, it looks like @Alex Martelli has already answered this here: stackoverflow.com/questions/1349311/… – unutbu Aug 17 '10 at 18:50 @Alex, yep, but I agree with @rbp -- there are clearly deeper problems here, tackling them first seems preferable. – Alex Martelli Aug 17 '10 at 23:37 add a comment |"
140,"
Q: Python, text mining, docx to table(CSV) ",0 You'll need the docx and CSV or openpyxl modules. You'll also need effort. Figure out a way to differentiate between the things you want to store in the CSV then throw this detection and storage into a loop that senses and stops when there is nothing more to do. That's the most advice you'll get with this type of question. share|| follow |||| answered Mar 22 '17 at 14:48 Chris CoxChris Cox 1344 bronze badges add a comment |
141,"
Q: Python text file into .csv ","0 If you're trying to open your .csv in excel, it's probably not going to be very readable. csv stands for comma separated values, and your output has no commas, but a lot of whitespace and newlines- excel can't really be expected to handle those. Here's what the output of your program is (plus newlines): hostname cisco1841 interface Loopback0 ip address 10.10.0.1 255.255.255.255 You can format your output more correctly by adding commas to your output file in place of the spaces that are already there. For the ip address line you want to grab, it's harder since that has a space in its name as well. I solved this by just grabbing the first ipAddress listed in that line- if you want the other one or both it's an easy fix. I also removed the buffer you were using and ""keepCurrentResultSet"" because there's really no need for them that I can see. inFile = open(""text.txt"") outFile = open(""result.csv"", ""w"") for line in inFile: if line.startswith (""hostname""): outFile.write(line.replace(' ',',')) elif line.startswith(""interface Loopback""): outFile.write(line.replace(' ',',')) ipAddrLine = next(inFile) ipAddress = ipAddrLine.split(' ')[2:3] outFile.write('ip address,' + ','.join(ipAddress)) inFile.close() outFile.close() That gives this output, which should be considerd valid .csv format by excel: hostname,cisco1841 interface,Loopback0 ip address,10.10.0.1 share|| follow |||| answered Jan 21 '14 at 1:23 Chris ArenaChris Arena 1,3561313 silver badges1616 bronze badges add a comment |"
142,"
Q: Scikit-Learn/Python text classfication ","0 You need to store the returned classifier to some object and then call predict() on it. Something like this: trained_clf = train(trial1, data.Details.values.astype('U'), target_one) predictions = trained_clf.predict(unstructured_data.Details.values.astype('U')) Update:- If you are talking about the accuracy inside the train() function, then you can print the predicted categories with the following: def train(classifier, X, y): ... ... print (""Accuracy: %s"" % classifier.score(X_test, y_test)) print (""Predictions: %s"" % classifier.predict(X_test) return classifier share|| follow |||| edited Jan 24 '18 at 10:18 answered Jan 24 '18 at 9:51 Vivek KumarVivek Kumar 23k66 gold badges4747 silver badges7777 bronze badges 1 I'm able to print the accuracy but not the category to which each text(Details column) belong to. – Vicpoo Jan 24 '18 at 10:03 1 Thanks a lot for your answer but the accuracy part is already working. I meant if i explicitly pass a row for cross validating to which category the text in the detail column belong to,how would i check it? – Vicpoo Jan 24 '18 at 10:17 1 @Vichu I added the line print (""Predictions: %s"" % classifier.predict(X_test) which will print the predictions of your test data? Do you have any other data that you want to predict, then follow my original code above. – Vivek Kumar Jan 24 '18 at 10:19 add a comment |"
143,"
Q: Python Text Game : Program improvement ","0 Resolved in comments: Just put a dummy '' value at the beginning of items. – Paul McGuire But the user will view the dummy value, as am printing the options to him. – Abdul Salam Just start at 1 then: for x in items[1:]: print(x) – Paul McGuire Thanks Paul ! It works Fine :) share|| follow |||| answered Jun 7 '17 at 12:50 community wiki stovfl add a comment |"
144,"
Q: Best tool for text extraction from PDF in Python 3.4 ","47 You need to install PyPDF2 module to be able to work with PDFs in Python 3.4. PyPDF2 cannot extract images, charts or other media but it can extract text and return it as a Python string. To install it run pip install PyPDF2 from the command line. This module name is case-sensitive so make sure to type 'y' in lowercase and all other characters as uppercase. >>> import PyPDF2 >>> pdfFileObj = open('my_file.pdf','rb') #'rb' for read binary mode >>> pdfReader = PyPDF2.PdfFileReader(pdfFileObj) >>> pdfReader.numPages 56 >>> pageObj = pdfReader.getPage(9) #'9' is the page number >>> pageObj.extractText() last statement returns all the text that is available in page-9 of 'my_file.pdf' document. share|| follow |||| edited Sep 7 '16 at 22:08 YoYo 7,02788 gold badges4343 silver badges6363 bronze badges answered Sep 19 '15 at 14:24 JohnnyBravo-xyzJohnnyBravo-xyz 61766 silver badges77 bronze badges Hi Ritesh, By any chance you you know the anser to this question. Question . – Bonson Sep 25 '15 at 3:07 4 Minor correction - think there shoudl be quotations for ""rb"" in the open command on line two rather than just rb. – kyrenia Aug 1 '16 at 19:31 3 Furthermore, the pages in pypdf2 are zero-indexed, i.e. getPage(9) will get you page #10. Page numbers in the original document are completely ignored by pypdf2. – nostradamus Oct 28 '16 at 7:42 One problem with pypdf2 is that in some cases it ignores new line characters which is not really a good thing! – Pedram Jul 13 '17 at 0:05 1 CAUTION: a) Not supported in Py3 and b) It ignores entire word if it has an un-parsable unicode ( e.g "" ) github.com/mstamy2/PyPDF2/issues/37 and it is unpredictable as commented by others above. It is a good tool, but not for production code sadly :( – user2390183 Dec 11 '18 at 10:27 | show 3 more comments"
145,"
Q: Python text help large files ","1 You need to read the files without saving the content in memory. You can do it by using with on the input files with open(r'C:\Users\Guy.SA\Desktop\fileB.txt', 'r') as file_2, open(r'C:\Users\Guy.SA\Desktop\fileC.txt', 'w') as new_file: for line_2 in file_2: with open(r'C:\Users\Guy.SA\Desktop\fileA.txt', 'r') as file_1: for line_1 in file_1: if line_1 == line_2: break else: new_file.write(line_2) share|| follow |||| answered Nov 3 '19 at 12:22 GuyGuy 32.3k99 gold badges2828 silver badges6161 bronze badges add a comment |"
146,"
Q: gTTS /python/ - can you text message the .mp3 file in your script? ","0 Twilio developer evangelist here. Twilio does support mp3 files for audio, as well as wav, aiff, gsm and ulaw. It looks as though that's exactly what gTTS does for you. The example in the documentation is: >>> from gtts import gTTS >>> tts = gTTS('hello') >>> tts.save('hello.mp3') Alternatively, you could just use Twilio's text to speech as part of your call, with <Say>. If you are not satisfied with the basic voices you can now use voices from AWS Polly in Twilio Voice. Let me know if that helps at all. share|| follow |||| edited Oct 11 '18 at 5:10 answered Oct 8 '18 at 4:28 philnashphilnash 47.9k99 gold badges4242 silver badges6060 bronze badges @ phinash - thank you your for your input, essentially I want to save a variable that contains text as an mp3, and then text said mp3 using twillio. I have updated your comment to include my code attempt – 0004 Oct 11 '18 at 4:07 It's better to update your question rather than my answer, so that we don't have a conversation through edits. – philnash Oct 11 '18 at 5:06 As for the code, you're trying to send the mp3 file as the body of a text message, which is not going to work. Instead you should look into making calls and responding with TwiML, using <Play> to play mp3 files. – philnash Oct 11 '18 at 5:08 I appreciate your answer. So there is no way to just text message an mp3 file using python? – 0004 Oct 11 '18 at 5:09 You can send media messages as long as you delivering to a number in the US or Canada. You'd need to include the file as the mediaUrl option in the request. Read these docs for more information. – philnash Oct 11 '18 at 5:12 add a comment |"
147,"
Q: Python Text-Based Baseball Game - Flipping Booleans ","0 from collections import deque bases = [False, False, True] playing_field = deque(bases) print(playing_field) #deque([False, False, True]) playing_field.rotate(-1) print(playing_field) #deque([False, True, False]) #can use it like a list: print(playing_field[0]) #False deque from collections is perfect for what you want to do. You can basically shift the list and add a member to first base. rotate() takes an integer, positive or negative, and shifts the list that many times left or right. If I were you, make it include a home plate, so you can pop it off and score or what not. IF you need deque has popleft() and popright() methods to literally take the left or right most item off the list. share|| follow |||| answered Nov 17 '16 at 18:05 MooingRawrMooingRawr 4,03033 gold badges2020 silver badges2626 bronze badges Wow this is so helpful I'll try this out. Very appreciated – lilsnyderio Nov 17 '16 at 18:24 add a comment |"
148,"
Q: Python extract text files ","2 There are a couple of issues with your code: Your text.close() should be at the same level as the for line in text loop. The print >> desc statement is out of place: you should print only if both Text1 and Text2 are defined. You could set them as None just outside the for line in text loop, and test if they are both not None. (Alternatively, you could set hit_count0=1 in the if moldesc[0] test, hit_count1=1 in the if moldesc[1] and test for hit_count0 and hit_count1). In that case, print the output and use a break to escape the loop. (so, in plain code:) for f in in_files: file = os.path.join(path, f) with open(file, ""r"") as text: hit_count = 0 hit_count1 = 0 for line in text: if moldesc[0] in line: Text1 = line.split()[-1] hit_count = 1 if moldesc[1] in line: Text2 = line.split()[-1] hit_count1 = 1 if hit_count and hit_count1: print >> desc, f + "","" + Text1 + "","" + Text2 break There's a third issue: You mention wanting the text before Text1 ? Then you may want to use Text1 = line[:line.index(moldesc[0])] instead of your Text1 = line.split()[-1]... share|| follow |||| edited Sep 25 '12 at 12:17 answered Sep 25 '12 at 12:03 Pierre GMPierre GM 15.9k33 gold badges4444 silver badges6161 bronze badges add a comment |"
149,"
Q: Text analysis in python [closed] ","2 You can use Apache Spark, it comes with four supported languages (Java, Scala, Python and R), it's compatible with ipython and jupyter with some tricky modifications. There some courses you can audit: Introduction to Big Data Scalable Machine Learning Here is a small pdf that paves the subject. Text Analysis with Apache Spark And here I show a small example of word-count using Apache-Spark, but it is not limited to this subject (it has PCA, SVD, and a big etc.) documentRDD = sc.parallelize([""Hello"", ""world"", ""from"", ""the"", ""python"", ""world""]) tokensTupleRDD = documentRDD.map(lambda word: (word, 1)) tokensCountRDD = tokensTupleRDD.reduceByKey(lambda a, b: a + b) print(tokensCountRDD.collect()) # (""Hello"", 1), (""world"", 2),... Other alternative is to use Scikit-learn, which is very used, easy and covers this area too, the only bad thing is that algorithms can't run in clusters and don't scale nicely. They even have a very easy tutorial in their site: Scikit-learn Tutorial So if your trying to learn I would suggest scikit-learn, but if you are trying to apply Bid Data at your work I might suggest you to study both and use Apache Spark share|| follow |||| edited May 23 '17 at 11:52 Community♦ 111 silver badge answered Nov 28 '15 at 23:08 Alberto BonsantoAlberto Bonsanto 13.2k77 gold badges4444 silver badges8080 bronze badges add a comment |"
150,"
Q: Python text file data extraction ","1 As others have suggested, you could create a dictionary for each data chunk. When you hit blank lines write to your output file and clear the dictionary. Using a defaultdict makes concatenating the multiple entries per key easier. from collections import defaultdict keep = ['P2', 'P4', 'G6'] tmp_dict = defaultdict(str) # a function to handle formatting of output def output_format(the_dict): return '\t'.join([the_dict[k].rstrip() for k in keep]) + '\n' with open('test.txt') as infile, open('output.txt', 'w') as outfile: for line in infile: # if there's non-whitespace text on this line if line.strip(): k, v = line.split(' : ') # store the data if the key is relevant, # appending if the key has already been hit if k in keep: tmp_dict[k] += v.rstrip() + ' ' # when there's a blank line, write the data to # the output file and clear the temporary dict else: outfile.write(output_format(tmp_dict)) tmp_dict = defaultdict(str) # one last time, in case file doesn't end in newline outfile.write(output_format(tmp_dict)) Not sure I fully understand the desired output. This prints the value for P2, then for P4 then for G6 all on one line with a tab between each. For example: blabla whatever This entry continues down here If an element is missing, the tabs will still be printed, so if P4 is missing there will be two tabs in a row. But the output is flexible. If you wanted, say, to preserve the original format after filtering and merging G6, then you could use: def output_format(the_dict): output = '' for k, v in the_dict.iteritems(): output += '{} : {}\n'.format(k, the_dict[k]) return output + '\n' share|| follow |||| edited Apr 27 '15 at 18:24 answered Apr 27 '15 at 15:16 ohruunuruusohruunuruus 3,79755 gold badges1515 silver badges3434 bronze badges this is very clean! Like it a lot. Just want to mention that the OP indicated he desires an output record ONLY when there's a P1 record and at least one G6 record in a given block. I also acknowledge that I'm not 100% clear on exactly what the right conditions are as the problem description and the sample code don't quite match. See my answer below... – Jasper33 Apr 27 '15 at 17:13 wait really? I don't see that but maybe I'm spacing out here. Also I think I have the output wrong, tweaking it now... – ohruunuruus Apr 27 '15 at 17:17 I got tripped up on this as well - OP wrote at very bottom of post: ""The problem I encounter is that some records do not have all entries I need and some have the G6 spread over several lines. Any ideas on how to do this?"" – Jasper33 Apr 27 '15 at 17:19 I took this to mean that sometimes an element (e.g. P4) is missing, but that the code should deal with this gracefully. Not sure how to handle output for that. – ohruunuruus Apr 27 '15 at 17:23 Yep. I sort of inferred from his code snippet that s/he explicitly required the P1_ and G6_ boo leans to be true before outputting to the text file. Anyhow, I think your code is about as close as anyone will get. Up voting now! Well done! – Jasper33 Apr 27 '15 at 17:26 | show 2 more comments"
151,"
Q: Python text file instead of dictionary ","8 I think you are now trying to reinvent a key-value database. Maybe the easiest thing would be to check if sqlite3 module would offer you what you need. Using a readymade database is easier than rolling your own! Of course, sqlite3 is not a key-value DB (on the surface), so if you need something even simpler, have a look at LMDB and its Python bindings: http://lmdb.readthedocs.org/en/release/ It is as lightweight and fast as it gets. It is probably close to the fastest way to achieve what you want. It should be noted that there is no such thing as an optimal key-value database. There are several aspects to consider. At least: Do you read a lot or write a lot? What are the key and value sizes? Do you need transactions/crash-proof? Do you have duplicate keys (one key, several values)? Do you want to have sorted keys? Do you want to read the data out in the same order it is inserted? What is your database size (MB, GB, TB, PB)? Are you constrained on IO or CPU? For example, LMDB I suggested above is very good in read-intensive tasks, not so much in write-intensive tasks. It offers transactions, keeps keys in sorted order and is crash-proof (limited by the underlying file system). However, if you need to write your database often, LMDB may not be the best choice. On the other hand, SQLite is not the perfect choice to this task - theoretically speking. In practice, it is built in into the standard Python distribution and is thus easy to use. It may provide adequate performance, and it may thus be the best choice. There are numerous high-quality databases out there. By not mentioning them I do not try to give the impression that the DBs mentioned in this answer are the only good alternatives. Most database managers have a very good reason for their existence. While there are some that are a bit outdated, most have their own sweet spots in the application area. The field is constantly changing. There are both completely new databases available and old database systems are updated. This should be kept in mind when reading old benchmarks. Also, the type of HW used has its impact; a computer with a SSD disk, a cloud computing instance, and a traditional computer with a HDD behave quite differently performance-wise. share|| follow |||| edited Jul 22 '14 at 11:30 answered Jul 22 '14 at 8:19 DrVDrV 16.7k22 gold badges4242 silver badges5858 bronze badges Thank you for your prompt response! I'll try both. – g0m3z Jul 22 '14 at 8:56 Based on DrV's idea I googled 'key-value database for python' and it turned out that there's another well-developed thread here on SO in this topic: link – g0m3z Jul 22 '14 at 9:09 @g0m3z: There is a lot of good discussion in that thread. However, all modern systems did not exist by then. Also, I think you should not start evaluating distributed or otherwise very complex systems as your task is simple. Start with the single-file-based ones. Personally, I'd try SQLite and, use it if it is not too slow, and forget about theoretical considerations. – DrV Jul 22 '14 at 11:31 In the meantime I voted on SQLite for two reasons. First, I knokw SQLite a bit and second as you mentioned my task is not that complex so far. If it changes over time I'll reconsider to use something else. Thanks again for your time and your valuable comments! – g0m3z Jul 22 '14 at 11:46 add a comment |"
152,"
Q: Python Text file to List not dividing up ","0 Python String strip() Method example usage #!/usr/bin/python str = ""0000000this is string example....wow!!!0000000""; print str.strip( '0' ) output: this is string example....wow!!! The example is from https://www.tutorialspoint.com/python/string_strip.htm share|| follow |||| answered Feb 3 '17 at 2:46 doubteventhisdoubteventhis 2355 bronze badges add a comment |"
153,"
Q: Python 3.4 on Sublime Text 3 ","39 You need to provide the full path to python3, since Sublime Text does not read your ~/.bash_profile file. Open up Terminal, type which python3, and use that full path: { ""cmd"": [""path/to/python3"", ""$file""], ""selector"": ""source.python"", ""file_regex"": ""file \""(...*?)\"", line ([0-9]+)"" } share|| follow |||| edited Oct 9 '16 at 16:40 duan 5,87833 gold badges3535 silver badges5454 bronze badges answered Apr 24 '14 at 1:43 AndrewAndrew 26k1111 gold badges5454 silver badges8383 bronze badges 2 This Sublime Text does not read your ~/.bash_profile file explains a heap of things I have been struggling with. Should have realised it but I didn't. Awesome clarification. Thank you! – jwpfox Apr 25 '14 at 0:01 When you paste the path_to_python in the above code and save it as a Build System File. 1.) Don't forget to give it an easy to remember name (i.e Python3) 2.) Go back to Build System and switch the Build to the one you just created. Everything should work correctly. – Trapp Sep 5 '17 at 0:25 add a comment |"
154,"
Q: Python text Categorization using TFIDF ","0 You can use the libsvm with python. 1.The first thing to do is to represent your document by respecting the format of the file provided by libsvm. 2.Then, your read the data : from libsvm import * prob = svm_problem(your data…) 3.You set the svm parameters param = svm_parameter(kernel_type …) 4.training the model m = svm_model(prob, param) 5.then you test the model for the new documents m.predict(your new data...) share|| follow |||| answered Jun 9 '16 at 8:43 Riadh BelkebirRiadh Belkebir 73911 gold badge1111 silver badges3131 bronze badges add a comment |"
155,"
Q: Extracting text from a PDF file using PDFMiner in python? ","172 Here is a working example of extracting text from a PDF file using the current version of PDFMiner(September 2016) from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter from pdfminer.converter import TextConverter from pdfminer.layout import LAParams from pdfminer.pdfpage import PDFPage from io import StringIO def convert_pdf_to_txt(path): rsrcmgr = PDFResourceManager() retstr = StringIO() codec = 'utf-8' laparams = LAParams() device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams) fp = open(path, 'rb') interpreter = PDFPageInterpreter(rsrcmgr, device) password = """" maxpages = 0 caching = True pagenos=set() for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True): interpreter.process_page(page) text = retstr.getvalue() fp.close() device.close() retstr.close() return text PDFMiner's structure changed recently, so this should work for extracting text from the PDF files. Edit : Still working as of the June 7th of 2018. Verified in Python Version 3.x Edit: The solution works with Python 3.7 at October 3, 2019. I used the Python library pdfminer.six, released on November 2018. share|| follow |||| edited Oct 4 '19 at 4:10 Trenton McKinney 5,6491313 gold badges2020 silver badges3535 bronze badges answered Oct 21 '14 at 19:47 DuckPuncherDuckPuncher 3,68533 gold badges1717 silver badges3333 bronze badges 2 works fine, but, how can I deal with spaces in for example names? suppose I have a pdf that contains 4 columns where I have first- and lastname in one col, now it get parsed with firstname in one row and lastname in one row, here's an example docdro.id/rRyef3x – Deusdeorum Mar 19 '16 at 11:45 2 Currently getting an import error with this code: ImportError: No module named 'pdfminer.pdfpage' – Jeffrey Swan Oct 22 '16 at 14:43 1 Thanks it works on python v2.7.12 and on ubuntu 16.04, though it would be better to load the pdf document with encoding utf-8, because my sample pdf has some encoding issue so try this after encoding with utf-8 and it resolve the issue... import sys reload(sys) sys.setdefaultencoding('utf-8') – sib10 May 29 '17 at 11:15 2 @DuckPuncher, Is it still working now? I had to change the file(path, 'rb') to `open(path, 'rb') to get mine to work. – craned Oct 17 '17 at 15:04 2 Still working for Python3.7 users. Installed pdfminer.six==20181108 package. Best solution so far for my case and I compared numerous solutions. – aze45sq6d Nov 5 '19 at 9:02 | show 7 more comments"
156,"
Q: python text to speech using pyttsx ",28 It's because you don't have espeak installed on your system. That's why it is giving error libespeak.so.1 error: no such file or directory found. Try to install espeak using sudo apt-get update && sudo apt-get install espeak share|| follow |||| answered Mar 25 '16 at 20:10 ashutosh benniashutosh benni 38133 silver badges55 bronze badges add a comment |
157,"
Q: Terminating a python text adventure [duplicate] ","0 If you are seeing that exception, it means that something is catching and printing it. My guess is that you have a try/except block like this: try: do_something() except: print_exc() Here the except: clause does not have an exception type specified, which means that all kind of exceptions (including SystemExit) will be caught. Bare except: clauses are meant for cleanup code and generally re-raise the exception after catching it. Avoid them unless you absolutely need them. Use this instead: try: do_something() except Exception: print_exc() This way, you'll catch only Exception subclass, i.e. all standard exceptions except SystemExit, KeyboardInterrupt and GeneratorExit. share|| follow |||| edited Dec 25 '15 at 18:01 answered Dec 25 '15 at 17:56 Andrea CorbelliniAndrea Corbellini 14k11 gold badge3838 silver badges5454 bronze badges add a comment |"
158,"
Q: How to get string objects instead of Unicode from JSON? ","100 +50 A solution with object_hook import json def json_load_byteified(file_handle): return _byteify( json.load(file_handle, object_hook=_byteify), ignore_dicts=True ) def json_loads_byteified(json_text): return _byteify( json.loads(json_text, object_hook=_byteify), ignore_dicts=True ) def _byteify(data, ignore_dicts = False): # if this is a unicode string, return its string representation if isinstance(data, unicode): return data.encode('utf-8') # if this is a list of values, return list of byteified values if isinstance(data, list): return [ _byteify(item, ignore_dicts=True) for item in data ] # if this is a dictionary, return dictionary of byteified keys and values # but only if we haven't already byteified it if isinstance(data, dict) and not ignore_dicts: return { _byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True) for key, value in data.iteritems() } # if it's anything else, return it in its original form return data Example usage: >>> json_loads_byteified('{""Hello"": ""World""}') {'Hello': 'World'} >>> json_loads_byteified('""I am a top-level string""') 'I am a top-level string' >>> json_loads_byteified('7') 7 >>> json_loads_byteified('[""I am inside a list""]') ['I am inside a list'] >>> json_loads_byteified('[[[[[[[[""I am inside a big nest of lists""]]]]]]]]') [[[[[[[['I am inside a big nest of lists']]]]]]]] >>> json_loads_byteified('{""foo"": ""bar"", ""things"": [7, {""qux"": ""baz"", ""moo"": {""cow"": [""milk""]}}]}') {'things': [7, {'qux': 'baz', 'moo': {'cow': ['milk']}}], 'foo': 'bar'} >>> json_load_byteified(open('somefile.json')) {'more json': 'from a file'} How does this work and why would I use it? Mark Amery's function is shorter and clearer than these ones, so what's the point of them? Why would you want to use them? Purely for performance. Mark's answer decodes the JSON text fully first with unicode strings, then recurses through the entire decoded value to convert all strings to byte strings. This has a couple of undesirable effects: A copy of the entire decoded structure gets created in memory If your JSON object is really deeply nested (500 levels or more) then you'll hit Python's maximum recursion depth This answer mitigates both of those performance issues by using the object_hook parameter of json.load and json.loads. From the docs: object_hook is an optional function that will be called with the result of any object literal decoded (a dict). The return value of object_hook will be used instead of the dict. This feature can be used to implement custom decoders Since dictionaries nested many levels deep in other dictionaries get passed to object_hook as they're decoded, we can byteify any strings or lists inside them at that point and avoid the need for deep recursion later. Mark's answer isn't suitable for use as an object_hook as it stands, because it recurses into nested dictionaries. We prevent that recursion in this answer with the ignore_dicts parameter to _byteify, which gets passed to it at all times except when object_hook passes it a new dict to byteify. The ignore_dicts flag tells _byteify to ignore dicts since they already been byteified. Finally, our implementations of json_load_byteified and json_loads_byteified call _byteify (with ignore_dicts=True) on the result returned from json.load or json.loads to handle the case where the JSON text being decoded doesn't have a dict at the top level. share|| follow |||| edited May 23 '17 at 11:47 Community♦ 111 silver badge answered Nov 6 '15 at 16:18 Mirec MiskufMirec Miskuf 1,51511 gold badge1111 silver badges1313 bronze badges 1 +1 for the approach here; I didn't really grasp it when I first read it, but finally understood when rereading it in light of Travis Jensen's answer. I've made a pretty aggressive edit in the hopes of clarifying how it works and what its advantages over my answer are. The core idea of the code remains untouched, but I've modified pretty much everything else. Feel free to roll back my edit if you object to this - it's your answer! – Mark Amery Jan 23 '16 at 11:28 No problem Mark, many thanks. I like your edit, it is much more explanatory than my original. Maybe, one day, I'll learn to give more concise answers. – Mirec Miskuf Jan 23 '16 at 21:08 2 This is great solution; efficient and elegant. However, if you're stuck in the realm of Python < 2.7, as I am, you will need to replace the line: return { byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True) for key, value in data.iteritems() } with return dict((_byteify(key, ignore_dicts=True), _byteify(value, ignore_dicts=True)) for key, value in data.iteritems()) for it to work. – Richard Dunn Apr 19 '16 at 10:27 I think you're wrong about the recursion depth issue. With yours, I can go up to 990: json_loads_byteified('[' * 990 + ']' * 990). With 991 it crashes. Mark's still works with 991: byteify(json.loads('[' * 991 + ']' * 991)). It crashes at 992. So at least in this test, Mark's can go deeper, contrary to what you said. – Stefan Pochmann May 3 '17 at 21:26 @MarkAmery What do you think about my above comment? (I just saw in the edit history that it was actually you who added that claim). – Stefan Pochmann May 3 '17 at 21:32 | show 3 more comments"
159,"
Q: Python Text Summarizer - maintain sentence order ","0 Got it working, leaving here in case others are curious: #iterate through each sentence and combine the weighted score of the underlying word sentence_scores = {} cnt = 0 for sent in sentence_list: sentence_scores[sent] = [] score = 0 for word in nltk.word_tokenize(sent.lower()): if word in word_frequencies.keys(): if len(sent.split(' ')) < 30: if sent not in sentence_scores.keys(): score = word_frequencies[word] else: score += word_frequencies[word] sentence_scores[sent].append(score) sentence_scores[sent].append(cnt) cnt = cnt + 1 #Sort the dictionary using the score in descending order and then index in ascending order #Getting the top 7 sentences #Putting them in 1 string variable from operator import itemgetter top7 = dict(sorted(sentence_scores.items(), key=itemgetter(1), reverse = True)[0:7]) #print(top7) def Sort(sub_li): return(sorted(sub_li, key = lambda sub_li: sub_li[1])) sentence_summary = Sort(top7.values()) summary = """" for value in sentence_summary: for key in top7: if top7[key] == value: summary = summary + key print(summary) share|| follow |||| answered Mar 8 '19 at 16:12 M WinchM Winch 5944 bronze badges add a comment |"
160,"
Q: Input in a Python text-based GUI (TUI) ","7 Also try useful built atop curses high level framework urwid. With that thing you could do rich and colorful interfaces. Buttons, edit fields, even status bars and progress bars and all that you need. To start working you only need Python curses installed and urwid folder with its sources (you can transfer whole urwid library with your application as standalone bundle!). It works even under cygwin under Windows XP/7 where is, as we know, no curses ports for Python. urwid portfolio No more lowlevel, sometimes very boring curses :) share|| follow |||| edited Apr 18 '16 at 11:01 ulidtko 11k77 gold badges4242 silver badges7979 bronze badges answered Aug 11 '13 at 22:49 rookrook 4,13933 gold badges3030 silver badges4343 bronze badges You can also easily modify (thank to Python) source widgets for project or educational needs. – rook Aug 12 '13 at 8:30 add a comment |"
161,"
Q: Python text game health bar ","0 All that needs to be done is a few simple conversion to convert your current health into a number of dashes and define the max number of dashes (20 in this case: healthDashes) to be equivalent to your max health (200: maxHealth). Consider you have 80 health left. So for example if we take healthDashes(20)/maxHealth(200) you get 10, this is the value that we divide our health by to convert it into the number of dashes that we want. You can then take your current health being 80 and the number of dashes is: 80/10 => 8 dashes. The percentage is straight forward: (health(80)/maxHealth(200))*100 = > 40 percent. Now in python you just apply that lodic above and you will get: health = 80.0 # Current Health (float so division doesn't make an int) maxHealth = 200 # Max Health healthDashes = 20 # Max Displayed dashes def do_health(): dashConvert = int(maxHealth/healthDashes) # Get the number to divide by to convert health to dashes (being 10) currentDashes = int(health/dashConvert) # Convert health to dash count: 80/10 => 8 dashes remainingHealth = healthDashes - currentDashes # Get the health remaining to fill as space => 12 spaces healthDisplay = '-' * currentDashes # Convert 8 to 8 dashes as a string: ""--------"" remainingDisplay = ' ' * remainingHealth # Convert 12 to 12 spaces as a string: "" "" percent = str(int((health/maxHealth)*100)) + ""%"" # Get the percent as a whole number: 40% print(""|"" + healthDisplay + remainingDisplay + ""|"") # Print out textbased healthbar print("" "" + percent) # Print the percent If you call the method you get the result: do_health() > |-------- | 40% Here are a few more examples with changing the value of health: |---------- | # health = 100 50% |--------------------| # health = 200 100% | | # health = 0 0% |------ | # health = 68 34% share|| follow |||| edited Aug 17 '19 at 2:51 answered Dec 30 '17 at 16:24 Spencer WieczorekSpencer Wieczorek 18.9k77 gold badges3838 silver badges4747 bronze badges Instead of using ''.join(['-' for i in range(currentDashes)]), it would be cleaner to do '-'*currentDashes. – Joseph Camacho Aug 17 '19 at 2:29 @JosephCamacho Good point, this was a few years back so it was probably a bit more rusty back then :P. I've also changed the health so it's a float due to how Python 2 handles integer division since I noticed OP tagged it in the question. Now it should have the correct answer in 2 and 3. – Spencer Wieczorek Aug 17 '19 at 2:53 add a comment |"
162,"
Q: Searching text in a PDF using Python? ","37 This is called PDF mining, and is very hard because: PDF is a document format designed to be printed, not to be parsed. Inside a PDF document, text is in no particular order (unless order is important for printing), most of the time the original text structure is lost (letters may not be grouped as words and words may not be grouped in sentences, and the order they are placed in the paper is often random). There are tons of software generating PDFs, many are defective. Tools like PDFminer use heuristics to group letters and words again based on their position in the page. I agree, the interface is pretty low level, but it makes more sense when you know what problem they are trying to solve (in the end, what matters is choosing how close from the neighbors a letter/word/line has to be in order to be considered part of a paragraph). An expensive alternative (in terms of time/computer power) is generating images for each page and feeding them to OCR, may be worth a try if you have a very good OCR. So my answer is no, there is no such thing as a simple, effective method for extracting text from PDF files - if your documents have a known structure, you can fine-tune the rules and get good results, but it is always a gambling. I would really like to be proven wrong. [update] The answer has not changed but recently I was involved with two projects: one of them is using computer vision in order to extract data from scanned hospital forms. The other extracts data from court records. What I learned is: Computer vision is at reach of mere mortals in 2018. If you have a good sample of already classified documents you can use OpenCV or SciKit-Image in order to extract features and train a machine learning classifier to determine what type a document is. If the PDF you are analyzing is ""searchable"", you can get very far extracting all the text using a software like pdftotext and a Bayesian filter (same kind of algorithm used to classify SPAM). So there is no reliable and effective method for extracting text from PDF files but you may not need one in order to solve the problem at hand (document type classification). share|| follow |||| edited Dec 1 '18 at 1:30 answered Jun 14 '13 at 0:52 Paulo ScardinePaulo Scardine 50.9k99 gold badges101101 silver badges128128 bronze badges All of the documents were scanned in as pdfs and OCR'ed to become searchable--is that different than what you're describing? – Insarov Jun 14 '13 at 16:12 1 @Insarov: Exactly what I'm talking about, any OCR worth its salary will have the option to output a pure text file along with the PDF file. – Paulo Scardine Jun 14 '13 at 22:23 If there is a sizable sample of document in relatively consistent narrative context (not necessarily in format), can we train a AI to understand it, so it can be used to read the text of the PDF files outsided the sample? – xappppp Jun 28 '19 at 19:22 @xappppp given enough time and resources, almost anything is possible. – Paulo Scardine Jun 28 '19 at 20:49 add a comment |"
163,"
Q: exp system for python text RPG ","0 Here is the signature you're currently using: def level_up(self): You complain that It doesn't seem to be getting the input from enemy.xp. You want to pass in enemy as a parameter: def level_up(self, enemy): As an entirely separate matter, it is fine to say self.xp = self.xp + enemy.xp, but it would be preferable to state it more concisely: self.xp += enemy.xp and similarly for self.xp -= self.lvlNext EDIT Perhaps you'd care to derive enemy in this way: room = world.tile_at(self.x, self.y) enemy = room.enemy It would be worth packaging that up as a helper function: def get_enemy(x, y): room = world.tile_at(x, y) return room.enemy share|| follow |||| edited Jan 6 at 1:26 answered Jan 6 at 1:19 J_HJ_H 7,50011 gold badge1212 silver badges2626 bronze badges Thank you for the response, I've edited the code as you've suggested including enemy in the def level_up(self, enemy). I'm looking for it to print the level up after it occurs. do you see where I am missing something. i will adjust the other code as you've suggested i just want to fix this issue first. – Gus Robins Jan 6 at 1:38 add a comment |"
164,"
Q: What are the advantages of Sublime Text over Notepad++ and vice-versa? [closed] ","88 votes One thing that should be considered is licensing. Notepad++ is free (as in speech and as in beer) for perpetual use, released under the GPL license, whereas Sublime Text 2 requires a license. To quote the Sublime Text 2 website: ..a license must be purchased for continued use. There is currently no enforced time limit for the evaluation. The same is now true of Sublime Text 3, and a paid upgrade will be needed for future versions. Upgrade Policy A license is valid for Sublime Text 3, and includes all point updates, as well as access to prior versions (e.g., Sublime Text 2). Future major versions, such as Sublime Text 4, will be a paid upgrade. This licensing requirement is still correct as of Dec 2019. share edited Dec 16 '19 at 18:25 answered Mar 7 '13 at 10:15 Tim RadcliffeTim Radcliffe 1,83311 gold badge1818 silver badges2828 bronze badges 3 This is very important point. Currently Sublime Text 3 doesn't have free version – Thang Nguyen Mar 28 '13 at 3:31 8 @TomWest Developers wouldn't allow unlimited evaluation periods unless they expect users who can't afford the software to carry on using it. You just wouldn't, would you? – Elliott Jan 17 '14 at 13:38 4 We offer the unlimited eval for several reasons, the top one being marketing: Sublime Text's popularity is purely due to word of mouth amongst developers. Incidentally this also works out well for starving students, start-ups and novices who can't or won't pay but do have a presence on sites like this one to spread the word about Sublime. - by SublimeHQ on reddit – Ashesh Kumar Singh Jul 14 '16 at 11:26 add a comment |"
165,"
Q: Python's .text : Couldn't get it to work ","2 import urllib url = ['http://google.com','http://bing.com'] for i in url: html = urllib.urlopen(i).read() print html No need for encode or text, just can print the html write after read(). I would suggest that you use python-requests. share|| follow |||| answered Oct 15 '13 at 5:02 Games BrainiacGames Brainiac 64.3k2727 gold badges120120 silver badges172172 bronze badges add a comment |"
166,"
Q: Python text mining ","1 You cannot have string as indices. You are iterating using a for each of string. i will contain the name. Not the index. You can remove this line. name = company[i] And replace next line with get_numbers(i) share|| follow |||| edited Jun 15 '15 at 9:29 answered Jun 15 '15 at 9:18 Tejus PrasadTejus Prasad 5,46155 gold badges3636 silver badges6868 bronze badges add a comment |"
167,"
Q: Python Text Loop ","1 I'll try my best to answer the pythonic way (even though I dont consider myself Python expert): Python is so great with lists and dictionnaries that you can avoid things like switch/case stuff. Here's your comparison the Python way: name = ""Olivier"" possible_choices = { ""The Kitchen"": lambda name: ""{} walked towards The Kitchen."".format(name), ""The Couch"": lambda name: ""{} went and had sat on The Couch."".format(name), ""Outside"": lambda name: ""{} put on their pack and went out the door."".format(name), } while True: choice = input(""Were would {} like to go?\n{}\n>"".format( name, '\n'.join(possible_choices))) if choice in possible_choices: print(possible_choices[choice](name)) break; # break the loop print(""error"") # loops With that: you have no ""switch/case"" because Python makes it possible, if you just add a key + lambda everything will work it's shorter thus: it's easier to read it's easier to maintain it's easier to understand thus it'll cost less to your company in the long run Learn how to use lambdas (= anonymous functions) which are used in every modern language, and this ""if choice in possible_choices"" is so clear that it makes it almost English! share|| follow |||| edited Jan 11 '18 at 8:37 answered Jan 11 '18 at 8:25 Olivier PonsOlivier Pons 13.1k2222 gold badges8686 silver badges171171 bronze badges This had really helped me out and thanks to it my stuff is working properly now. I thank you for helping me with my problem! – SenpaiPuppy Jan 11 '18 at 13:02 I don’t know how exactly and I need 15 rep for my rep to other people to be public. – SenpaiPuppy Jan 11 '18 at 14:08 There's a checkbox just under the up and down arrows of my answer, if you click on it this means ""ok this answer solved it"" ;) – Olivier Pons Jan 11 '18 at 14:30 add a comment |"
168,"
Q: Python Text File Line indentation ","1 Use \n instead of /n You need to have a number instead of just a string for the loop. i.e. studentNumber = int(input((""Enter amount of students: ""))) You need to append the new line char at the end of the print statement. Hence: studentInformation = [] studentNumber = int(input((""Enter amount of students: ""))) for i in range(0, studentNumber): studentInformation.append(studentNumber) studentID = input(""Enter student ID: "") studentEmail = input(""Enter student email: "") studentPhrase = studentID + ""#"" + studentEmail studentInformation[i] = studentPhrase print (studentPhrase) objectFile = open(""list.txt"", ""a"") objectFile.write(studentPhrase + '\n') objectFile.close OUTPUT: Enter amount of students: 3 Enter student ID: 123 Enter student email: [email protected] 123#[email protected] Enter student ID: 2 Enter student email: [email protected] 2#[email protected] Enter student ID: 1 Enter student email: [email protected] 1#[email protected] OUTPUT (from the file): 123#[email protected] 2#[email protected] 1#[email protected] share|| follow |||| edited Mar 18 '19 at 8:27 answered Mar 18 '19 at 8:10 DirtyBitDirtyBit 13.5k44 gold badges2222 silver badges4646 bronze badges add a comment |"
169,"
Q: Failing to add new lines to python text file [duplicate] ","1 You have to use \n character to create a new line. Example: f.write(""Line one...\n"") f.write(""Line two...\n"") Good luck! share|| follow |||| answered Feb 14 '18 at 2:40 Liam LuttonLiam Lutton 20411 silver badge88 bronze badges add a comment |"
170,"
Q: Python text parsing between two words ","2 since you're just parsing the text you just need the regex: import re result = re.findall(""text.*?bunch"", text_from_web_page) share|| follow |||| edited Nov 22 '12 at 3:49 answered Nov 22 '12 at 3:44 scriptsscripts 1,25411 gold badge1818 silver badges2323 bronze badges Thanks but I have no idea the type of tag it will be in. What I have done is parsed out all the text so now this is more of a text parsing question. I've updated my code to show this. – user1328021 Nov 22 '12 at 3:46 what is text_from_web_page variable supposed to be in my example? – user1328021 Nov 22 '12 at 3:56 the text you extracted in this case: ""This is the text of the webpage. It is just a string of a bunch of stuff and maybe some tags in between. "" – scripts Nov 22 '12 at 3:58 add a comment |"
171,"
Q: python text reading ","1 you operating with file handler, but should work on line like this d1 = record.strip(""\n"").split("","") datafile = open(""temp.txt"", ""r"") record = datafile.readline() while record != '': d1 = record.strip(""\n"").split("","") print d1[0],float (d1[1]) record = datafile.readline() datafile.close() share|| follow |||| answered Jun 20 '12 at 20:06 www.eugenehp.tkwww.eugenehp.tk 1,1851010 silver badges1616 bronze badges add a comment |"
172,"
Q: Python text file ","1 Python already has everything built-in you need for this: the csv module: import csv with open(""ABC.txt"", ""rb"") as csvfile: reader = csv.reader(csvfile, delimiter=""\t"") data = list(reader) minimum = data[2][17] Now minimum will contain 4444 if that's the 18th value in the 3rd row. share|| follow |||| edited Dec 16 '12 at 11:56 answered Dec 16 '12 at 11:51 Tim PietzckerTim Pietzcker 274k5353 gold badges421421 silver badges490490 bronze badges Hey tim the only issue I have now is that 4444 seems like a text, how do I get it so I can use the actual number so if the result is 4444 and I wanted to add 6 it would be 4450 – Greggy D Dec 16 '12 at 12:17 @GreggyD: minimum = int(data[2][17]) – Tim Pietzcker Dec 16 '12 at 12:18 For the column I actually need I get this error now: invalid literal for int() with base 10: '8633.3112' – Greggy D Dec 16 '12 at 12:23 @GreggyD use float() instead of int(). – Ashwini Chaudhary Dec 16 '12 at 12:37 Thanks Ashwini that got it, now will get on with the rest of it. Appreciate all those that provided comments – Greggy D Dec 16 '12 at 13:27 add a comment |"
173,"
Q: Ascending python text ","2 You can edit your code a little bit to store the scores in a list, then sort them using the sorted function. Then you can just take the first three scores of your sorted list. with open(r'doc.txt', 'r') as f: scores = [] for line in f: data = line.split() scores.append(data) top3 = sorted(scores, key = lambda x: int(x[1]), reverse=True)[:3] for score in top3: print '{0[0]:<15}{0[1]:<15}'.format(score) share|| follow |||| answered Oct 8 '15 at 15:54 C_Z_C_Z_ 5,61733 gold badges2626 silver badges5656 bronze badges add a comment |"
174,"
Q: Python : Text Analytics ","0 Not sure I understand completely what do you want, but maybe defaultdict is what you are looking for: from collections import defaultdict s = 'mississippi' d = defaultdict(int) for k in s: d[k] += 1 d.items() [('i', 4), ('p', 2), ('s', 4), ('m', 1)] When a letter is first encountered, it is missing from the mapping, so the default_factory function calls int() to supply a default count of zero. The increment operation then builds up the count for each letter. So you will have to do something similar. You will have a list of persons. Whenever you encounter that person in the text you add him/her to the defaultdict and increase the counter associated with him/her. So in the end you will have a dict with persons as keys and frequencies as values. share|| follow |||| answered Feb 13 at 9:41 kalzsokalzso 33411 gold badge44 silver badges1919 bronze badges add a comment |"
175,"
Q: Python text box ","0 you can use StringVar to get the string. the textvariable in your Label needs to be a Tkinter variable, quote: "" textvariable= Associates a Tkinter variable (usually a StringVar) to the contents of the entry field. (textVariable/Variable) you can read more here import tkinter as tk import tkinter.font as f r = tk.Tk() name = tk.StringVar() email = tk.StringVar() password = tk.StringVar() def Print(): print(""Name is"", name.get()) print(""Email is"", email.get()) print(""Password is"", password.get()) f = tk.Frame(r, height=600, width=900) f.pack() tk.Label(f, text=""Name"").place(x=30, y=50) tk.Label(f, text=""Email"").place(x=30, y=90) tk.Label(f, text=""Password"").place(x=30, y=130) sbmitbtn = tk.Button(f, text=""Submit"", activebackground=""pink"", activeforeground=""blue"", command=lambda: [Print(), f.destroy()]).place(x=30, y=170) e1 = tk.Entry(f, textvariable=name).place(x=80, y=50) e2 = tk.Entry(f, textvariable=email).place(x=80, y=90) e3 = tk.Entry(f, textvariable=password).place(x=95, y=130) r.mainloop() share|| follow |||| answered Nov 30 '19 at 16:27 ranifischranifisch 1,61311 gold badge1010 silver badges2020 bronze badges Why r.mainloop() is used in the last? – Satyajit Satapatathy Jan 8 at 11:55 add a comment |"
176,"
Q: Python Image to Text ","0 Something is wrong with how you typed the filename. Try this in your python code: import os print(os.listdir(""/home/Desktop/ITT/"")) You should see your filename printed. Copy the filename from there instead. If this fails, go up a directory (eg /home/Desktop) and try that. share|| follow |||| answered Dec 15 '18 at 17:37 jason mjason m 4,9161616 gold badges5353 silver badges107107 bronze badges @TchemedjeRhonnelYvan Yes. So the problem is the directory. Go to your command line. type cd ~ that should bring you to home. then cd to Desktop, etc.Once you are where you want, type pwd. – jason m Dec 15 '18 at 17:41 @TchemedjeRhonnelYvan try what i wrote above. – jason m Dec 15 '18 at 17:42 add a comment |"
177,"
Q: PYTHON - text store [closed] ","0 This will append to the file and create the file if it does not exist: import os #hard code the path to the external file external_file = 'names.txt' #if file exists, use it to load name, else ask user if not os.path.isfile(external_file): with open(external_file, ""a"") as f: pass #if file exists, use it to load name, else ask user name = raw_input(""What's your name?"") with open(external_file, ""r"") as f: lines = f.read().strip().split() # split the names to avoid issues like name ""a"" in name ""al"" being True if name in lines: print ""Hi {}"".format(name) else: with open(external_file, ""a"") as f: # using ""a"" will append to the file f.write(name) f.write(""\n"") I am not sure on your logic for checking if a user exists as you would need to know the name to check if the users name is already in the file. This reflects your comments: import os #hard code the path to the external file external_file = 'names.txt' #if file does not exist ask user for name and save it if not os.path.isfile(external_file): name = raw_input(""What's your name?"") print ""Hi {} I am storing your name"".format(name) with open(external_file, ""a"") as f: f.write(name) else: # file exists, use it to load name and print with open(external_file, ""r"") as f: name =f.read() print ""Hi {}"".format(name) If the file does not exist, the name cannot be in there so this will ask the user for their name or print ""Hi name"" if the file does exist share|| follow |||| edited Jun 11 '14 at 22:11 answered Jun 11 '14 at 21:54 Padraic CunninghamPadraic Cunningham 148k1717 gold badges164164 silver badges243243 bronze badges So this stores the name and uses it everytime you start the code? – user3729400 Jun 11 '14 at 22:02 So you just want to store one name? where are you getting the name from to check in the first instance from the file? – Padraic Cunningham Jun 11 '14 at 22:04 Yeah. Like it shall store the name and print it everytime the programme starts but if no name is in the .txt then it will ask it for a name. – user3729400 Jun 11 '14 at 22:06 ok I see now, I will edit, so only one file and one name yes? – Padraic Cunningham Jun 11 '14 at 22:06 That will come from 'What's your name' – user3729400 Jun 11 '14 at 22:06 | show 1 more comment"
178,"
Q: Python text processing ","2 You're iterating through file2 len(file1) times which is certainly not what you want to do. You want to construct a replacement dictionary from file1 like so: import re # regex to find usernames. # You can use str.split to find the usernames like you did if you're # not comfortable with regular expressions. user_regex = re.compile(r'^\${([a-zA-Z0-9_]+)}: ') # rename files to something better codes_file = ""/home/user/scripts/temp/txt1"" old_file = ""/home/user/scripts/temp/txt2"" new_file = ""/home/user/scripts/temp/txt3"" codes = {} with open(codes) as f: # use with to safely open files for line in f: match = user_regex.search(line) if match: codes[match.group(1)] = line # now we have the codes in ram for easy lookup with open(old_file) as old, open(new_file, 'w') as new: for line in old: match = user_regex.search(line) if match and match.group(1) in codes.keys(): new.write(codes[match.group(1)]) else: new.write(line) share|| follow |||| edited Jan 3 '19 at 11:16 answered Jan 3 '19 at 11:08 FHTMitchellFHTMitchell 9,35722 gold badges1616 silver badges3333 bronze badges add a comment |"
179,"
Q: inserting text with python [duplicate] ","0 Unless you perform this particular task on a regular basis, you don't really need a python script for it. Most editors created with programming in mind (Vim, Emacs, Notepad++, etc) supports some kind of regexp-based search and replace. Replacing ^\(.*\)$ with ""\1"" should wrap all lines in double-quotes in editors that use simple regular expressions. Depending on what tool you use, this might be done in easier ways too; e.g. in a line-by-line substitution with Sed or Vim, replacing .* with ""&"" does the same job. If you're unfamiliar with regular expressions, I highly recommend googling for a good tutorial. Check out the three-part introduction to Sed by Daniel Robbins if you're using Linux/Unix. You may also refer to this page for a more general introduction to regular expressions. share|| follow |||| answered Jul 15 '10 at 1:08 jabiralijabirali 1,13555 silver badges1212 bronze badges add a comment |"
180,"
Q: I'm making a python text-adventure. Need help making an 'inventory' ","2 Your approach seems to work fine, but you have a couple of bugs. In cell(), you need a : after the elif ""febreeze""... statement (before the comment starts). Also, at the end of that elif block, you need to return 'cell'. With these changes, I can pick up the Febreeze and use it. share|| follow |||| edited Nov 25 '11 at 23:32 answered Nov 25 '11 at 20:09 DavidDavid 1,36977 silver badges88 bronze badges add a comment |"
181,"
Q: Rendered HTML to plain text using Python ","92 BeautifulSoup is a scraping library, so it's probably not the best choice for doing HTML rendering. If it's not essential to use BeautifulSoup, you should take a look at html2text. For example: import html2text html = open(""foobar.html"").read() print html2text.html2text(html) This outputs: Some text more text even more text * list item * yet another list item Some other text * list item * yet another list item share|| follow |||| edited Feb 16 '19 at 9:04 JosefAssad 3,5202222 silver badges3434 bronze badges answered Nov 12 '12 at 3:09 deldel 4,98377 gold badges3636 silver badges4444 bronze badges 2 Can I use html2text in junction with BeautifulSoup. For example I parse the chunk of html I'm interested at and then feed it to html2text using pretify()? – btatarov Nov 12 '12 at 3:58 1 Yes, html2text can process HTML in chunks by calling HTML2Text.feed(chunk) on each successive chunk, and then calling HTML2Text.close() to get the text result (similar to HTMLParser.feed()). – del Nov 12 '12 at 4:40 30 This answer made me happy and sad at the same time. RIP Aaron Swartz. – Steve Rossiter Jan 16 '16 at 0:37 add a comment |"
182,"
Q: Text File Parsing with Python ","16 I would use a for loop to iterate over the lines in the text file: for line in my_text: outputfile.writelines(data_parser(line, reps)) If you want to read the file line-by-line instead of loading the whole thing at the start of the script you could do something like this: inputfile = open('test.dat') outputfile = open('test.csv', 'w') # sample text string, just for demonstration to let you know how the data looks like # my_text = '""2012-06-23 03:09:13.23"",4323584,-1.911224,-0.4657288,-0.1166382,-0.24823,0.256485,""NAN"",-0.3489428,-0.130449,-0.2440527,-0.2942413,0.04944348,0.4337797,-1.105218,-1.201882,-0.5962594,-0.586636' # dictionary definition 0-, 1- etc. are there to parse the date block delimited with dashes, and make sure the negative numbers are not effected reps = {'""NAN""':'NAN', '""':'', '0-':'0,','1-':'1,','2-':'2,','3-':'3,','4-':'4,','5-':'5,','6-':'6,','7-':'7,','8-':'8,','9-':'9,', ' ':',', ':':',' } for i in range(4): inputfile.next() # skip first four lines for line in inputfile: outputfile.writelines(data_parser(line, reps)) inputfile.close() outputfile.close() share|| follow |||| edited Apr 26 '17 at 11:36 Christopher Bottoms 9,34066 gold badges3939 silver badges8080 bronze badges answered Aug 13 '12 at 15:03 Joe DayJoe Day 4,89433 gold badges2020 silver badges2626 bronze badges thanks! what would be the best way to skip the first 4 lines then? To admit, I could not find a way to do it, that's why I decided to read the whole thing. Should I write the file except the first 4 lines to another file to run the loop you have above? I bet there should be an easier way though. EDIT: oh wait, I think you mean replacing the line txt = data_parser(my_text, reps) with the loop you have above. – marillion Aug 13 '12 at 15:06 You've already skipped the first 4 lines with the line my_text = inputfile.readlines()[4:], if you would rather read the file line-by-line and not load the whole thing in to ram at the beginning of the script I can update my answer. – Joe Day Aug 13 '12 at 15:12 Sorry, I got it wrong at the first place (see my EDIT above), but thanks, it works perfectly!!! Now, I would be very glad to learn about the ""read line-parse-write line (line-by-line)"" way of doing things. I have some files large file with a size of +500MB, which may mess up things. Could you update your answer with a second example? – marillion Aug 13 '12 at 15:20 I updated my answer with a version that reads the input file a line at a time. – Joe Day Aug 13 '12 at 15:29 Greatly appreciated, thank you! for i in range(4): inputfile.next() was what I was looking for before deciding to read the whole thing by the way! – marillion Aug 13 '12 at 15:41 add a comment |"
183,"
Q: Python text file processing speed issues ","9 In Python (at least <= 2.6.x), gzip format parsing is implemented in Python (over zlib). More, it appears to be doing some strange things, namely, decompress to the end of file to memory and then discard everything beyond the requested read size (then do it again for next read). DISCLAIMER: I've just looked at gzip.read() for 3 minutes, so I can be wrong here. Regardless of whether my understanding of gzip.read() is correct or not, gzip module appears to be not optimized for large data volumes. Try doing the same thing as in Perl, i.e. launching an external process (e.g. see module subprocess). EDIT Actually, I missed the OP's remark about plain file I/O being just as slow as compressed (thanks to ire_and_curses for pointing it out). This striken me as unlikely, so I did some measurements... from timeit import Timer def w(n): L = ""*""*80+""\n"" with open(""ttt"", ""w"") as f: for i in xrange(n) : f.write(L) def r(): with open(""ttt"", ""r"") as f: for n,line in enumerate(f) : if n % 1000000 == 0 : print n def g(): f = gzip.open(""ttt.gz"", ""r"") for n,line in enumerate(f) : if n % 1000000 == 0 : print n Now, running it... >>> Timer(""w(10000000)"", ""from __main__ import w"").timeit(1) 14.153118133544922 >>> Timer(""r()"", ""from __main__ import r"").timeit(1) 1.6482770442962646 # here i switched to a terminal and made ttt.gz from ttt >>> Timer(""g()"", ""from __main__ import g"").timeit(1) ...and after having a tea break and discovering that it's still running, I've killed it, sorry. Then I tried 100'000 lines instead of 10'000'000: >>> Timer(""w(100000)"", ""from __main__ import w"").timeit(1) 0.05810999870300293 >>> Timer(""r()"", ""from __main__ import r"").timeit(1) 0.09662318229675293 # here i switched to a terminal and made ttt.gz from ttt >>> Timer(""g()"", ""from __main__ import g"").timeit(1) 11.939290046691895 Module gzip's time is O(file_size**2), so with number of lines on the order of millions, gzip read time just cannot be the same as plain read time (as we see confirmed by an experiment). Anonymouslemming, please check again. share|| follow |||| edited Apr 22 '10 at 19:46 answered Apr 22 '10 at 15:35 atzzatzz 14.3k33 gold badges3232 silver badges3131 bronze badges 3 This is interesting, but isn't it beside the point? Whether this is true or not, the OP points out that looping over the uncompressed file (and therefore not using the gzip module at all) in Python is also slow. – ire_and_curses Apr 22 '10 at 16:53 @ire_and_curses -- thanks for pointing this out, I'll update my answer. – atzz Apr 22 '10 at 19:17 That's really bizzare about gzip.open. Is this an expected behaviour by design? – Santa Apr 22 '10 at 19:51 @Santa -- good question... I guess the module was not intended for processing large volumes of data (and on small files it's fast enough), but I don't know. – atzz Apr 22 '10 at 20:42 1 @Santa: If you have to ask, then it's never expected behaviour. – BlueRaja - Danny Pflughoeft Apr 22 '10 at 20:52 | show 1 more comment"
184,"
Q: python text adventure validate user input ","0 Try this : print(""input fire chandelier"") user = input() if ""fire"" in user or ""chandelier"" in user: print (""works"") else: print(""fails"") share|| follow |||| answered Mar 8 '15 at 15:00 user2440647user2440647 This is what I think OP meant to be doing with the original code but this will get horribly complicated as the list of commands grows. – Two-Bit Alchemist Mar 8 '15 at 15:01 thanks this is what I was looking for I would upvote but I dont have enough rep :) – Zac Connolly Mar 8 '15 at 15:09 add a comment |"
185,"
Q: Python visual text editor [closed] ","0 Take a look at Spyder (https://pythonhosted.org/spyder/). If you're coming from the Matlab world, then this can feel familiar to you. You have plotting capabilities like in Matlab, and you have the numpy library loaded by default, for matlab-like matrix computations. share|| follow |||| answered Mar 24 '16 at 6:44 Adi LevinAdi Levin 4,58911 gold badge1010 silver badges2323 bronze badges this is exactly what i was looking for. Thanks a lot. – sanster9292 Mar 24 '16 at 19:48 add a comment |"
186,"
Q: Python: Text file to dictionary whilst preserving order? ","1 This should do the trick, and is shorter than most of the other solutions. from collections import OrderedDict data = [] for line in eventVar.readline(): # Remove braces line = line.strip()[1:-1] results = OrderedDict() # Split by comma sign for pair in line.split(','): # Get key and value by splitting on colon key, value = pair.split(':') # Use eval to get rid of quotation surrounding the key and value results[eval(key)] = eval(value) # Add the results from reading this line to a list data.append(results) share|| follow |||| answered Jan 5 '14 at 4:37 Steinar LimaSteinar Lima 6,33122 gold badges3030 silver badges3737 bronze badges Doesn't this have problems with strings containing commas? – dstromberg Jan 5 '14 at 20:08 @dstromberg It has, but judging by the sample input, I don't think that is an issue in this case. – Steinar Lima Jan 6 '14 at 17:39 add a comment |"
187,"
Q: python - asn1 parsed text to json ","2 +25 Unfortunately, it is unlikely that somebody will come with a straight answer to your question (which is very similar to How to extract data from asn1 data file and load it into a dataframe?) The text of your link is obviously a log file where ASN.1 value notation was used to make the messages human readable. So trying to decode these messages from their textual form is unusual and you will probably not find tooling for that. In theory, the generic method would be this one: Gather the ASN.1 DEFINITIONS (schema) that were used to create the ASN.1 messages Compile these DEFINITIONS with an ASN.1 tool (aka compiler) to generate an object model in your favorite language (python). The tool would provide the specific code to encode and decode ... you would use ASN.1 values decoders. Add your custom code (either to the object model or plugged in the ASN.1 compiler) to encode your JSON objects As you see, it is a very long shot (I can expand if this explanation is too short or unclear) Unless your task is repetivite and/or the number of messages is big, try the methods you already know (manual search, regex) to search the log file. If you want to see what it takes to create ASN.1 tools, you can find a few (not that many as ASN.1 is not particularly young and popular). Check out https://github.com/etingof/pyasn1 (python) I created my own for fun in Java and I am adding the ASN.1 value decoders to illustrate my answer: https://github.com/yafred/asn1-tool (branch text-asn-value-support) share|| follow |||| answered Dec 29 '19 at 9:13 YaFredYaFred 7,23533 gold badges2121 silver badges3636 bronze badges add a comment |"
188,"
Q: Delayed text in python ","1 def delay_print(text, delay): for i in text: time.sleep(delay) print(i, end='') sys.stdout.flush() print() This is just loops through the given text and uses end='' to not print a new line until the loop finishes share|| follow |||| edited Mar 21 '17 at 22:41 answered Mar 21 '17 at 21:30 ChrisBrownie55ChrisBrownie55 2,46622 gold badges1111 silver badges2626 bronze badges Why the name sPrint for the function? Why not stick to the Style Guide for Python Code and call the function delayed_print or something like that? – Matthias Mar 21 '17 at 21:41 I actually didn't know there was a naming convention, I usually just name things like that. Thanks for showing me that though – ChrisBrownie55 Mar 21 '17 at 21:44 how do I call the function, I figured out with the other one it is poketext(""Text Here"") but I can't seem to figure it out with this one – ActiveGaming Mar 21 '17 at 22:21 Seriously?? You call it with your string and your delay, delay_print(""here is a string but you can use a variable too"", 0.05) – ChrisBrownie55 Mar 21 '17 at 22:22 0.05 is the time in seconds between each character print – ChrisBrownie55 Mar 21 '17 at 22:23 | show 2 more comments"
189,"
Q: python text file parse and print ","0 with open(""file.txt"",""r"") as f: lines=f.readlines() [print(lines[c-1]+lines[c:c+X]) for c,e in enumerate(lines) if ""ABC"" in e] Try this list comprehension share|| follow |||| answered Jul 23 '18 at 6:10 whackamadoodle3000whackamadoodle3000 5,33644 gold badges1616 silver badges3333 bronze badges add a comment |"
190,"
Q: Converting html to text with Python ","65 What am I missing? soup.get_text() gives exactly the same output you wanted... from bs4 import BeautifulSoup soup = BeautifulSoup(html) print(soup.get_text()) output Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa Consectetuer adipiscing elit. Some Link Aenean commodo ligula eget dolor. Aenean massa Aenean massa.Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa Consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa EDIT - And to keep newlines, as pointed out by @t-8ch: print(soup.get_text('\n')) PS! To be exact you can replace newline with a double one -- then it is identical to your example :) soup.get_text().replace('\n','\n\n') share|| follow |||| edited Oct 10 '19 at 9:14 caram 28411 silver badge1010 bronze badges answered Feb 4 '13 at 20:06 rootroot 50.6k1717 gold badges9090 silver badges110110 bronze badges 2 soup.get_text() is exactly what I needed. Thank you! – Aaron Bandelli Feb 4 '13 at 21:22 BeautifulSoup 4 only, unfortunately. – palswim Oct 15 '18 at 5:49 This saved my day! I update the response to for Python3 and incorporating @t-8ch's newline idea. – caram Oct 10 '19 at 7:44 add a comment |"
191,"
Q: Splitting text with python ","1 import re a=""""""SYSTEM DOF=UY,UZ,RX LENGTH=FT FORCE=Kip JOINT 1 X=0 Y=-132.644 Z=0 2 X=0 Y=-80 Z=0 3 X=0 Y=-40 Z=0 4 X=0 Y=0 Z=0 5 X=0 Y=40 Z=0 6 X=0 Y=80 Z=0 7 X=0 Y=132.644 Z=0"""""" # replace all character except digit, '-', '.' and ' '(space) with nothing, get all the info you need, than split each info into a list b = re.sub(r'[^\d\. -]','',a).split() # split the list to sublists, each contain four elements lines = [b[i:i+4] for i in range(0, len(b), 4)] for line in lines: print(line) out: ['1', '0', '-132.644', '0'] ['2', '0', '-80', '0'] ['3', '0', '-40', '0'] ['4', '0', '0', '0'] ['5', '0', '40', '0'] ['6', '0', '80', '0'] ['7', '0', '132.644', '0'] or write to file: print(' '.join(line),file=open('youfilename', 'a')) or: with open('filename', 'w') as f: for line in lines: f.write(' '.join(line) + '\n') # or f.writelines(' '.join(line)+'\n' for line in lines) out: 1 0 -132.644 0 2 0 -80 0 3 0 -40 0 4 0 0 0 5 0 40 0 6 0 80 0 7 0 132.644 0 share|| follow |||| edited Dec 13 '16 at 2:23 answered Dec 13 '16 at 1:44 宏杰李宏杰李 9,84822 gold badges1818 silver badges2828 bronze badges This solution is working but what if I wanted to output the results to a text file? – user7269405 Dec 13 '16 at 2:13 add a comment |"
192,"
Q: long text as String in python ","23 Use triple quotes : mytext = """"""Some text Some more text etc... """""" share|| follow |||| answered Jul 17 '12 at 14:40 Kassym DorselKassym Dorsel 4,25011 gold badge1919 silver badges4848 bronze badges 2 Note that while not commonly used, triple single quotes also work although triple-double-quotes are preferred (as long as your string doesn't contain triple-double-quotes). Phew, that's a mouthful. – mgilson Jul 17 '12 at 14:43 Just what I meant. Thank you! – user1031431 Jul 17 '12 at 14:43 add a comment |"
193,"
Q: Split text using Python [duplicate] ","1 Check this, might help you >>> re.compile('[0-9]+').split(""hel2l3o"") ['hel', 'l', 'o'] >>> >>> re.compile('cut').split(""hellocutworldcutpython"") ['hello', 'world', 'python'] split about first cut >>> l=re.compile('cut').split(""hellocutworldcutpython"") >>> print l[0], string.join([l[i] for i in range(1, len(l))], """") hello worldpython share|| follow |||| edited Sep 21 '11 at 13:13 answered Sep 21 '11 at 12:48 Aman AgarwalAman Agarwal 67866 silver badges1515 bronze badges need **TWO part ['hello', 'worldcutpython'] – Smirnov Sep 21 '11 at 12:58 can you please explain it clearly – Aman Agarwal Sep 21 '11 at 12:59 Need text only before txt and after. – Smirnov Sep 21 '11 at 13:04 >>> l=re.compile('cut').split(""hellocutworldcutpython"") >>> print l[0], string.join([l[i] for i in range(1, len(l))], """") hello worldpython – Aman Agarwal Sep 21 '11 at 13:12 add a comment |"
194,"
Q: Python Formatting Large Text ","22 The standard library has tools for this: import textwrap #... def getCalendarHeader(self): print textwrap.dedent(""""""\ BEGIN:VCALENDAR PRODID:-//Atlassian Software Systems//Confluence Calendar Plugin//EN VERSION:2.0 CALSCALE:GREGORIAN X-WR-CALNAME;VALUE=TEXT: X-WR-CALDESC;VALUE=TEXT: """""") share|| follow |||| answered Feb 15 '12 at 21:31 Ned BatchelderNed Batchelder 294k6464 gold badges483483 silver badges594594 bronze badges Works perfectly. Thanks! – petebowden Feb 15 '12 at 21:39 add a comment |"
195,"
Q: CSV to python, text align ","3 Using ljust() You can use ljust() function (for strings). ljust will pad the string from the right with spaces (or any other char if specified). Example usage: >>> 'hello'.ljust(10) 'hello ' >>> 'hello'.ljust(10, 'A') 'helloAAAAA' So when you print the values, just pad them using ljust Assume having a - list a of lists that contains your values: for b in a: for c in b: print c.ljust(15), print """" Using format You may use the string format function for that purpose: >>> '{:10}'.format('hello') 'hello ' The general format of a standard format specifier is: format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type] fill ::= <any character> align ::= ""<"" | "">"" | ""="" | ""^"" sign ::= ""+"" | ""-"" | "" "" width ::= integer precision ::= integer type ::= ""b"" | ""c"" | ""d"" | ""e"" | ""E"" | ""f"" | ""F"" | ""g"" | ""G"" | ""n"" | ""o"" | ""s"" | ""x"" | ""X"" | ""%"" Another way to use the string format: >>> '{text: <10}'.format(text='hi') 'hi ' Using slicing Well, it might be considered more elegant (?) or Pythonic (??) to use that method, but I don't personally like it: >>> ('hello' + 10 * ' ')[:10] 'hello ' Using print formatting You can use the percentage sign when printing, for formatting stuff with C style printf... Nevermind, it's not a good idea. Go look somewhere else! share|| follow |||| edited Nov 15 '16 at 20:27 answered Nov 15 '16 at 20:15 Yotam SalmonYotam Salmon 2,2191313 silver badges3131 bronze badges add a comment |"
196,"
Q: Python text files merge from subdirectory [closed] ","2 import os root = './' files = [(path,f) for path,_,file_list in os.walk(root) for f in file_list] out_file = open('master.txt','w') for path,f_name in files: in_file = open('%s/%s'%(path,f_name), 'r') # write out root/path/to/file (space) file_contents for line in in_file: out_file.write('%s/%s %s'%(path,f_name,line)) in_file.close() # enter new line after each file out_file.write('\n') out_file.close() if you only want some files from the tree rooted at root change the third line to # only take .txt files from the directory tree files = [(path,f) for path,_,file_list in os.walk(root) for f in file_list if f.endswith('.txt')] share|| follow |||| edited Aug 28 '12 at 11:29 answered Aug 28 '12 at 10:14 Matti LyraMatti Lyra 11.2k44 gold badges4040 silver badges6262 bronze badges Use os.path for extracting the extensions and joining paths... – Katriel Aug 28 '12 at 10:30 10x, @MattiLyra i am getting syntax error for the 'in', in = os.open('%s/%s' % (path, f_name), 'r'). BTW files are in C:\\Tasks\\ folder. – user1582596 Aug 28 '12 at 10:32 2 rather than using in and out use in_file and out_file – geo_pythoncl Aug 28 '12 at 10:46 2 in both cases don't use os.open() use just open() – geo_pythoncl Aug 28 '12 at 11:10 1 @user1582596 see updated code above – Matti Lyra Aug 28 '12 at 11:29 | show 9 more comments"
197,"
Q: How can I detect if a file is binary (non-text) in python? ","41 You can also use the mimetypes module: import mimetypes ... mime = mimetypes.guess_type(file) It's fairly easy to compile a list of binary mime types. For example Apache distributes with a mime.types file that you could parse into a set of lists, binary and text and then check to see if the mime is in your text or binary list. share|| follow |||| edited Nov 4 '17 at 14:18 A. Hennink 18322 silver badges1313 bronze badges answered May 22 '09 at 16:21 Gavin M. RoyGavin M. Roy 3,72844 gold badges3030 silver badges2727 bronze badges 15 Is there a way to get mimetypes to use the contents of a file rather than just its name? – intuited Oct 13 '10 at 7:01 4 @intuited No, but libmagic does that. Use it via python-magic. – Bengt Jun 30 '12 at 1:25 There is a similar question with some good answers here: stackoverflow.com/questions/1446549/… The answer based on an activestate recipe looks good to me, it allows a small proportion of non-printable characters (but no \0, for some reason). – Sam Watkins Mar 14 '13 at 2:57 5 This isn't a great answer only because the mimetypes module is not good for all files. I'm looking at a file now which system file reports as ""UTF-8 Unicode text, with very long lines"" but mimetypes.gest_type() will return (None, None). Also, Apache's mimetype list is a whitelist/subset. It is by no means a complete list of mimetypes. It cannot be used to classify all files as either text or non-text. – Purrell Feb 26 '15 at 22:21 1 guess_types is based on the file name extension, not the real content as the Unix command ""file"" would do. – Eric H. Jun 20 '17 at 7:00 | show 2 more comments"
198,"
Q: Search and replace line in Python text stream ","1 Strings are immutable objects in you Python, you can't change their values. If you want to change the value inside the StringIO buffer, you can do so as with any other file using seek and write, but replacing lines with different lengths will be a problem. I suggest modeling the text stream as a list of lines. Ex: text = ""line 1\nMATCHTHIS line 2\nline 3"" lines = text.split('\n') for i, line in enumerate(lines): if line.startswith(""MATCHTHIS""): meta, new_line = parse_data(line) lines[i] = new_line # Replace old line. new_text = '\n'.join(lines) # Recreate text with replaced lines. Also, classic console tools like sed and piping are perfect for this job. share|| follow |||| answered Aug 6 '15 at 1:53 BoppreHBoppreH 5,23911 gold badge2626 silver badges5555 bronze badges Thanks, this worked. ... sed isn't an option as this is part of a Python web app. ... I'm curious about strings being immutable. StringIO indicates that it can read/write buffers, though I wasn't able to get this working as I wanted (probably due to my inexperience). – user2487593 Aug 8 '15 at 22:36 add a comment |"
199,"
Q: R or Python, text analysis and my particular situation ","0 This question isn't the best format for StackOverflow - it is rather opinionated and quite vague. Personally I would tend towards R for most data analysis. Python is a fantastic general purpose language but for 90% of data analysis, I would choose R. If you are a complete beginner, I would recommend the R for Data Science book to get started with R, then I would recommend looking at the tidytext package for text and sentiment analysis. Appreciate some advice as i dont want to start learning, realise its not for me and start from point 0 again. Just like learning French helps your Italian, starting with Python, realising it is not for you and then learning R would not be starting from point 0 again. Learning Python would be a valuable exercise that would help you become a better programmer. A good data analyst should be able to express themselves in multiple languages. share|| follow |||| answered Aug 6 '17 at 11:28 JamesJames 39222 silver badges99 bronze badges Appreciate the answer. I do understand the question is not really stack overflow suitable but its been bugging me, I thought its best to get an answer from those who know the languages the best. My maths isnt fantastic, its not hopeless either, im just not great at summing things in my head quickly, i rely on a calculator alot. I dont know if this really has any relevance to anything. Also you mentioned learning Python and then R would not make me start at point 0, would the same argument hold from learning R and switching to Python? thanks – user8424260 Aug 6 '17 at 11:41 add a comment |"
200,"
Q: Python, PIL; Text to Image and fonts ","10 You aren't looking at actual font files in the control panel (explorer magically turns into the font viewer control panel when in the Windows/fonts folder as well), they are grouped by family for your convenience. Double click the family to see the fonts in the family: Then right-click and choose properties to find the file name: share|| follow |||| answered Apr 6 '13 at 23:39 Pavel AnossovPavel Anossov 49.6k1111 gold badges126126 silver badges113113 bronze badges 1 Thanks Pavel, that worked fine: font = ImageFont.truetype(""Arialbd.ttf"",14) – Harry Lime Apr 6 '13 at 23:42 add a comment |"
201,"
Q: Python text adventure trouble with storing data ","1 Is this the structure you're after? choices = { 1 : { 'prompt' : { 'prompt' : 'Level 1 prompt', 'A' : 'Choice A', 'B' : 'Choice B', 'C' : 'Choice C' }, }, 2 : { 'promptA' : { 'prompt' : 'Level 2 prompt A', 'A' : 'A Choice A', 'B' : 'A Choice B', 'C' : 'A Choice C' }, 'promptB' : { 'prompt' : 'Level 2 prompt B', 'A' : 'B Choice A', 'B' : 'B Choice B', 'C' : 'B Choice C' }, 'promptC' : { 'prompt' : 'Level 2 prompt C', 'A' : 'C Choice A', 'B' : 'C Choice B', 'C' : 'C Choice C' }, }, 3 : { 'promptA' : { 'prompt' : 'Level 3 prompt A', 'A' : 'A Choice A', 'B' : 'A Choice B', 'C' : 'A Choice C' }, 'promptB' : { 'prompt' : 'Level 3 prompt B', 'A' : 'B Choice A', 'B' : 'B Choice B', 'C' : 'B Choice C' }, 'promptC' : { 'prompt' : 'Level 3 prompt C', 'A' : 'C Choice A', 'B' : 'C Choice B', 'C' : 'C Choice C' }, } } def print_segment(level, prev_choice = ''): d = choices.get(level).get('prompt' + prev_choice) print(d.get('prompt')) for c in 'ABC': print(""[{}] {}"".format(c, d.get(c))) # Output >>> print_segment(1) Level 1 prompt [A] Choice A [B] Choice B [C] Choice C >>> print_segment(2, 'A') Level 2 prompt A [A] A Choice A [B] A Choice B [C] A Choice C >>> print_segment(3, 'B') Level 3 prompt B [A] B Choice A [B] B Choice B [C] B Choice C share|| follow |||| edited Nov 11 '16 at 17:36 answered Nov 11 '16 at 17:25 Steven SummersSteven Summers 3,91222 gold badges1212 silver badges2424 bronze badges Yes, that's exactly what I was looking for, thank you! – Cannon Sloan Nov 11 '16 at 17:35 add a comment |"
202,"
Q: Python Text Adventure Game Input Infinite Loop ","1 I think it has to do with your action.split(). This command will split on spaces and returns an array: see the documentation on split(). In essence this is what happens: Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 Type ""help"", ""copyright"", ""credits"" or ""license"" for more information. >>> action = ""go north"" >>> action = action.split() >>> print (action) ['go', 'north'] >>> ""go north"" == action False >>> As you can see the array is not equal to the ""go north"" comman,d and thus it is not in your list of valid commands. share|| follow |||| edited Jul 30 '19 at 12:20 aschultz 1,47833 gold badges1212 silver badges2424 bronze badges answered Aug 7 '18 at 7:26 Clueless_captainClueless_captain 32811 silver badge99 bronze badges add a comment |"
203,"
Q: python text processing: identify nouns from individual words ","1 Even if you use a dictionary, you will always have to deal with ambiguity, for example, the same word depending on the context can be a noun or a verb, take the word research The government will invest on research. The goal is to research new techniques of POS-tagging. Most dictionaries will have more than one definition of research, example: research as a noun research as a verb Where do these words come from, can you maybe pos-tag them within the context where they occur? share|| follow |||| answered Nov 6 '18 at 22:25 David BatistaDavid Batista 2,11111 gold badge1616 silver badges3131 bronze badges They have no context, it's a vocabulary list. You are of course right about the ambiguity, most verbs can be used as nouns, too. A good catch, a nice walk, ... I'm interested in throwing anything away that is obviously not a noun. ""beautiful"", ""learn"", ""above"", ... – lhk Nov 7 '18 at 9:49 add a comment |"
204,"
Q: Selective text using Python ","5 Try the following regex: ITEM1\.([\s\S]*?)ITEM1A\. Adding the question mark makes it non-greedy thus it will stop at the first occurrence share|| follow |||| answered Sep 29 '18 at 11:24 ARRARR 61633 silver badges1212 bronze badges 1 @Adrian do not forget to accept Ahmad's answer and also, next time, try to edit your answer to clarify instead of 'answering' the question :) – axm__ Sep 29 '18 at 11:51 2 @axm__ yes I will. Sorry for the confusion. This was my first post. Will keep in mind for the future! Thank you again! – Adrian Sep 29 '18 at 12:01 add a comment |"
205,"
Q: Python: Text File Reading Special Characters Issues ","0 HaHa yes, thank you. I also realized I had 2.7.13 python open and as soon as I closed it. The module started working both ways. Thank you again!! share|| follow |||| answered Feb 25 '17 at 7:24 LookingForAnswerLookingForAnswer 2122 bronze badges add a comment |"
206,"
Q: Python text file strings into columns in spreadsheet ","3 Alright, here's how you'd write to an actual Excel file. Note that my method of splitting isn't as complicated as others because this is mostly about writing to Excel. You'll need the python-excel package to do this. >>> data = [] >>> with open(""data.txt"") as f: ... for line in f: ... data.append([word for word in line.split("" "") if word]) ... >>> print data [['nothing doing', 'nothing[0]', 'doing[0]\n'], ['hello world', 'hello[0]', 'world[2]']] >>> >>> import xlwt >>> wb = xlwt.Workbook() >>> sheet = wb.add_sheet(""New Sheet"") >>> for row_index in range(len(data)): ... for col_index in range(len(data[row_index])): ... sheet.write(row_index, col_index, data[row_index][col_index]) >>> >>> wb.save(""newSheet.xls"") >>> This produces a workbook with one sheet called ""New Sheet"" that looks like this Hopefully this helps share|| follow |||| answered Jan 26 '14 at 18:33 wnnmawwnnmaw 4,55811 gold badge2525 silver badges5555 bronze badges you mention the print data, there are over 600 rows to print! – user3220585 Jan 27 '14 at 12:05 Then don't print it! :P I included that here so you can get a better idea of what I'm doing. Its not necessary for this to work – wnnmaw Jan 27 '14 at 13:10 removed the print because that was just stupid of me :P but again, this just produces all of the row in one excel cell :/ – user3220585 Jan 27 '14 at 16:32 @user3220585 Did you make any changes aside from removing print? – wnnmaw Jan 27 '14 at 17:05 no changes at all, other than renaming the text file to my own – user3220585 Jan 27 '14 at 19:33 | show 8 more comments"
207,"
Q: Having issue with subprocess library in python during text extraction from image using OCR ","0 Look like there is an error because it cannot found the file if the file is in the same directory I will recommend you use: import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) os.path.join(BASE_DIR, 'file_dir') share|| follow |||| answered Feb 25 at 6:50 Paritosh YadavParitosh Yadav 15777 bronze badges add a comment |"
208,"
Q: performance - python text file edit 2GB files ","1 I would suggest something like this: # if python 2.x #from itertools import tee, izip # if python 3 from itertols import tee # http://docs.python.org/2/library/itertools.html#recipes def pairwise(iterable): ""s -> (s0,s1), (s1,s2), (s2, s3), ..."" a, b = tee(iterable) next(b, None) # if python 2.x #return izip(a, b) return zip(a, b) new_data = [] with open('temp100.txt', 'r') as sqFile: for sLine, edit_line in pairwise(seqFile): # I think this is just new_line = tempLine #tempLine = edit_line[:20] #new_line = editLine.replace(editLine, tempLine) new_data.append(sLine + editLine[:20]) if len(sLine) > 20: new_data.append('\n') with open(""new_temp100.txt"", ""w"") as new: new.write(''.join(new_data)) you can probably do better if you just stream directly to disk # if python 2.x #from itertools import tee, izip # if python 3 from itertols import tee # http://docs.python.org/2/library/itertools.html#recipes def pairwise(iterable): ""s -> (s0,s1), (s1,s2), (s2, s3), ..."" a, b = tee(iterable) next(b, None) # if python 2.x #return izip(a, b) return zip(a, b) new_data = [] with open('temp100.txt', 'r') as sqFile: with open(""new_temp100.txt"", ""w"") as new: for sLine, edit_line in pairwise(seqFile): tmp_str = sLine + editLine[:20] if len(sLine) > 20: tmp_str = tmp_str + '/n' new.write(tmp_str) so you don't have to hold the full contents of the file into memory share|| follow |||| edited Oct 20 '13 at 4:26 answered Oct 19 '13 at 21:09 tacaswelltacaswell 65k1313 gold badges171171 silver badges170170 bronze badges why open a file for reading add stuff to a string and then write it all to a file? You could do both at the same time by nesting the two open calls -- then you just write each resultant line at a time. Think this would be quicker. – Tim Diggins Oct 19 '13 at 21:19 @TimDiggins because that is what the OP does (by resetting sys.stdout = new) – tacaswell Oct 19 '13 at 21:20 hmm, interesting. But I am getting the following error: from itertools import izip ImportError: cannot import name izip – Tom Anonymous Oct 19 '13 at 22:00 @TomAnonymous See edits, you are using python 3 where zip behaves as izip from python2. I use python2, and forget these things. – tacaswell Oct 19 '13 at 22:05 Ok, I applied the edit and it fixed the Error issue. The output text is coming off out of order, but I can work on that myself. Thanks again! – Tom Anonymous Oct 19 '13 at 22:13 | show 1 more comment"
209,"
Q: autoscroll of text and scrollbar in python text box ","24 You can cause the text widget to scroll to any location with the see method, which takes an index. For example, to make the last line of the widget visible you can use the index ""end"": outputwindow.see(""end"") Here's a complete working example: import time try: # python 2.x import Tkinter as tk except ImportError: # python 3.x import tkinter as tk class Example(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) self.text = tk.Text(self, height=6, width=40) self.vsb = tk.Scrollbar(self, orient=""vertical"", command=self.text.yview) self.text.configure(yscrollcommand=self.vsb.set) self.vsb.pack(side=""right"", fill=""y"") self.text.pack(side=""left"", fill=""both"", expand=True) self.add_timestamp() def add_timestamp(self): self.text.insert(""end"", time.ctime() + ""\n"") self.text.see(""end"") self.after(1000, self.add_timestamp) if __name__ == ""__main__"": root =tk.Tk() frame = Example(root) frame.pack(fill=""both"", expand=True) root.mainloop() share|| follow |||| edited Jun 5 '15 at 20:14 answered Jun 5 '15 at 16:16 Bryan OakleyBryan Oakley 264k2828 gold badges362362 silver badges520520 bronze badges Hello Bryan Oakley, can i please know why we have to use the function time.ctime() and also is it that every one second, scroll update happens?? – Ds Arjun Jun 8 '15 at 17:31 @DsArjun: you don't have to call time.ctime(). That was just a way to illustrate the data changing over time. You can insert any information you want. This simply calls add_timestamp every second. You can call any function or method you want. – Bryan Oakley Jun 8 '15 at 17:34 autoscroll is working in my code. Thanks a lot. But other problem what I have seen is, if you put cursor on top of text window and do scroll up using pointer wheel it wont go up. Only way is to drag up. Any way of scroll functionality with pointer wheel rotation? I also observed thet If the state is disabled then scroll works with pointer wheel rotation – Ds Arjun Jun 9 '15 at 17:19 add a comment |"
210,"
Q: Python Text Matching - Synonyms ","-1 I'm not sure how TF-IDF would be of use here if you are working with individual word pairs. Anyways, there are two obvious solutions to this. Use a traditional knowledge base, I would recommend Wordnet for this use case, it's widely considered a standard in the industry. The second option would be to use the machine learning algorithm Word2Vec (or a variant like Glove). I would say this is the easiest solution if you use a model with is already trained like the Google News one. Look into Gensim's implementation to load the model and compute similarities. share|| follow |||| answered May 18 '17 at 11:56 OerstedOersted 14533 silver badges77 bronze badges add a comment |"
211,"
Q: Python, text input in pyqt as a variable [duplicate] ","44 My first suggestion is to use Designer to create your GUIs. Typing them out yourself sucks, takes more time, and you will definitely make more mistakes than Designer. Here are some PyQt tutorials to help get you on the right track. The first one in the list is where you should start. A good guide for figuring out what methods are available for specific classes is the PyQt4 Class Reference. In this case you would look up QLineEdit and see the there is a text method. To answer your specific question: To make your GUI elements available to the rest of the object, preface them with self. import sys from PyQt4.QtCore import SIGNAL from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout class Form(QDialog): def __init__(self, parent=None): super(Form, self).__init__(parent) self.le = QLineEdit() self.le.setObjectName(""host"") self.le.setText(""Host"") self.pb = QPushButton() self.pb.setObjectName(""connect"") self.pb.setText(""Connect"") layout = QFormLayout() layout.addWidget(self.le) layout.addWidget(self.pb) self.setLayout(layout) self.connect(self.pb, SIGNAL(""clicked()""),self.button_click) self.setWindowTitle(""Learning"") def button_click(self): # shost is a QString object shost = self.le.text() print shost app = QApplication(sys.argv) form = Form() form.show() app.exec_() share|| follow |||| edited Mar 3 '15 at 18:19 alisianoi 1,22522 gold badges2020 silver badges3636 bronze badges answered Jun 10 '10 at 19:59 tgraytgray 7,64444 gold badges3232 silver badges3838 bronze badges I'm getting this error! AttributeError: 'Form' object has no attribute 'le' – esafwan Jun 10 '10 at 20:19 2 You need to name it self.le in your __init__ function. Everywhere you use it in your Form class it should be self.le. – tgray Jun 10 '10 at 20:30 I didn't get you. Sorry, I'm just getting started with these... i used the code which you gave, and the error came. I think in it, its already self.le – esafwan Jun 10 '10 at 20:48 I don't get the error when I run the above code. I'm using Python 2.6, what version are you using? – tgray Jun 10 '10 at 21:09 Python 2.6.5 under ubuntu 64 bit. – esafwan Jun 11 '10 at 14:15 | show 2 more comments"
212,"
Q: Text analyzation. Python beginner ","0 I usually don't play around with encoding/decoding files, but I do mess around with encodings for servers, from what I know I would do this. Instead of: with open (fn.encoding='utf_8') as f: I would do: with open (fn.decode('utf_7') as f: # your code here. share|| follow |||| answered Oct 20 '17 at 14:58 Cleve GreenCleve Green 52144 silver badges1212 bronze badges add a comment |"
213,"
Q: OpenCV MSER detect text areas - Python ","17 Below is the code # Import packages import cv2 import numpy as np #Create MSER object mser = cv2.MSER_create() #Your image path i-e receipt path img = cv2.imread('/home/rafiullah/PycharmProjects/python-ocr-master/receipts/73.jpg') #Convert to gray scale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) vis = img.copy() #detect regions in gray scale image regions, _ = mser.detectRegions(gray) hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] cv2.polylines(vis, hulls, 1, (0, 255, 0)) cv2.imshow('img', vis) cv2.waitKey(0) mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8) for contour in hulls: cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1) #this is used to find only text regions, remaining are ignored text_only = cv2.bitwise_and(img, img, mask=mask) cv2.imshow(""text only"", text_only) cv2.waitKey(0) share|| follow |||| edited Sep 12 '19 at 10:14 Martin Broadhurst 8,30022 gold badges2525 silver badges3232 bronze badges answered Aug 22 '17 at 7:28 RAFI AFRIDIRAFI AFRIDI 27133 silver badges33 bronze badges 1 This answer was very helpful to me. But I am bothered by the question why the reshape(-1, 1, 2) call is used. It returns a 3-dimensional array seemingly containing 2-dimensional data: [[[38, 30]], [[39, 30]], [[40, 30]], ...]. As far as I understand omitting that call leads to the same output of convexHull. Thus my question: What is it for? – ideaboxer Jul 25 '18 at 22:02 @ideaboxer did you get what the .reshape here does ? – Shreyas Moolya Aug 27 '19 at 10:29 add a comment |"
214,"
Q: Python Texttable ","0 You assigned the list of elements to tab.add_row(row), that's why you got the SyntaxError The correct way use Texttable is: # create a list with the elements and assign it to row row = [""Match1"", Team1_matches[1], Team2_matches[1], max(Team1_matches[1],Team2_matches[1])] # insert a row into table by invoking add_row() of the TextTable object tab. tab.add_row(row) share|| follow |||| answered Feb 21 '17 at 19:56 Haifeng ZhangHaifeng Zhang 19.2k1010 gold badges4646 silver badges9494 bronze badges add a comment |"
215,"
Q: Python text file processing ","4 y=x does not ""move content"" from one file to another. It just rebinds the name (variable) y so that afterwards, it refers to the same object as x. To copy the content from one file-like object to another, use shutil.copyfileobj: from shutil import copyfileobj with open('DCR.txt') as input: with open(""111.txt"", ""a+"") as output: copyfileobj(input, output) share|| follow |||| answered Mar 18 '12 at 11:07 Fred FooFred Foo 308k6161 gold badges645645 silver badges772772 bronze badges Cool, thanks. But it still doesn't create new files, saying absolutely nothing. I use PyScripter, usually it throws some errors when script doesn't work. – user547907 Mar 18 '12 at 11:15 yes, I've tried it multiple times. moreover, y=open(""111.txt"", ""a+"") should create file in working directory but it does not. – user547907 Mar 18 '12 at 11:51 You can specify a full path to make sure the file is created where you expect: open(""c:\myproject\111.txt"") – Mizipzor Mar 18 '12 at 12:04 1 @mizipzor: using backslashes in a plain string is a Bad Idea; either use forward slashes or a raw string. – Wooble Jun 12 '12 at 12:25 add a comment |"
216,"
Q: Python Text-Based Game with Tkinter ","0 You can't do a while loop like that in a GUI event handler. Until you return from the handler function, the GUI doesn't get to update its display, accept user input, or do anything else, which means player_input will never change once you get there. A handler function needs to do one thing, set up anything needed for future handlers, and return immediately. It's hard to get your head around thinking in terms of the event loop and handler callbacks the first time you write a GUI, but it's absolutely essential; until you do, things seem bizarre and arbitrary. So, the answer isn't to ""wait for input"", but to set up a handler that fires when that input is ready. (You can instead set up a handler on an after function so it fires every N milliseconds instead of firing on input, but that usually just makes things more complicated.) One way to do this is to have the ""Progress"" button trigger ""the next step"", and keep track of some state that lets you determine what ""the next step"" is. An in fact, you already have that state: it's whether the player has already given correct input or not. So: def pinput(): global player_input player_input = e.get() print(player_input) def progress(): pinput() if player_input != ""1"" and player_input != ""2"": hallway_1_enter() else: hallway_1_doit() def hallway_1_enter(): global player_input global prin global e prin.set(""You are in a hallway. Yellow lockers line the sides of all walls and you see three doors. One leads into a Library, one to a History classroom, and one to a Math room. You can 1.) Search the hallway or 2.) Procede to one of the rooms."") def hallway_1_doit(): if player_input == ""1"": #Even if 1 is in the entry box beforehand, it never gets here # etc. This is obviously a bit of a clunky design, but it's the smallest change to your existing design that gets you past the first step. Once you get the hang of it, you should be able to improve it from there. Also notice the global player_input I put in pinput. Your existing function didn't have that, so it was just creating a local variable with the same name, which then goes away immediately; the global never gets changed. Any function that wants to assign to a global needs the global statement. share|| follow |||| answered Apr 29 '18 at 19:48 abarnertabarnert 285k2828 gold badges432432 silver badges527527 bronze badges add a comment |"
217,"
Q: Python text replace with for loop ","2 Rather than a for loop, why not: #read entire file's content with open('hey.txt') as f: content = f.read() # find time and replace with empty string new_content = re.sub(r'[0-5]*[0-9]:[0-5]*[0-9]\n','', content) #write results into file with open('hey2.txt', 'w') as f: f.write(new_content) share|| follow |||| edited Aug 30 '18 at 22:18 answered Aug 30 '18 at 22:11 VanTanVanTan 43522 silver badges99 bronze badges add a comment |"
218,"
Q: Replace line in python text file ","0 I'll suggest open a new file for writing: def modifyQty(dbfilename,modelname,size,newcount): o = open(dbfilename, 'r') new_file = open('abc.txt', 'w') data = o.readlines() for line in data: values = line.split().split(',') # split by comma if values[0] == modelname and values[1] == size: line = line.replace(values[2], newcount) new_file.write(line + '\n') o.close() new_file.close() modifyQty('shoes.txt','Reebok 111','11','1') share|| follow |||| edited Apr 7 '13 at 9:45 answered Apr 7 '13 at 6:44 Aamir AdnanAamir Adnan 31.4k1515 gold badges9797 silver badges144144 bronze badges so after writing into that new file, i need to have all my other data in there too – user2253899 Apr 7 '13 at 6:53 Yes you will have all of your data in new file. There are efficient methods also for reading and writing but depends on the requirement e.g. how big is the file. – Aamir Adnan Apr 7 '13 at 6:55 add a comment |"
219,"
Q: Python - Add Date Stamp To Text File ","25 import datetime def timeStamped(fname, fmt='%Y-%m-%d-%H-%M-%S_{fname}'): return datetime.datetime.now().strftime(fmt).format(fname=fname) with open(timeStamped('myfile.txt'),'w') as outf: outf.write('data!') share|| follow |||| edited Mar 7 '11 at 1:58 answered Mar 7 '11 at 1:53 Hugh BothwellHugh Bothwell 47.6k66 gold badges6464 silver badges8888 bronze badges 2 +1 for using '-' as the time-element separator ... ':' is not a valid character in a Windows file name or directory name. – John Machin Mar 7 '11 at 2:08 Note that the String.format() method is not available for all versions of Python v2, and nor is the with open block, so if the example doesn't work then that might be the reason. – Fish Feb 13 '13 at 11:05 add a comment |"
220,"
Q: Selenium Python Text Extraction ","1 Why are you using address = driver.find_element_by_class_name('sidebar') - this is the reason why you are getting a lot of unwanted text in your code. The text that you want to get is rendered in a div which is a result of an repeater - since the page is an Angular page. <div ng-show="" ! searchInProgress"" ng-repeat=""result in results track by result.id"" ng-click=""goToLocation(result)"" class=""module shade mar-15-bot ng-scope"" style=""cursor: pointer;""> You should probably do something like this - not sure if the code is going to be accurate get_all_divs = self.driver.find_elements_by_css_selector('.module.shade.mar-15-bot.ng-scope') This will get you all divs inside the given repeater. Now the text that you want is inside the first div in a p tag. for i in get_all_divs: print i.find_element_by_css_selector('div > p').text You get inside the element with class and then inside that, you get the immediate div child and the p tag and the text inside it. A bit rusty with Python so you might have to make changes to the for loop that I have written. share|| follow |||| edited May 8 '18 at 18:17 answered May 8 '18 at 17:05 demouser123demouser123 2,81844 gold badges3434 silver badges5656 bronze badges Hi @demouser123 thank you for your response, and apologies I'm quite new with selenium. I am running into some errors on get_all_divs = self.driver.find_elements_by_class_name('.module.shade.mar-15-bot.ng-scope'). When I write get_all_divs = driver.find_elements_by_class_name('.module.shade.mar-15-bot.ng-scope') I'm receiving this: InvalidSelectorException: Message: Given css selector expression ""..module.shade.mar-15-bot.ng-scope"" is invalid: SyntaxError: '..module.shade.mar-15-bot.ng-scope' is not a valid selector – mm_nieder May 8 '18 at 18:08 See edited content. Instead of using class_name using css_selector to get the list of elements. – demouser123 May 8 '18 at 18:25 add a comment |"
221,"
Q: Python redmine: wiki page text ","1 You can't get a wiki page text this way because project.wiki_pages returns just a list of all wiki pages available for a project with a few other details. To get a text, author and some other info for a wiki page you have to make a separate call for each wiki page: from redmine import Redmine redmine = Redmine('http://redmine.example.com', username='user', password='1234') projects = redmine.project.all() for project in projects: print('Project: ' + project.name) for wiki_page in project.wiki_pages: wiki_page_details = redmine.wiki_page.get(wiki_page.title, project_id=project.id) print('Title: ' + wiki_page_details.title) print('Content: ' + wiki_page_details.text) print('\n===========================\n') This is not very efficient but Python Redmine is just an interface to the Redmine REST API, and Redmine doesn't provide a way to get all information about project wiki pages in one call, so making a separate call for each wiki page is the only way to go. share|| follow |||| answered Mar 7 '14 at 16:06 Max TepkeevMax Tepkeev 1,9261111 silver badges1212 bronze badges add a comment |"
222,"
Q: Python text adventure woes ","2 You don't have to split everything every time there is a choice, no. Consider this: print('This happens first') choice = raw_input('Make a choice (1 or 2)') if choice == 1: print('Conditional event no1') else: print('Conditional event no2') print('This happens after the choice, no matter what happened') The story can go on from there. You can visualize this as a flowchart resembling this: /------ Choice 1 -------\ Starting point / \____ Happens in either case \ / \------ Choice 2 -------/ share|| follow |||| edited Oct 11 '15 at 3:03 answered Oct 10 '15 at 22:44 HugoMailhotHugoMailhot 1,09799 silver badges1919 bronze badges This was pretty obvious I guess, but I just registered and wanted to test this out. Thanks! – mariahcareydid911 Oct 10 '15 at 22:47 Welcome to SO! Please accept this answer if this solved your problem :) – HugoMailhot Oct 10 '15 at 22:48 add a comment |"
223,"
Q: Python text RPG error [closed] ","1 It's not clear what's causing the error from the code you posted, but from the behavior you describe it sounds like you're not defining mobHP as a global. Here's some example code. mobHP = 20000 playerHP = 30000 def check_hps(): if mobHP <= 0: win() if playerHP <= 0: lose() def broken_attack(): attack_value = 100 # how hard you hit mobHP -= attack_value check_hps() def fixed_attack(): global mobHP attack_value = 100 mobHP -= attack_value check_hps() To demonstrate: >>> mobHP 20000 >>> broken_attack() >>> mobHP 20000 >>> fixed_attack() >>> mobHP 19900 >>> fixed_attack() >>> mobHP 19800 This code is, of course, really difficult to maintain. It is, however, pretty easy to implement. Contrast with: class Actor(object): def __init__(self, name, maxHP, min_dmg, max_dmg): self.name = name self.maxHP = maxHP self.currentHP = maxHP self.roll_dmg = lambda: random.randint(min_dmg, max_dmg) def attack(self, other, retaliation=False): other.currentHP -= self.roll_dmg() if not retaliation: other.attack(self, retaliation=True) def run(self, other): if random.random() >= 0.5: return True return False class Battle(object): def __init__(self, *actors, location=None): self.actors = actors self.location = location def do_round(self): for actor in self.actors: choice, target = self.make_choice(actor) self.do_choice(choice, target) yield self.check_battle_over() def check_battle_over(self): for actor in self.actors: if actor.currentHP <= 0: return actor return None def do_choice(self, func, target): return func(target) def make_choice(self, actor): print(""1. Attack\n"" ""2. Run away\n"") choice = input("">> "") if choice == '1': action = actor.attack elif choice == '2': action = actor.run actor_mapping = {num:act for num,act in enumerate( filter(lambda a: a is not actor), start=1} print(""Target:"") print(""\n"".join([""{}. {}"".format(*target) for target in actor_mapping.items()])) target = actor_mapping[input("">> "")] return (action, target) def start(self): while True: loser = do_round() if loser: print(loser.name + "" is dead!"") return player = Actor(name=""Player"", maxHP=30000, min_dmg=80, max_dmg=200) monster = Actor(name=""Monster"", maxHP=20000, min_dmg=50, max_dmg=100) battle = Battle(player, monster) battle.start() share|| follow |||| edited May 21 '15 at 3:40 answered May 21 '15 at 3:05 Adam SmithAdam Smith 40.1k77 gold badges4949 silver badges8282 bronze badges add a comment |"
224,"
Q: Python text search library [closed] ","0 regex module supports named lists: import regex def match_words(words, string): return regex.search(r""\b\L<words>\b"", string, words=words) def match(string, include_words, exclude_words): return (match_words(include_words, string) and not match_words(exclude_words, string)) Example: if match(""hello world how are you what are you doing"", include_words=[""world"", ""how are""], exclude_words=[""tigers"", ""bye bye""]): print('matches') You could implement named lists using standard re module e.g.: import re def match_words(words, string): re_words = '|'.join(map(re.escape, sorted(words, key=len, reverse=True))) return re.search(r""\b(?:{words})\b"".format(words=re_words), string) how do I build the list of included and excluded words based on the +, -, and """" grammar? You could use shlex.split(): import shlex include_words, exclude_words = [], [] for word in shlex.split('+world -tigers ""how are"" -""bye bye""'): (exclude_words if word.startswith('-') else include_words).append(word.lstrip('-+')) print(include_words, exclude_words) # -> (['world', 'how are'], ['tigers', 'bye bye']) share|| follow |||| edited Feb 27 '15 at 8:14 answered Feb 27 '15 at 0:20 jfsjfs 311k120120 gold badges768768 silver badges13801380 bronze badges Clever and probably faster than Amrita's solution, but I don't think this helps me with the keyword grammar either, unless I am missing some magic in the re_words creation. – ipartola Feb 27 '15 at 3:46 I've updated the answer to show how re_words are used in implementing the match() function and how to parse '+world -tigers ""how are"" -""bye bye""'. – jfs Feb 27 '15 at 8:15 Perfect. Didn't realize that shlex would do this with just the split function. This is perfect! – ipartola Feb 27 '15 at 15:48 add a comment |"
225,"
Q: How to search and replace text in a file? ","234 fileinput already supports inplace editing. It redirects stdout to the file in this case: #!/usr/bin/env python3 import fileinput with fileinput.FileInput(filename, inplace=True, backup='.bak') as file: for line in file: print(line.replace(text_to_search, replacement_text), end='') share|| follow |||| edited May 20 '19 at 21:36 Jacktose 51655 silver badges1616 bronze badges answered Dec 15 '13 at 10:47 jfsjfs 311k120120 gold badges768768 silver badges13801380 bronze badges 13 What is the end='' argument supposed to do? – egpbos Apr 1 '14 at 13:40 18 line already has a newline. end is a newline by default, end='' makes print() function do not print additional newline – jfs Apr 1 '14 at 13:46 11 Don't use fileinput! Consider writing the code to do this yourself instead. Redirecting sys.stdout isn't a great idea, especially if you're doing it without a try..finally like fileinput does. If an exception gets raised, your stdout might never get restored. – craigds Dec 18 '14 at 3:09 6 @craigds: wrong. fileinput is not a tool for all jobs (nothing is) but there are many cases where it is the right tool e.g., to implement a sed-like filter in Python. Don't use a screwdriver to pound nails. – jfs Dec 18 '14 at 13:16 5 If you really want to redirect stdout to your file for some reason, it's not hard to do it better than fileinput does (basically, use try..finally or a contextmanager to ensure you set stdout back to it's original value afterwards). The source code for fileinput is pretty eye-bleedingly awful, and it does some really unsafe things under the hood. If it were written today I very much doubt it would have made it into the stdlib. – craigds Dec 18 '14 at 22:06 | show 18 more comments"
226,"
Q: Python text clustering software or package ","0 Yes. There is software that can name the clusters cluster_1 cluster_2 cluster_3 cluster_4 Seriously what do you expect the cluster names to be? An automatic summary from the clustered articles? Text summarization isn't scaling up too well yet. But you could probably try to run the complete cluster through a text summarization engine. Note that k-means will produce quite artificial clusters. It doesn't know or care about topics. It just optimizes mathematical variance, which probably is not very useful for text data. share|| follow |||| answered Sep 4 '12 at 9:00 Has QUIT--Anony-MousseHas QUIT--Anony-Mousse 66.4k1212 gold badges109109 silver badges173173 bronze badges add a comment |"
227,"
Q: Large text Python 3 ","0 You have put ""A"" in quotes, this will print the letter A to the screen. If you want to reference the variable, you want to get rid of the quotes. writing = Text(app, text=A, color=""red"") share|| follow |||| answered Apr 14 '18 at 6:06 DarthVladerDarthVlader 12111 silver badge88 bronze badges add a comment |"
228,"
Q: Python - Text comparison of two columns ","5 I think you need apply with axis=1 for process by rows and then compare with in. Last convert Trues and Falses to 1,0 by astype in new column: df = pd.DataFrame({'text':['i hate cars','i hate cars'], 'text_dins':['cars', 'rabbits']}) print (df) text text_dins 0 i hate cars cars 1 i hate cars rabbits df['new'] = df.apply(lambda x: x['text_dins'] in x['text'] , axis=1).astype(int) print (df) text text_dins new 0 i hate cars cars 1 1 i hate cars rabbits 0 Another solution with list comprehension if no NaNs: df['new'] = [int(x[0] in x[1]) for x in zip(df['text_dins'], df['text'])] print (df) text text_dins new 0 i hate cars cars 1 1 i hate cars rabbits 0 share|| follow |||| edited Mar 26 '19 at 20:01 Gabriel Miretti aka gmiretti 85088 silver badges1111 bronze badges answered Jul 23 '17 at 14:25 jezraeljezrael 498k4040 gold badges595595 silver badges619619 bronze badges How do i find count of words under column ""text"". eg. 1st row has 3 words. – Kshitij Manvelikar Dec 27 '19 at 11:05 @KshitijManvelikar - Use df['text'].str.count(' ') + 1 – jezrael Dec 27 '19 at 11:07 Error : wrapper() takes 1 positional argument but 2 were given New to python so please do let me know if I am missing something. – Kshitij Manvelikar Dec 27 '19 at 11:26 1 @KshitijManvelikar - Maybe help this – jezrael Dec 27 '19 at 11:28 add a comment |"
229,"
Q: Create dummy and categorical variables from specific word(s) in text column in Python dataframe ","0 Here's my take on this. Since you're dealing with text, pandas.Series.str.contains should be plenty (no need to use re.search. np.where and np.select are useful when it comes to assigning new variables based on conditions. import pandas as pd import numpy as np Cars_listing = pd.DataFrame({ 'Cars_notes': ['""This Audi has ABS braking, leather interior and bucket seats...""', '""The Ford F150 is one tough pickup truck, with 4x4, new suspension and club cab...""', '""Our Nissan Sentra comes with ABS brakes, Bluetooth-enabled radio...""', '""This Toyota Corolla is a gem, with new tires, low miles, a few scratches...""', '""The Renault Le Car has been sitting in the garage, a little rust...""', '""The Kia Sorento for sale has a CD player, new tires...""', '""Red Dodge Viper convertible for sale, ceramic brakes, low miles...""'] }) # 1. car_type Cars_listing['car_type'] = np.select( condlist=[ # note you could use the case-insensitive search with `case=False` Cars_listing['Cars_notes'].str.contains('ford', case=False), Cars_listing['Cars_notes'].str.contains('audi|renault', case=False), Cars_listing['Cars_notes'].str.contains('Toyota|Kia') ], choicelist=[1, 2, 3], # dummy variables default=0 # you could set it to `np.nan` etc ) # 2. ABS_brakes Cars_listing['ABS_brakes'] = np.where(# where(condition, [x, y]) Cars_listing['Cars_notes'].str.contains('ABS brak'), 1, 0) # 3. imperfection Cars_listing['imperfection'] = np.where( Cars_listing['Cars_notes'].str.contains('rust|scratches'), 1, 0) # 4. sporty Cars_listing['sporty'] = np.where( Cars_listing['Cars_notes'].str.contains('convertible'), 1, 0) Cars_notes car_type ABS_brakes imperfection sporty 0 """"""This Audi has ..."" 2 1 0 0 1 """"""The Ford F150 ..."" 1 0 0 0 2 """"""Our Nissan Sen..."" 0 1 0 0 3 """"""This Toyota Co..."" 3 0 1 0 4 """"""The Renault Le..."" 2 0 1 0 5 """"""The Kia Sorent..."" 3 0 0 0 6 """"""Red Dodge Vipe..."" 0 0 0 1 share|| follow |||| edited Aug 18 '19 at 20:20 answered Aug 18 '19 at 20:13 political scientistpolitical scientist 2,31822 gold badges88 silver badges2424 bronze badges @johnfrancis don’t forget to accept the answer if it solves the problem - this way the question disappears from unanswered queue – political scientist Aug 21 '19 at 5:44 add a comment |"
230,"
Q: Python : Text Replacement In Large Files ","4 Use the fileinput module, which handles files correctly when replacing data, with the inplace flag set: import sys import fileinput for line in fileinput.input('my_text_file.txt', inplace=True): x = process_result(line) if x: line = line.replace('something', x) sys.stdout.write(line) When you use the inplace flag, the original file is moved to a backup, and anything your write to sys.stdout is written to the original filename (so, as a new file). Make sure you include all lines, altered or not. You have to rewrite the complete file whenever your replacement data is not exactly the same number of bytes as the parts that you are replacing. share|| follow |||| edited May 26 '13 at 21:13 answered May 26 '13 at 20:35 Martijn Pieters♦Martijn Pieters 807k196196 gold badges30223022 silver badges26652665 bronze badges add a comment |"
231,"
Q: How to get output of terminal commands to python gui text area using subprocess? ","1 you can use subprocess.Popen and redirect stdout to PIPE and test simply read stdout e.g complie.stdout.readlines() in you set text area calls import subprocess complie = subprocess.Popen('gcc a.c', stdout=subprocess.PIPE,shell=True) runprog = subprocess.Popen('./a.out', stdout=subprocess.PIPE,shell=True) print (complie.stdout.readlines()) print (runprog.stdout.readlines()) instead of prints you can put complie.stdout.readlines() in your text area set calls Example Code : import subprocess from tkinter import * #Example on Windows root = Tk() T = Text(root, height=2, width=200) T.pack() complie = subprocess.Popen('ping google.com', stdout=subprocess.PIPE,stdin=subprocess.PIPE ,shell=True) complie.wait() outcome = complie.stdout.readlines() print(outcome) T.insert(END, outcome) mainloop() Hope this helps share|| follow |||| answered Apr 29 '18 at 11:09 toheedNiaztoheedNiaz 1,30811 gold badge88 silver badges1313 bronze badges add a comment |"
232,"
Q: Python text game: how to make a save feature? ","10 If I understand the question correctly, you are asking about a way to serialize objects. The easiest way is to use the standard module pickle: import pickle player = Player(...) level_state = Level(...) # saving with open('savefile.dat', 'wb') as f: pickle.dump([player, level_state], f, protocol=2) # loading with open('savefile.dat', 'rb') as f: player, level_state = pickle.load(f) Standard Python objects and simple classes with any level of nesting can be stored this way. If your classes have some nontrivial constructors it may be necessary to hint pickle at what actually needs saving by using the corresponding protocol. share|| follow |||| edited Sep 4 '13 at 6:13 DanielB 2,2381313 silver badges2727 bronze badges answered Sep 4 '13 at 5:56 fjarrifjarri 8,3383333 silver badges4343 bronze badges You could use import cPickle as pickle and use ...protocol=pickle.HIGHEST_PROTOCOL – DanielB Sep 4 '13 at 6:02 To just make sure: Where you have, for example, ""player = Player(...)"". The periods in the parenthesis is just a placeholder, right? In reality, I would place the proper attributes there, yes? – Nick56x Sep 4 '13 at 6:29 Yep. I just put them there to have something concrete to illustrate store/load. See @Mariano's answer for even more concrete example ) – fjarri Sep 4 '13 at 6:43 add a comment |"
233,"
Q: Why is there no Python text highlighting in my Atom Text editor? ","-1 I recommend installing the ide-python package in Atom. You can run this from the command line: apm install atom-ide-ui apm install ide-python Or install ide-python and atom-ide-ui from Install in Atom's settings. share|| follow |||| answered Oct 13 '19 at 20:24 Mark MadejMark Madej 1,20899 silver badges1616 bronze badges add a comment |"
234,"
Q: Unicode vs encoding python text processing ","2 If raw_html.decode() gives you an encoding exception, then it was already Unicode: >>> u'é'.decode('latin1') Traceback (most recent call last): File ""<stdin>"", line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128) because Python 2 implicitly first tries to encode (with the default ASCII codec) when trying to 'decode' Unicode values. share|| follow |||| answered Nov 25 '13 at 11:15 Martijn Pieters♦Martijn Pieters 807k196196 gold badges30223022 silver badges26652665 bronze badges Thanks! sorry... just realized the mistake... when I try to encode it to UTF-8 i get the following error: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal. Is it because some characters can not be encoded in UTF-8? In this case it would make more sense to work the text in unicode, no? Thanks for help!! – user2950162 Nov 25 '13 at 11:35 Work with Unicode where possible. Decoding Unicode makes no sense, you already decoded to Unicode, after all. Check with print type(raw_html). – Martijn Pieters♦ Nov 25 '13 at 11:39 sorry to bother.. but it is driving me crazy... Does Python encodes the loaded text in some format? I am trying to match the euro symbol (€) by its unicode (\u20ac) which I see in the text file but it does not seems to recognize it when I import... any ideas of what I might be doing wrong? I am using r'\\u20ac?\b' to match it and it worked well if I manually input the string as a variable... – user2950162 Nov 25 '13 at 12:06 Make that a new question, perhaps? Why are you escaping the Unicode escape and not using a ur'...' raw Unicode value? – Martijn Pieters♦ Nov 25 '13 at 12:35 Thanks Matijin! I will formulate it as a new question. – user2950162 Nov 25 '13 at 14:19 add a comment |"
235,"
Q: BeautifulSoup Python .text method doesn't return proper text ","0 To get rid of the blank space, I recommend you do something like this: for result in results: chosen_team_results.append(''.join(str(result.parent.text).split())) print(chosen_team_results) share|improve this answer| follow |||| answered Jul 26 '19 at 16:55 Arnav ChawlaArnav Chawla 32422 silver badges1515 bronze badges add a comment |"
236,"
Q: Python text to file on change ","0 so how can I change the input field to a textarea and still have textvariable = name passed through to update name.txt? You can't. The Text widget doesn't support the textvariable option. You don't need it though, you can get the data from the widget the same as you would with textvariable. Namely, by calling the get method on the widget. In your specific case, you can modify writefile to look like this: def writefile(): data = entry.get(""1.0"", ""end-1c"") with open(""./name.txt"", ""r+"") as f: f.truncate(0) f.writelines(data) print(""File has been written !"") root.update() share|improve this answer| follow |||| answered Mar 19 at 16:07 Bryan OakleyBryan Oakley 264k2828 gold badges362362 silver badges520520 bronze badges I've updated my Text () ... but nothing comes through to name.txt :-( Can you se what im doing wrong? – Henrik C Wiberg Mar 19 at 21:00 @HenrikCWiberg: you can't use trace on the Text widget, if that's what you're asking. – Bryan Oakley Mar 19 at 21:38 OK, do you have any idea how to get the content from a text area, into the name.txt without a submit button? – Henrik C Wiberg Mar 19 at 22:40 @HenrikCWiberg: yes, but that's a separate question. The comment section isn't designed for large conversations or large answers. You should either ask a new question, or edit your question to make it more clear that you're wanting to save the data on every keystroke. – Bryan Oakley Mar 19 at 22:44 Thanks for your reply – Henrik C Wiberg Mar 19 at 23:01 add a comment |"
237,"
Q: Python: Image to text ","0 The FileNotFoundError is is raised by subprocess, which is trying to launch an external process. The command it's trying to launch is not found. You can see a couple lines up in the traceback it says: File ""D:\Python\lib\site-packages\pytesseract\pytesseract.py"", line 46, in run_tesseract proc = subprocess.Popen(command, stderr=subprocess.PIPE) To use pytesseract you need some other dependencies installed on your system, see this section: https://pypi.python.org/pypi/pytesseract/0.1 INSTALLATION: Python-tesseract requires python 2.5 or later. You will need the Python Imaging Library (PIL). Under Debian/Ubuntu, this is the package ""python-imaging"". Install google tesseract-ocr from http://code.google.com/p/tesseract-ocr/ . You must be able to invoke the tesseract command as ""tesseract"". If this isn't the case, for example because tesseract isn't in your PATH, you will have to change the ""tesseract_cmd"" variable at the top of 'tesseract.py'. Try typing tesseract in a command shell, if it doesn't work then you're not set up to use that package yet. Follow their install instructions. share|improve this answer| follow |||| answered Jun 15 '17 at 15:56 calico_calico_ 9011010 silver badges2020 bronze badges Tesseract is installled, can it be because I have installled tesseract in C: ddirectory and I am running python and pytestesseract in D : directory – Abhishek Choudhery Jun 16 '17 at 2:09 it was that issue only. I uninstalled the Tesseract from C directory and installed in D directory. Thanks bro – Abhishek Choudhery Jun 16 '17 at 4:31 add a comment |"
238,"
Q: Image to text python ","7 You have to have tesseract installed and accesible in your path. According to source, pytesseract is merely a wrapper for subprocess.Popen with tesseract binary as a binary to run. It does not perform any kind of OCR itself. Relevant part of sources: def run_tesseract(input_filename, output_filename_base, lang=None, boxes=False, config=None): ''' runs the command: `tesseract_cmd` `input_filename` `output_filename_base` returns the exit status of tesseract, as well as tesseract's stderr output ''' command = [tesseract_cmd, input_filename, output_filename_base] if lang is not None: command += ['-l', lang] if boxes: command += ['batch.nochop', 'makebox'] if config: command += shlex.split(config) proc = subprocess.Popen(command, stderr=subprocess.PIPE) return (proc.wait(), proc.stderr.read()) Quoting another part of source: # CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY tesseract_cmd = 'tesseract' So quick way of changing tesseract path would be: import pytesseract pytesseract.tesseract_cmd = ""/absolute/path/to/tesseract"" # this should be done only once pytesseract.image_to_string(img) share|improve this answer| follow |||| edited Jul 25 '16 at 5:09 answered Jul 21 '16 at 16:07 Łukasz RogalskiŁukasz Rogalski 16.9k66 gold badges4444 silver badges7777 bronze badges I think you are right but I have installed tesseract but it still gives the same error. Infact the brutal part is that when I open the image using image.show() method it dies open the image but in the very next line when I process the image it throws FileNotFoundError. I am completely stuck :( – muazfaiz Jul 24 '16 at 23:08 FileNotFoundError is from lack of tesseract, not lack of image file itself. See edit to my answer. – Łukasz Rogalski Jul 25 '16 at 5:09 add a comment |"
239,"
Q: Unicode Text Not Printing to Python Console/Terminal/Screen ","3 That is probably not because of your encoding, since python 3 uses utf-8 as default. More probable issue is that your console uses the font consolas which has no hebrew support. Change to a font like courier new to show the hebrew characters in the console. On windows - simply hit the icon on top of the window (should be up-left or up-right if your windows is hebrew). Then hit properties (הגדרות) and choose the font you want (I recommends courier new). EDIT The problem seems to be the use of the character \u05be (מקף) in the text. I tried the following when loading the file and it worked as it should: json_data = open('DATA_1GENESIS.json', encoding=""utf8"").read().replace('\u05be', '') share|improve this answer| follow |||| edited Oct 29 '16 at 20:26 answered Oct 29 '16 at 20:00 UrielUriel 12.6k33 gold badges1818 silver badges4040 bronze badges 1 what characters do you see? question mark? or gibrish? – Uriel Oct 29 '16 at 20:07 Please see EDIT above. The test print works fine when I just placed the Hebrew text in a simple string. – Jerusalem Programmer Oct 29 '16 at 20:11 I used your code, but still it does not address the problem that I described of disappearing output from the Python screen/console/terminal. Why do you say that it worked as it should? What worked in your proposed solution? – Jerusalem Programmer Oct 29 '16 at 20:40 Did you copy/paste my code and attempt to see the problem with disappearing output as I described? – Jerusalem Programmer Oct 29 '16 at 20:41 yes. printed out all of it, along with the hebrew text – Uriel Oct 29 '16 at 20:50 | show 1 more comment"
240,"
Q: Can someone help me fix this code for a Python Text Adventure RPG Game? [closed] ",1 You've misspelled the __init__ method name. You have defined a method called _int_. So Python fails to find the game_over attribute as it wouldn't be defined until _int_ is called. share|improve this answer| follow |||| answered Oct 17 '19 at 16:53 R001WR001W 7122 bronze badges I changed the init but it still quits when I try to get past the introduction phase. I require assistance from anyone. – Ya_Boi_Havoc Oct 18 '19 at 16:13 add a comment |
241,"
Q: text summarization using Text-Summarizer in python ","0 Check if you have a circular dependencies import. This might just be the same answer as this question here: ImportError: Cannot import name X share|improve this answer| follow |||| answered Aug 21 '18 at 14:44 NotSoShabbyNotSoShabby 1,49022 gold badges1212 silver badges3232 bronze badges add a comment |"
242,"
Q: Encoding Text files with Python ","1 I don't know if you're using python 3. Check this out here: https://docs.python.org/3/howto/unicode.html . Moreover, you should use UTF-8 to encode your unicode. CP1255 has only Hebrew in it https://en.wikipedia.org/wiki/Windows-1255 Your console should support unicode, which is why you see it properly there. Basically, what I'm saying is: If you use Python 3 and use no shenanigan at all, it should work by default :-) If you force an old encoding... then you'll get down the rabbit hole... share|improve this answer| follow |||| answered May 7 '18 at 15:50 Samuel GIFFARDSamuel GIFFARD 59122 silver badges1414 bronze badges add a comment |"
243,"
Q: Python reading text files ","0 what is f_contents it's supposed to be just print(contents)after reading in each line and storing it to contents. Hope that helps :) An example of reading a file content: with open(""criticaldocuments.txt"", ""r"") as f: for line in f: print(line) #prints all the lines in this file #allows the user to iterate over the file line by line OR what you want is something like this using readlines(): with open(""criticaldocuments.txt"", ""r"") as f: contents = f.readlines() #readlines() will store each and every line into var contents if contents == None: print(""No lines were stored, file execution failed most likely"") elif contents == ""Password is Password"": print(""We cracked it"") else: print(contents) # this returns all the lines if no matches Note: contents = f.readlines() Can be done like this too: for line in f.readlines(): #this eliminates the ambiguity of what 'contents' is doing #and you could work through the rest of the code the same way except #replace the contents with 'line'. share|improve this answer| follow |||| edited Feb 17 at 21:13 Kingsley 6,84244 gold badges1818 silver badges3434 bronze badges answered Feb 17 at 20:33 FishingCodeFishingCode 66655 silver badges1111 bronze badges Im kinda confused, I just started learning this stuff could you give me a example of the code? – lukes Feb 17 at 20:44 sure no problem – FishingCode Feb 17 at 20:44 add a comment |"
244,"
Q: Python text-based random questions quiz ","1 Questions 1, 3, and 5 are identical, so you only have three unique questions, so only three questions are displayed. random_question in used_questions evaluates to True as long as one element of used_questions is equal to random_question; they do not have to refer to the exact same list in memory. As mentioned in the comments and other answers, shuffling the list is an easier approach here. import random questions=[ [""The answer is A"",""A"",""a""], [""The answer is B"",""B"",""b""], [""The answer is A"",""A"",""a""], [""The answer is F"",""F"",""f""], [""The answer is A"",""A"",""a""] ] random.shuffle(questions) for question in questions: print(question[0]) players_answer=input("""") if players_answer in question: print(""\nCorrect!"") else: print(""\nWrong!"") Result: The answer is F F Correct! The answer is A B Wrong! The answer is A Q Wrong! The answer is B B Correct! The answer is A A Correct! share|improve this answer| follow |||| edited Sep 25 '17 at 13:57 answered Sep 25 '17 at 13:55 KevinKevin 66k1111 gold badges8585 silver badges126126 bronze badges Thanks a lot! I would have probably never realised – VEyeZen Sep 25 '17 at 13:57 add a comment |"
245,"
Q: How to change easyGUI python text color ","0 Quoting from this site: EasyGUI won;t help change colour, it is just a way of making a command line script act like a GUI - by popping up dialog boxes to capture input and dsplay messages. It replaces raw_input and print with dialogs. --------code-------- from WConio import textcolor apples_left = 5 print ""There are"",(textcolor(4)),apples_left,(textcolor(7)),""left in the basket."" The output works, but there is one snag. I think that I am leaving something out because it has the word ""None"" on both sides of the variable. This could be an easy solution for your problem. You may get WConio from here: http://newcenturycomputers.net/projects/wconio.html If you face python not found error at the time of installation then you may try following this answer. UPDATE: There is one more package named Colorama, which is cross-platform for colored terminal text and pretty much stable. You may give it a try as well. share|improve this answer| follow |||| edited May 23 '17 at 11:53 Community♦ 111 silver badge answered Mar 6 '17 at 2:45 Shubham NamdeoShubham Namdeo 1,23211 gold badge1515 silver badges3535 bronze badges add a comment |"
246,"
Q: how do i change the color of a specific word in a text widget in python [duplicate] ","1 There are some methods created for you to achieve this. I suggest read the Tk documentation (Text, Text.search(), Tags, Indexes)! Tk provide the text.search method for you so you don't need to implement your own. Tk Text widget provide you tags which you can create and modify tags. Workflow: 1. search for a pattern with the text.search() method that will return an index of the beginning position 2. create a tag with the text.tag_config() 3. add the created tag with the text.tag_add() from tkinter import Tk, Entry, Button, Text, IntVar from tkinter import font class Text_tag_example(): def __init__(self, master): self.master = master self.my_font = font.Font(family=""Helvetica"",size=18) self.startindex = ""1.0"" #needed for search method, index (""line, column"") self.endindex = ""end"" #needed for search method, index (end of index) self.init_widgets() def init_widgets(self): self.txt_widget = Text(self.master, font=self.my_font, height=10, width=40) self.txt_widget.grid(row=0, columnspan=2) self.ent_string = Entry(self.master, font=self.my_font) self.ent_string.grid(row=1, column=0) self.but_search = Button(self.master, text=""Search"", font=self.my_font, command=self.search_word) self.but_search.grid(row=1, column=1) def search_word(self): word = self.ent_string.get() #get string from entry countVar = IntVar() # contain the number of chars that matched searched_position = self.txt_widget.search(pattern=word, index=self.startindex, stopindex=self.endindex, count=countVar) self.txt_widget.tag_config(""a"", foreground=""blue"", underline=1) endindex = ""{}+{}c"".format(searched_position, countVar.get()) #add index+length of word/pattern self.txt_widget.tag_add(""a"", searched_position, endindex) if __name__ == ""__main__"": root = Tk() app = Text_tag_example(root) root.mainloop() Usage: -type in text widget ""hello hi bye"" -type in entry widget ""hi"" -press Search button -""hi"" should be blue and underlined Probably your next question will be ""How to tag all the same words in text?"" Again read the documentation otherwise you will not be able to understand it! share|improve this answer| follow |||| answered Dec 7 '19 at 19:02 Module_artModule_art 56433 silver badges1919 bronze badges add a comment |"
247,"
Q: Change python interpreter - Sublime Text 2 ","18 Tools > Build System > New Build System. Insert the file and with your python version: { ""cmd"": [""python3"", ""-u"", ""$file""], ""file_regex"": ""^[ ]*File \""(...*?)\"", line ([0-9]*)"", ""selector"": ""source.python"" } share|improve this answer| follow |||| edited Oct 9 '17 at 16:59 Martin Valgur 3,5491616 silver badges3333 bronze badges answered Aug 4 '12 at 9:20 Davoud Taghawi-NejadDavoud Taghawi-Nejad 12.1k1111 gold badges4949 silver badges7171 bronze badges I tried what you said, with python version [""/Library/Frameworks/Python.framework/Versions/Current/bin/python"" since I have many python version :(, when I try to run the program on shell, it runs fine, but with sublime, I see print soup.prettify() UnicodeEncodeError: 'ascii' codec can't encode character u'\xbb' in position 6231: ordinal not in range(128), My code is #! /usr/bin/env python import urllib2 from bs4 import BeautifulSoup f = urllib2.urlopen('http://www.google.com') soup = BeautifulSoup(f.fp) print soup.prettify() – daydreamer Jan 9 '13 at 18:31 1 To get the above advice to work on OS X I needed to add a symbolic link into usr/bin to the location of my Python 3.3 installation. Like this: sudo ln -s ""/Library/Frameworks/Python.framework/Versions/3.3/bin/python3"" ./usr/bin/python3 – jwpfox Mar 3 '13 at 9:31 1 I'm not sure why and maybe someone else can clear it up for me, but when I put ""python2"" where it says ""python3"", the build system did not work. Changing ""python3"" to ""C:\\Python27\\python.exe"" did work for me however. – Dante Oct 4 '15 at 5:08 add a comment |"
248,"
A: StringIO in Python3
","745 when i write import StringIO it says there is no such module. From What’s New In Python 3.0: The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO or io.BytesIO for text and data respectively. . A possibly useful method of fixing some Python 2 code to also work in Python 3 (caveat emptor): try: from StringIO import StringIO ## for Python 2 except ImportError: from io import StringIO ## for Python 3 Note: This example may be tangential to the main issue of the question and is included only as something to consider when generically addressing the missing StringIO module. For a more direct solution the the message TypeError: Can't convert 'bytes' object to str implicitly, see this answer. share|improve this answer| follow |||| edited Aug 24 '19 at 15:18 answered Aug 17 '13 at 3:43 nobarnobar 33.9k1111 gold badges106106 silver badges121121 bronze badges 12 Worth mentioning these are not the same, so you can end up with TypeErrors ( string argument expected, got 'bytes') if you make this change in isolation. You need to carefully distinguish btyes and str (unicode) in python 3. – Andy Hayden Apr 22 '15 at 3:13 7 For newbs like me: from io import StringIO means you call it as StringIO(), not io.StringIO(). – Noumenon Jul 7 '15 at 23:43 10 How to actually be compatible with Python 2 and 3: just from io import StringIO – Oleh Prypin Aug 11 '15 at 19:35 8 THIS IS SIMPLY WRONG for numpy.genfromtxt() in python 3. Please refer to the answer from Roman Shapovalov. – Bill Huang Mar 5 '16 at 10:01 1 @nobar: The latter. The original question uses python 3.x, from which the module StringIO is gone and from io import BytesIO should be applied instead. Tested myself on python 3.5 @ eclipse pyDev + win7 x64. Please let me know if I were wrong thanks. – Bill Huang Mar 5 '16 at 17:16 | show 9 more comments"
249,"
Q: Python: words replacing in huge text ","3 Input format and search / replace pairings info is going to necessary to refine this answer if it comes close to start with, but this would be my initial stab at it (assuming some form of regularity in the input data, space delimited in my example code below). replacements = { 's1': 'r1', 's2': 'r2' ... } with open('input.txt') as fhi, open('output.txt', 'w') as fho: for line in fhi: words = line.split(' ') fho.write(' '.join(map(lambda w: replacements.get(w, w), words)) # Or as a list comprehension from the comments. fho.write(' '.join([replacements.get(w, w) for w in words])) The idea here is that we'll be relocating data into an output file from an input file. For each word of each line, we check to see if it's in our replacements dictionary. We retrieve the new value if it is, or return the word unchanged otherwise via the dict.get(key[, default]) method. This may not be ideal, doesn't handle punctuation, would probably have trouble on an input file that wasn't broken into lines, etc, but may be a way to get started. share|improve this answer| follow |||| edited Jun 1 '11 at 22:57 answered Jun 1 '11 at 22:17 g.d.d.cg.d.d.c 38k77 gold badges8080 silver badges9999 bronze badges 1 Note, I've leveraged the 2.7 syntax for with that supports multiple files. If you're working with 2.5 or 2.6 you'll have to nest with statements in order to deal with two files (or create a custom context manager that handles more than one file). – g.d.d.c Jun 1 '11 at 22:19 Thanks, your solution ' '.join(map(lambda w: replacements.get(w, w), words)) is very fast in my case (load all data in the memory + ""extremely tokenized"" text). – Alex Jun 1 '11 at 22:33 @Alex - Glad to help. Be sure to tag an answer if you've found an approach that works so that people don't continue to try to solve it for you. :) – g.d.d.c Jun 1 '11 at 22:37 1 how does this compare in performance to a list comprehension? out.write( ' '.join( [ replacements.get( w, w ) for w in line.split() ] ) – dusktreader Jun 1 '11 at 22:51 @dusktreader - The list comprehension may actually end up being a bit faster, is definitely possible. I'll update the answer to include it as an alternate. Thanks! – g.d.d.c Jun 1 '11 at 22:56 | show 2 more comments"
250,"
Q: python text parsing to get the value based on coulmns ",0 Iterate over the lines in the file and keep or discard the lines that match your criteria. Split each line on whitespace use indexing to extract the columns you care about share|improve this answer| follow |||| answered Dec 10 '19 at 3:55 wwiiwwii 15.2k44 gold badges2424 silver badges5858 bronze badges add a comment |
251,"
Q: Searching python text file without for loops and if statments ","3 Reading a sequential file (e.g. a text file) is always going to be a sequential process. Unless you can store it in separate chunks or skip ahead somehow it will be hard to do any parallel processing. What you could do is separate the inherently sequential reading process from the searching process. This requires that the file content be naturally separated into chunks (e.g. lines) across which the search is not intended to find a result. The general structure would look like this: initiate a list of processing threads with input queues read the file line by line and accumulate chunks of lines up to a given threshold when the threshold or the end of file is reached, add the chunk of lines to the next processing thread's input queue wait for all processing threads to be done merge results from all the search threads. In this era of solid state drives and fast memory busses, you would need some pretty compelling constraining factors to justify going to that much trouble. You can figure out your minimum processing time by measuring how long it takes to read (without processing) all the lines in your largest file. It is unlikely that the search process for each line will add much to that time given that I/O to read the data (even on an SSD) will take much longer than the search operation's CPU time. share|improve this answer| follow |||| edited Mar 2 at 13:31 answered Feb 28 at 14:35 Alain T.Alain T. 15.7k11 gold badge2020 silver badges3636 bronze badges add a comment |"
252,"
Q: Highlight text in idle python ","0 Since IDLE shell isn't a terminal, it won't render the ANSI escape sequences. So the result you got: >>> from lazyme.string import color_print >>> color_print('abc', color='pink') [95mabc[0m though it is correct, it won't render the color for you. share|improve this answer| follow |||| answered Jul 7 '17 at 7:09 Louie LuLouie Lu 1155 bronze badges add a comment |"
253,"
Q: Append text to a Text file without replacing it Python [duplicate] ","1 When you write to a file it always effectively overwrites the bytes inside the file stream. What you might want to do instead, is read the file first, and write the necessary parts, and then write your original contents back: with open(filename,'r+',encoding=""UTF-8"") as file: data = file.read() file.write('test\n') file.write(data) This should be all you need. Remove the f = open(filename) and file_contents = f.read() lines, because you are opening the same file twice. share|improve this answer| follow |||| answered Dec 24 '19 at 17:50 r.ookr.ook 9,15311 gold badge1212 silver badges2727 bronze badges add a comment |"
254,"
Q: editing text via python ","2 with open('file.txt', 'r') as f: for line in f: print '%s:OK' % line.rstrip('\r\n') share|improve this answer| follow |||| edited Feb 26 '11 at 1:25 answered Feb 26 '11 at 0:44 ThiefMasterThiefMaster 267k7171 gold badges519519 silver badges586586 bronze badges dude it's showing error `Traceback (most recent call last): File ""C:\Users\Hamoud\Desktop\PY.py"", line 4, in <module> print '%s:OK' % f.rstrip('\r\n') AttributeError: 'file' object has no attribute 'rstrip'1 – hamoud Feb 26 '11 at 1:05 He meant print '%s:OK' % line.rstrip('\r\n') – Hut8 Feb 26 '11 at 1:15 add a comment |"
255,"
Q: Python: Inserting text after a tab in a text file ","2 You can't use seek for that. It is used to position the file cursor at a certain position in a file. (I.e. set the cursor to a position as a count of characters). If you really want to insert you have to rewrite everything behind the cursor position anyway, otherwise your insertion overwrites bits of the file. One way to do this is this: fd = open(filename, ""r+"") text = fd.read() text = text.replace(""\t"", ""\t"" + ""Inserted text"", 1) fd.seek(0) fd.write(text) fd.close() share|improve this answer| follow |||| edited May 19 '15 at 11:43 answered May 19 '15 at 10:50 tedted 4,21544 gold badges2828 silver badges6969 bronze badges 1 I think you wanted to do txt.read() instead of txt.read and .replace(""\t"", ""\t""+INSERTED_TEXT, 1) instead of .replace(""\t"", ""\t"", 1) – Elisha May 19 '15 at 11:17 @Elisha: thanks for spotting this, and feel free to just correct obvious mistakes like this, after all this is why you can edit others posts (I believe). – ted May 19 '15 at 11:24 add a comment |"
256,"
Q: Python text file lines keep formatting ","4 Remove newline from the string: def main(): infile=open(""putNamesHere.txt"",""r"") outfile=open(""getFromHere.txt"",""w"") for line in infile: line=line.upper() mystring=('*'+line.rstrip()+'*\n') outfile.write(mystring) share|improve this answer| follow |||| answered Aug 8 '13 at 20:13 4d4c4d4c 5,94733 gold badges2121 silver badges2929 bronze badges 2 Use with ... as ... or remember to close the files. – squiguy Aug 8 '13 at 20:19 Consider using an argument to str.rstrip() if end-of-line white space needs to be preserved: mystring='*'+line.rstrip('\n')+'*\n' – Robᵩ Aug 8 '13 at 20:23 The advice about with ... as ... should be done to the asker. – rook Aug 8 '13 at 20:32 add a comment |"
257,"
Q: Is there a Python text mining script to classify text with multiple classifications? ","0 I suggest you look up https://skymind.ai/wiki/word2vec, word to vectorized allows for vectorization of phrases and sentence to apply more context to the word. Word to vec models create better word association models. I would also search google scholar for papers including NLP AND word2vec AND NIPS AND categorization. This search yielded 4,300+ papers that would give you a lot of direction in solving your problem. If you only want one category to be chosen over all this is a very difficult task. I saw a presentation on #Mailchimps NLP model for classifying client content into categories and sometimes the correct category would literally be the 4th one. The model they created was very well done but still couldn't detect some edge cases and contained some classic biases toward more common categories over the less common. https://scholar.google.com/scholar?hl=en&as_sdt=0%2C11&q=NLP+AND+word2vec+AND+categorization+AND+mailchimp&btnG= The recommendation engine paper is tied to your task because the complexity of predicting context of small amount of words in order to make a search suggestion is a similar problem. share|improve this answer| follow |||| answered Nov 9 '19 at 20:16 Michael HearnMichael Hearn 37722 silver badges1111 bronze badges Thank you very much Mr. Hearn, it is much appreciated. – Enduringness Nov 10 '19 at 23:04 add a comment |"
258,"
Q: Text extraction using python [duplicate] ","831 +500 You misunderstand how boolean expressions work; they don't work like an English sentence and guess that you are talking about the same comparison for all names here. You are looking for: if x == 1 or y == 1 or z == 1: x and y are otherwise evaluated on their own (False if 0, True otherwise). You can shorten that using a containment test against a tuple: if 1 in (x, y, z): or better still: if 1 in {x, y, z}: using a set to take advantage of the constant-cost membership test (in takes a fixed amount of time whatever the left-hand operand is). When you use or, python sees each side of the operator as separate expressions. The expression x or y == 1 is treated as first a boolean test for x, then if that is False, the expression y == 1 is tested. This is due to operator precedence. The or operator has a lower precedence than the == test, so the latter is evaluated first. However, even if this were not the case, and the expression x or y or z == 1 was actually interpreted as (x or y or z) == 1 instead, this would still not do what you expect it to do. x or y or z would evaluate to the first argument that is 'truthy', e.g. not False, numeric 0 or empty (see boolean expressions for details on what Python considers false in a boolean context). So for the values x = 2; y = 1; z = 0, x or y or z would resolve to 2, because that is the first true-like value in the arguments. Then 2 == 1 would be False, even though y == 1 would be True. The same would apply to the inverse; testing multiple values against a single variable; x == 1 or 2 or 3 would fail for the same reasons. Use x == 1 or x == 2 or x == 3 or x in {1, 2, 3}. share|improve this answer| follow |||| edited Mar 12 '18 at 19:56 answered Feb 27 '13 at 12:27 Martijn Pieters♦Martijn Pieters 807k196196 gold badges30223022 silver badges26652665 bronze badges 111 I wouldn't be so quick to go for the set version. Tuple's are very cheap to create and iterate over. On my machine at least, tuples are faster than sets so long as the size of the tuple is around 4-8 elements. If you have to scan more than that, use a set, but if you are looking for an item out of 2-4 possibilities, a tuple is still faster! If you can arrange for the most likely case to be first in the tuple, the win is even bigger: (my test: timeit.timeit('0 in {seq}'.format(seq=tuple(range(9, -1, -1))))) – SingleNegationElimination Oct 24 '13 at 15:27 52 @dequestarmappartialsetattr: In Python 3.3 and up, the set is stored as a constant, bypassing the creation time altogether, eliminating the creation time. Tuples can be cheap to create as Python caches a bundle of them to avoid memory churn, making that the biggest difference with sets here. – Martijn Pieters♦ Oct 24 '13 at 15:29 11 @dequestarmappartialsetattr: If you time just the membership test, for integers sets and tuples are equally fast for the ideal scenario; matching the first element. After that tuples lose out to sets. – Martijn Pieters♦ Oct 24 '13 at 15:37 15 @MartijnPieters: Using the set literal notation for this test isn't a savings unless the contents of the set literal are also literals, right? if 1 in {x, y, z}: can't cache the set, because x, y and z could change, so either solution needs to build a tuple or set from scratch, and I suspect whatever lookup savings you might get when checking for membership would be swamped by greater set creation time. – ShadowRanger Sep 4 '16 at 0:37 7 @ShadowRanger: yes, peephole optimisation (be it for in [...] or in {...}) only works if the contents of the list or set are immutable literals too. – Martijn Pieters♦ Sep 4 '16 at 7:58 | show 14 more comments"
259,"
Q: Extract text from HTML in python [duplicate] ","5 BeautifulSoup is a popular option for reading and parsing HTML pages. share|improve this answer| follow |||| answered Jan 13 '12 at 2:19 MakotoMakoto 90.4k2020 gold badges152152 silver badges196196 bronze badges Dang. What easy points, @Makoto! :D – Droogans Jan 13 '12 at 2:48 add a comment |"
260,"
Q: Text plot with Python ","0 The axes object in matplotlib.pyplot offers a simple method, text() for plotting text. Example: import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.text(5, 3, ""I am a boy"") ax.set_xlim([0,7]) # Expand the plot limits to include the text. ax.set_ylim([0,7]) plt.show() Result: However, the method annotate() provides additional features for ease in annotating data. share|improve this answer| follow |||| answered Nov 18 '17 at 23:44 Mark HannelMark Hannel 67755 silver badges1111 bronze badges add a comment |"
261,"
Q: Text Shadow With Python ","8 Have a look at these examples. Example 1 Example 2 and the last one is kinda similar to what you attempted. import Image, ImageFont, ImageDraw import win32api, os x, y = 10, 10 fname1 = ""c:/test.jpg"" im = Image.open(fname1) pointsize = 30 fillcolor = ""red"" shadowcolor = ""yellow"" text = ""hi there"" font = win32api.GetWindowsDirectory() + ""\\Fonts\\ARIALBD.TTF"" draw = ImageDraw.Draw(im) font = ImageFont.truetype(font, pointsize) # thin border draw.text((x-1, y), text, font=font, fill=shadowcolor) draw.text((x+1, y), text, font=font, fill=shadowcolor) draw.text((x, y-1), text, font=font, fill=shadowcolor) draw.text((x, y+1), text, font=font, fill=shadowcolor) # thicker border draw.text((x-1, y-1), text, font=font, fill=shadowcolor) draw.text((x+1, y-1), text, font=font, fill=shadowcolor) draw.text((x-1, y+1), text, font=font, fill=shadowcolor) draw.text((x+1, y+1), text, font=font, fill=shadowcolor) # now draw the text over it draw.text((x, y), text, font=font, fill=fillcolor) fname2 = ""c:/test2.jpg"" im.save(fname2) os.startfile(fname2) share|improve this answer| follow |||| answered Sep 24 '13 at 8:44 cyberbemoncyberbemon 2,21399 gold badges2626 silver badges4949 bronze badges add a comment |"
262,"
Q: Text Mining Library for Python ","1 I think scikit-learn should fit your needs. It's difficult to say without knowing exactly what your data will look like, but there are many text manipulation functions and built-in clustering algorithms within scikit-learn's API. share|improve this answer| follow |||| answered Jan 13 '14 at 23:50 willwestwillwest 35433 silver badges1313 bronze badges add a comment |"
263,"
Q: Plain-text formatting in python [closed] ","2 .txt files do not have formatting. You would have to use HTML or some other format of rich text/markup. Example of HTML: This is <b>bold</b> text. This is <u>underlined</u>. share|improve this answer| follow |||| edited Nov 18 '12 at 16:39 Jon Clements♦ 114k2424 gold badges199199 silver badges241241 bronze badges answered Nov 18 '12 at 16:36 DoorknobDoorknob 48.7k2020 gold badges9494 silver badges137137 bronze badges So it isn't possible to see the underlined or bold text in notepad, right? – stensootla Nov 18 '12 at 16:39 @geekkid No, it is not, unless you use rtf or something of the sort. en.wikipedia.org/wiki/Rich_Text_Format – Doorknob Nov 18 '12 at 16:39 As a note, you'll need wordpad (or whatever it's called these days) for RTF files – Jon Clements♦ Nov 18 '12 at 16:41 Yes, just plain notepad can't do anything like that. Wordpad is preinstalled on all Windows computers, I think. – Doorknob Nov 18 '12 at 16:41 1 @geekkid but for what you're doing, you could just use HTML and save it as .html. Then open it in a web browser. – Doorknob Nov 18 '12 at 16:48 | show 5 more comments"
264,"
Q: simple python text adventure game errors [closed] ","2 From a quick glance at it. #gold = int(100) is commented out on line 1. This causes a issue because it doesn't know what gold is. it isn't defined. remove the # before it. share|improve this answer| follow |||| edited Mar 1 at 8:19 petezurich 5,33388 gold badges2424 silver badges3838 bronze badges answered Mar 1 at 8:10 Terence Towndrow 8XTerence Towndrow 8X 3866 bronze badges add a comment |"
265,"
Q: Separate blocks of text python ","2 Here's a modified example of something similar I did recently, basically goes through your text and copies over line by line. The core logic is based around appending to the current file name, which is reset after it finds a new section. Will use the first line of the next section as the filename. #!/usr/bin/env python import re data = """""" Channel 9 (1 item) A woman selling her caravan near Bendigo has been left $1,100 out hosted by Peter Hitchener A woman selling her caravan near Bendigo has been left $1,100 out of pocket after an elderly couple made the purchase with counterfeit money. The wildlife worker tried to use the notes to pay for a house deposit, but an agent noticed the notes were missing the Coat of Arms on one side. Brief: Radio & TV Demographics: 153,000 (male 16+) • 177,000 (female 16+) Southern Cross Victoria Bendigo (1 item) Heathcote Police are warning the residents to be on the lookout a hosted by Jo Hall Heathcote Police are warning the residents to be on the lookout after a large dash of fake $50 note was discovered. Victim Marianne Thomas was given counterfeit notes from a caravan. The Heathcote resident tried to pay the house deposit and that's when the counterfeit notes were spotted. Thomas says the caravan is in town for the Spanish Festival. Brief: Radio & TV Demographics: 4,000 (male 16+) • 3,000 (female 16+) """""" current_file = None for line in data.split('\n'): # Set initial filename if current_file == None and line != '': current_file = line + '.txt' # This is to handle the blank line after Brief if current_file == None: continue text_file = open(current_file, ""a"") text_file.write(line + ""\n"") text_file.close() # Reset filename if we have finished this section # which is idenfitied by: # starts with Brief - ^Brief # contains some random amount of text - .* # ends with ) - )$ if re.match(r'^Brief:.*\)$', line) is not None: current_file = None This will output the following files Channel 9 (1 item).txt Southern Cross Victoria Bendigo (1 item).txt share|improve this answer| follow |||| answered May 30 '18 at 1:24 harveyharvey 2,70766 silver badges99 bronze badges add a comment |"
266,"
Q: Getting selected text (python-gtk) ","1 self.s_text=gtk.SelectionData.get_text Method get_text is not called yet! You are assigning self.s_text to the method(function) object itself. Which is converted to string in ""Selected Text is : %s"" % self.s_text You should changed it to: self.s_text=gtk.SelectionData.get_text() EDIT: But since you need a SelectionData object to pass to this method, the whole idea is wrong. I combined you two codes as something working: #!/usr/bin/env python import pygtk pygtk.require('2.0') import gtk class MyApp (object): def __init__(self): self.window=gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.connect(""delete_event"", gtk.main_quit ) self.entry = gtk.Entry() self.window.selection_convert(""PRIMARY"", ""STRING"") self.window.connect(""selection_received"", self.selection_received) self.window.add(self.entry) self.window.show_all() # Signal handler called when the selections owner returns the data def selection_received(self, widget, selection_data, data): print 'selection_data.type=%r'%selection_data.type # Make sure we got the data in the expected form if str(selection_data.type) == ""STRING"": self.entry.set_text(""Selected Text is : %s"" % selection_data.get_text()) elif str(selection_data.type) == ""ATOM"": # Print out the target list we received targets = selection_data.get_targets() for target in targets: name = str(target) if name != None: self.entry.set_text(""%s"" % name) else: self.entry.set_text(""(bad target)"") else: self.entry.set_text(""Selection was not returned as \""STRING\"" or \""ATOM\""!"") return False def main(self): gtk.main() app=MyApp() app.main() share|improve this answer| follow |||| edited Dec 25 '12 at 5:56 answered Dec 24 '12 at 4:52 saeedgnusaeedgnu 2,9592222 silver badges4040 bronze badges i tried this but it is not working ! it turns me : self.s_text=gtk.SelectionData.get_text() TypeError: descriptor 'get_text' of 'gtk.SelectionData' object needs an argument what is the diffrence wtih the first code !?!?! – J3y4Nf3R Dec 24 '12 at 10:56 add a comment |"
267,"
Q: transferring python text into word document [duplicate] ","1913 Here is the best way, for me, to add code inside word: go to Insert tab, Text section, click object button (it's on the right) choose OpenDocument Text which will open a new embedded word document copy and paste your code from Visual Studio / Eclipse inside this embedded word page save and close The result looks very nice. Here are the advantages of this method: the code keeps its original layout and colors the code is separated from the rest of the document, as if it was a picture or a chart spelling errors won't be highlighted in the code (this is cool !) And it takes only few seconds. share|improve this answer| follow |||| edited Apr 1 '19 at 7:48 Serj Sagan 21.6k1515 gold badges127127 silver badges149149 bronze badges answered Apr 16 '10 at 13:54 gargamelgargamel 19.3k11 gold badge1313 silver badges33 bronze badges 20 Brilliant! I've been fiddling with styles, tables and other workarounds for years. This fixes everything in one go. Note that you also get the bonus of adding a caption to your code. I added a new label type ""Listing"" for brownie points :-) – Nebula Jan 31 '13 at 12:47 6 Please note tath this requires open/libre office to be installed on your machine. – BetaRide Oct 10 '13 at 6:06 14 @BetaRide No, it doesn't. I just used this on a document on a machine without OpenOffice installed. OpenDocument refers to a file format with support from MS Office among others: en.wikipedia.org/wiki/OpenDocument – Jamie F Oct 10 '13 at 17:51 44 Can't see that option on Word 2011 on OSX :( – Matt Gibson Nov 8 '13 at 14:10 39 For Word 2011 on OSX use: Insert -> Object...Microsoft Word Document – s_t_e_v_e Apr 7 '14 at 17:19 | show 29 more comments"
268,"
Q: Building Python in Sublime Text ","12 I fixed this by saving my file as a .py file. I opened up a new file, added some code, and tried to build it, which gave me that error. After saving as a .py file, I was able to build and run fine. share|improve this answer| follow |||| answered Apr 6 '13 at 2:45 Clint PowellClint Powell 2,24822 gold badges1313 silver badges1919 bronze badges add a comment |"
269,"
Q: Python colored text toggle issue [duplicate] ","0 You're overwriting the hello function with a string also called hello in the hello function. You could try to rename your hello string to greeting and using that instead. share|improve this answer| follow |||| answered May 13 '18 at 0:20 JoostJoost 2,60511 gold badge77 silver badges2020 bronze badges Haha thank you I appreciate it. – Jake May 13 '18 at 0:23 add a comment |"
270,"
Q: Python regex match text between quotes ","14 match starts searching from the beginning of the text. Use search instead: #!/usr/bin/env python import re text = 'Hello, ""find.me-_/\\"" please help with python regex' pattern = r'""([A-Za-z0-9_\./\\-]*)""' m = re.search(pattern, text) print m.group() match and search return None when they fail to match. I guess you are getting AttributeError: 'NoneType' object has no attribute 'group' from python: This is because you are assuming you will match without checking the return from re.match. share|improve this answer| follow |||| answered Jan 31 '12 at 19:26 Douglas LeederDouglas Leeder 46.8k88 gold badges8383 silver badges126126 bronze badges How can I check if they returned null or not? – Abhishek Bhatia May 21 '15 at 22:37 if m is None: doStuffIfNoMatch() else: doStuffIfMatch()` – Douglas Leeder May 22 '15 at 7:09 add a comment |"
271,"
Q: Python - getting text from html ","2 As Joel Cornett mentioned, using BeautifulSoup like this: from bs4 import BeautifulSoup html = '''<div class=""definicja""><a href=""javascript: void(0);"" onclick=""play('/mp3/1/81/c5ebfe33a08f776931d69857169f0442.mp3')"" class=""ikona_sluchaj2""></a> <a href=""/slownik/angielsko_polski/,royal+python"">ROYAL PYTHON</a></div>''' soup = BeautifulSoup(html) print soup.getText() share|improve this answer| follow |||| answered Apr 20 '12 at 21:17 FeniksoFenikso 8,09433 gold badges3838 silver badges6868 bronze badges add a comment |"
272,"
Q: Split text file Python ","1 You can use regular expressions like so: import re #splits on ""Chapter "" + a sequence of numbers + a newline #re.IGNORECASE means it will split on ""CHAPTER"" or ""chapter"" or ""Chapter"" etc split_text = re.split(""Chapter [0-9]+\n"", mytext, flags=re.IGNORECASE) print(split_text) >>> ['', '\nLorem ipsum\n\ndolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt\n\n', '\nconsectetur adipiscing\n\nsed do eiusmod tempor\n\n', '\net dolore magna aliqua.'] You can now choose the text from each chapter by the index of split_text e.g.: print(split_text[2]) >>> consectetur adipiscing sed do eiusmod tempor share|improve this answer| follow |||| edited Jul 21 '18 at 10:53 answered Jul 21 '18 at 10:42 brazofuertebrazofuerte 3,61333 gold badges1010 silver badges3131 bronze badges add a comment |"
273,"
Q: Which text editor for python development? [duplicate] ","0 There is no preferred editor for anything. It's whatever your personal preference is. I personally prefer emacs with python-mode because it makes indenting easier (and indenting is kind of important in python), but anything will work. share|improve this answer| follow |||| answered Nov 25 '10 at 14:42 Graeme PerrowGraeme Perrow 48.7k1919 gold badges7171 silver badges119119 bronze badges I hear a LOT of discussion about indenting issues with Python. Is this really that big of a deal? I use Notepad++ on Win7 and have it set for tab to be 4 spaces. I guess that's the right thing to do? – Random_Person Nov 25 '10 at 14:47 That will work fine as long as you don't need to share code with another developer using a different editor. – Gareth Rees Nov 25 '10 at 14:50 1 4 spaces is the recommended thing to do, as defined in PEP 8. If you edit a file that someone else has written with tabs for indentation, though, adding indentation with spaces can break it. – Thomas K Nov 25 '10 at 14:54 add a comment |"
274,"
Q: Python 3 PDF text extraction [closed] ","9 Pdfminer3k should be exactly that: pdfminer3k is a Python 3 port of pdfminer. PDFMiner is a tool for extracting information from PDF documents https://pypi.python.org/pypi/pdfminer3k share|improve this answer| follow |||| answered Sep 14 '15 at 10:50 Juuso MeriläinenJuuso Meriläinen 48233 silver badges77 bronze badges Thanks a lot for the concise answer, was looking for this for a long time. – Ryan Kung Sep 15 '15 at 12:52 add a comment |"
275,"
Q: Selenium (python) text deletes on its own ","-1 Have you checked if there is any javascript code in the page listening to changes to the input field and deleting its contents? share|improve this answer| follow |||| answered May 4 '17 at 18:59 Pablo R.Pablo R. 3444 bronze badges this is not an answer. – Jayesh Doolani May 4 '17 at 19:41 unfortunately, new users are barred from comments untill they get X points. I am assuming that was the rational. :/ – NanoMage May 5 '17 at 22:48 Yes, I did try to interact with the javacode and that seemed like a promising solution. But I ended up writing a function to make selenium type slower and that solved my problem. I think the field was looking for fake users and ignoring my inputs because they were coming to fast. – dansbot May 20 '17 at 0:10 add a comment |"
276,"
Q: Python text file search and update ","0 The problem is in the line where you write the IDs from the current search into the file. By putting a space between the placeholder and the newline, that space gets written into the text file (i.e. the first line of the text file would be, say, '151921257466 '), meaning that when the contents of the text file are read out, that space is present in the items you're comparing the IDs of the current search to--which means that it does not match the IDs of the current search, regardless whether it should or not (because what you're comparing to has an extra space in it that will make it not match). Change f.write(""%s \n"" % item['id']) to f.write(item['id'] + ""\n"") should make it work on Windows--I don't have a Unix OS available to test it on, so if that doesn't make it work on Unix, I am unfortunately unable to assist. share|improve this answer| follow |||| edited Dec 21 '15 at 1:35 answered Dec 19 '15 at 14:00 train1855train1855 25011 gold badge55 silver badges1313 bronze badges I've edited the post to make it slightly clearer, the input 'items' is actually a list of dicts (each item is a dict) so item['id'] should, and does, return correctly - it just doesn't find it in 'lines' – Testy8 Dec 19 '15 at 23:54 add a comment |"
277,"
Q: Python - Text widget from tkinter in tkk ","1 The solution is to use the tkinter text widget. tkinter and ttk are designed to work together. share|improve this answer| follow |||| answered Jun 20 '17 at 17:40 Bryan OakleyBryan Oakley 264k2828 gold badges362362 silver badges520520 bronze badges I wanted to avoid that, diferents look and feel in the same window is not usual. But if there isn't any other choise... – user6420716 Jun 20 '17 at 17:51 @ViníciusGabriel: what ""look and feel"" do you think a ttk text widget would give you? A text widget is mostly just a big rectangle with text. What do you expect to be different? – Bryan Oakley Jun 20 '17 at 18:18 The same diference between Entry from tkinter and Entry from ttk. – user6420716 Jun 20 '17 at 18:54 @ViníciusGabriel: so, just the border? I don't have a system I can try it out on (linux ttk widgets don't look much different from tkinter widgets), but you can probably get the look you want just by packing a borderless text widget inside a ttk.Entry widget so that you see the entry widget border. – Bryan Oakley Jun 20 '17 at 19:05 add a comment |"
278,"
Q: Sublime Text python error [duplicate] ",0 Add Python to your Windows Path Installing Python share|improve this answer| follow |||| answered Jul 12 '18 at 10:49 Mike_JrMike_Jr 18511 silver badge1515 bronze badges It worked! Thanks! – Shell1500 Jul 12 '18 at 11:01 add a comment |
279,"
Q: Python retrieve variable from text ","2 If the file isn't too big, just use readlines to read the lines into a list, making the last line easy to find. Once you have that line, split it on the comma and then again on spaces to isolate the values. I'm not sure what you mean by ""passing"" them... so here is an example that just extracts the 4 values, converts them to integers and returns them as a tuple. def get_content(): """"""""Returns (numbers, boxes, tickets, files) for the last entry in test.txt"""""" with open('test.txt') as fp: lines = fp.readlines() # just in case there is an empty line at the end... while lines and not lines[-1].startswith('Total Numbers'): del lines[-1] if lines: # split on comma then take the number at the end and return return tuple(int(part.split()[-1]) for part in lines[-1].strip().split(',')) print(get_content()) share|improve this answer| follow |||| answered Jan 12 '16 at 4:49 tdelaneytdelaney 42k44 gold badges4646 silver badges6565 bronze badges add a comment |"
280,"
Q: Newbie Mistakes: Python Text File Reading ","1 I think you mean that your output appears like myuser mypass ... instead of myuser mypass You want to strip the newline characters from your readline()s: myuser = loginsystem.readline().rstrip() However, it is probably better practice to use with so your file handling is properly taken care of. with open(""loginsystem.txt"", ""r"") as loginsystem: ... Additionally, if you want to read all pairs of usernames and passwords, you can do something like: from itertools import izip_longest with open(""loginsystem.txt"", ""r"") as loginsystem: for line1, line2 in izip_longest(*[loginsystem]*2): username = line1.rstrip() password = line2.rstrip() print username, password share|improve this answer| follow |||| answered Feb 21 '14 at 20:39 wflynnywflynny 14k44 gold badges3333 silver badges5050 bronze badges add a comment |"
281,"
Q: Can python send text to the Mac clipboard ","7 New answer: This page suggests: Implementation for All Mac OS X Versions The other Mac module (MacSharedClipboard.py, in Listing 4) implements the clipboard interface on top of two command-line programs called pbcopy (which copies text into the clipboard) and pbpaste (which pastes whatever text is in the clipboard). The prefix ""pb"" stands for ""pasteboard,"" the Mac term for clipboard. Old answer: Apparently so: http://code.activestate.com/recipes/410615/ is a simple script demonstrating how to do it. Edit: Just realised this relies on Carbon, so might not be ideal... depends a bit what you're using it for. share|improve this answer| follow |||| edited Dec 1 '09 at 12:20 answered Dec 1 '09 at 11:58 mavnnmavnn 7,14333 gold badges2626 silver badges4949 bronze badges I never thought of using a command line tool. Perfect, thanks – David Sykes Dec 1 '09 at 14:20 That code sample does not handle Unicode strings correctly. I have provided a Unicode-aware solution as a separate answer. – David Foster Sep 21 '14 at 5:00 add a comment |"
282,"
Q: Aligning Text/Formating Text - Docx - Python ","1 This code Will Run For You from docx import Document from docx.enum.section import WD_SECTION from docx.enum.text import WD_ALIGN_PARAGRAPH document = Document() app_name = ""Company App Here"" consultant = ""Jerry"" document.add_heading (app_name + "" Report"",0) p1=document.add_paragraph(""Testing performed by "" + consultant) p1.alignment=WD_ALIGN_PARAGRAPH.CENTER document.add_page_break() document.add_heading('Executive Summary\n',1) p2=document.add_paragraph() p2.add_run('\tThe blah blah blah text here').bold=True p2.alignment=WD_ALIGN_PARAGRAPH.LEFT document.add_page_break() document.save('./report.docx') This will generate a docx (as per the question's requirement). share|improve this answer| follow |||| edited Jan 1 '18 at 9:47 Pierre.Vriens 1,96477 gold badges2222 silver badges3535 bronze badges answered Jan 1 '18 at 9:09 Sahil JainSahil Jain 18922 silver badges1414 bronze badges it will generate a docx as per the questions requirement -.- – Sahil Jain Jan 1 '18 at 9:39 add a comment |"
283,"
Q: Python replace “” to \"" in text ","4 If what you're trying to generate is JSON-encoded strings, the right way to do that is to use the json module: text = json.dumps(text) If you're trying to generate actual JavaScript source code, that's still almost the right answer. JSON is very close to being a subset of JavaScript—a lot closer than a quick&dirty fix for one error you happen to have noticed so far is going to be. If you actually want to generate correct JS code for any possible string, you have to deal with the corner cases where JSON is not quite a subset of JS. But nobody ever does (it took years before anyone even noticed the difference in the specs). share|improve this answer| follow |||| edited Mar 15 '18 at 20:54 answered Mar 15 '18 at 20:29 abarnertabarnert 285k2828 gold badges432432 silver badges527527 bronze badges add a comment |"
284,"
Q: Python Text File Compare and Concatenate ","0 I'm afraid you are mistaken, your code does not produce the output you say it does. Partly because a file can only be read once, with the exception being if you move the read cursor back to the beginning of the file (file.seek(0), docs). Partly because the second element of a line in the first file ends with a newline character, thus you are comparing e.g. ""abc"" with ""abc\n"" etc. which will never be true. Hence the output file will be completely empty. So how do you solve the problem? Reading a file more than once seems overly complicated, don't do that. I suggest you do something along the lines of: # open all the files simultaneously with open('file1', 'r') as (f1 ), open('file2', 'r') as (f2 ), open('output', 'w') as (outf ): lines_left = True while lines_left: f1_line = f1.readline().rstrip() # check if there's more to read if len(f1_line) != 0: f1_line_tokens = f1_line.split(' ') # no need to strip the line from the second file f2_line_tokens = f2.readline().split(' ') if f1_line_tokens[1] == f2_line_tokens[0]: outf.write(f1_line + ' ' + f2_line_tokens[1]) else: lines_left = False I've tested it on your example input and it produces the correct output (where file1 is the first example file and file2 is the second). If we talk about huge files (millions of lines), this version will be considerably faster than aarons. In other cases the performance difference will be negligible. share|improve this answer| follow |||| edited Oct 10 '17 at 20:04 answered Oct 10 '17 at 19:26 gblomqvistgblomqvist 10211 gold badge11 silver badge1111 bronze badges add a comment |"
285,"
Q: Python TEXT to HEX Byte String ","1 As stated by @usr2564301 there is no direct bytestring conversion in python 2.7 as bytestrings are just ""strings"". Using simple hex strings also works with the LED Controller, so no need to use bytestrings. But the question remains open as this is not a solution... at least for python 2.7. share|improve this answer| follow |||| answered Dec 19 '18 at 12:23 David MartinDavid Martin 4577 bronze badges add a comment |"
286,"
Q: What does Ruby have that Python doesn't, and vice versa? ","34 votes Ruby has the concepts of blocks, which are essentially syntactic sugar around a section of code; they are a way to create closures and pass them to another method which may or may not use the block. A block can be invoked later on through a yield statement. For example, a simple definition of an each method on Array might be something like: class Array def each for i in self yield(i) # If a block has been passed, control will be passed here. end end end Then you can invoke this like so: # Add five to each element. [1, 2, 3, 4].each{ |e| puts e + 5 } > [6, 7, 8, 9] Python has anonymous functions/closures/lambdas, but it doesn't quite have blocks since it's missing some of the useful syntactic sugar. However, there's at least one way to get it in an ad-hoc fashion. See, for example, here. share answered Jul 11 '09 at 12:32 John FeminellaJohn Feminella 264k3737 gold badges319319 silver badges335335 bronze badges 6 @Lennart: apart from your example just beeing horrible it is syntactically wrong, too. – unbeknown Jul 11 '09 at 13:02 2 @unbeknow: A, right. But if that had been a function instead of a print, it would have worked. In python 3 this works: [print(e+5) for e in [1,2,3,4]] And when it comes to horribleness, I think the ruby code above is horrible, so that's clearly subjective and thereby not a part of this question. @John I'm not saying it's equivalent, I'm saying it's not obvious what the difference is from your example. @Bastien, no, but that you can do similar things doesn't mean they are the same. Differences here should be listed even if there are otehr ways to do it. – Lennart Regebro Jul 11 '09 at 13:40 22 I'm a Python Programmer. I would like to see an example of how Ruby blocks help you to write something more concisely or more beautiful than with Python because it has not blocks. Your example could be written: for i in [1, 2, 3, 4]: print(i + 5). It doesn't use blocks, but its concise and beautiful as well as the ruby each example. – Manuel Ceron Jul 11 '09 at 18:56 10 @Manuel, procs are useful for attaching functors to non-trivial data structures (trees, graphs...) which can't be 'for-looped' and hence require special iterators to transverse. Blocks, which are anonymous procs, let you implement the functor in one expression (vs. define then implement) which dramatically speeds up the process of coding and clarifies intent. E.g. if you were creating a graph data structure you could define one 'each' iterator and then mixin Enumerable which would instantly give you access to dozens of iterators (sort, all?, any?, grep). Now you call a block ... – bias Oct 15 '09 at 13:57 4 @RommeDeSerieux, because it needs a name in the language! Moreover, it's a function object, not a function. Let's look at the Ruby Docs: ""Proc objects are blocks of code that have been bound to a set of local variables"" so an anonymous Proc is just the block and it's certainly not just a function! – bias Oct 15 '09 at 14:08 | show 10 more comments"
287,"
Q: Python | change text color in shell [duplicate] ","54 Use Curses or ANSI escape sequences. Before you start spouting escape sequences, you should check that stdout is a tty. You can do this with sys.stdout.isatty(). Here's a function pulled from a project of mine that prints output in red or green, depending on the status, using ANSI escape sequences: def hilite(string, status, bold): attr = [] if status: # green attr.append('32') else: # red attr.append('31') if bold: attr.append('1') return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string) share|improve this answer| follow |||| answered Feb 24 '10 at 22:55 Dietrich EppDietrich Epp 168k2929 gold badges289289 silver badges364364 bronze badges 13 +1 especially for sys.stdout.isatty() – Nifle Feb 24 '10 at 22:59 It's also nice to have an override for the case that the output is not a tty, but you still want the colour - say you are just filtering lines with sed or grep – John La Rooy Feb 24 '10 at 23:02 unbuffer can do that, so you're not stuck if there's no override. – Ignacio Vazquez-Abrams Feb 24 '10 at 23:18 @Ignacio, cool I wonder why debian doesn't have an unbuffer package :( – John La Rooy Feb 24 '10 at 23:26 2 found it - debian hides it in expect-dev under the name expect_unbuffer – John La Rooy Feb 24 '10 at 23:32 | show 2 more comments"
288,"
Q: text mining: read text files in Python ","5 In a language like Python, you'll need to use a for loop to read the contents of each file, one at a time. (Related: How to list all files of a directory in Python) from os import listdir from os.path import isfile, join path = ""C:/txt/Romney"" files = [ f for f in listdir(path) if isfile(join(path,f)) ] for file in files: with open file as f: text = f.read() do_something_with(text) share|improve this answer| follow |||| edited May 23 '17 at 11:56 Community♦ 111 silver badge answered Jun 16 '14 at 20:56 Dave YarwoodDave Yarwood 1,98111 gold badge1111 silver badges2323 bronze badges add a comment |"
289,"
Q: Python kivy text input ","0 There are two reasons why is not working: The if should be in the method that handles the events, i.e. show_popup You should compare the text in the Label, not the Label itself. Instead of self.search=='A', you should use self.search.text=='A' Here is the corrected __init__ and show_popup code: class Diction(GridLayout): def __init__(self, **kwargs): super(Diction, self).__init__(**kwargs) self.cols=2 self.add_widget(Label(text=""Search"")) self.search=TextInput(multiline=False) self.add_widget(self.search) self.add_widget(Button(text=""click"",on_press=self.show_popup)) def show_popup(self, b): if self.search.text=='A': print 'True' else: print 'False' p = CustomPopup() p.open() An alternative approach using the Kivy Language The Kivy Language could help you to have a cleaner code. Your code could look like this: from kivy.app import App from kivy.uix.gridlayout import GridLayout # DON'T forget to import Label!!! from kivy.uix.label import Label from kivy.uix.popup import Popup from kivy.lang import Builder Builder.load_string("""""" <CustomPopup@Popup>: title: ""My Custom Poput"" <Diction@GridLayout>: cols: 2 search: id_search Label: text: ""Search"" TextInput: id: id_search Button: text: ""click"" on_press: root.show_popup(self) """""") class CustomPopup(Popup): pass class Diction(GridLayout): def show_popup(self, b): if self.search.text=='A': print 'True' else: print 'False' # You can send any content to the popup with the content attribute CustomPopup(content=Label(text=self.search.text)).open() class MyApp(App): def build(self): return Diction() It helps to keep the logic separated from the interface. You can even keep in separate files if you use the load_file function instead of the load_string. share|improve this answer| follow |||| edited Nov 9 '13 at 17:30 answered Nov 3 '13 at 19:21 toto_ticototo_tico 11.1k55 gold badges5656 silver badges8080 bronze badges thank you so much, that makes a lot of sense to me, btw what if i want show my translation on popup! am i have to write it like this? CustomPopup(text=""self.search.text"") or in kv language <CustomPopup@Popup>: text:self.search.text ??? – Zorig Nov 9 '13 at 10:40 I am not sure if I understand your question. (1) I can tell you that Popup has no attribute text. Do you mean title? or do you mean a Widget (probably a Label) with the text? (2) The 2nd option is wrong because search doesn't belong to CustomPopup. I ll try to give you another example, but you better ask a different question for it, so more ppl get to read it and reply. – toto_tico Nov 9 '13 at 17:13 I modify the example so you can send content to your CustomPopup. You probably don't want a CustomPopup, it might be more useful to have a ""CustomContent"". The content property of Custom will accept any Widget. – toto_tico Nov 9 '13 at 17:32 Regarding the use of kv language, you can always access any id inside the class scope. For example, you could have done something like on_press: print id_search.text because on_press is inside the of the Diction scope. However you cannot access id_search in CustomPopup because id_search belongs to Diction. Lastly, the search attribute is meant to be used used in Python. Nonetheless, it can be access with root.search (or self.parent.search). – toto_tico Nov 9 '13 at 17:40 Thanks, you get it right, CustomContent is very useful also really new to me. Now i finally found myself again. thanks again – Zorig Nov 11 '13 at 16:48 add a comment |"
290,"
Q: execute python in sublime text ","1 Tools - Build System - New Build System { ""cmd"": [""path_to_your_python"", ""$file""] } than just use this build system. share|improve this answer| follow |||| answered Mar 18 '15 at 23:23 Ben UsmanBen Usman 4,56355 gold badges3636 silver badges5353 bronze badges by which type we should save this build system file – ASEN Mar 18 '15 at 23:45 .sublime-build I guess – Ben Usman Mar 18 '15 at 23:46 add a comment |"
291,"
Q: Python text file read/write optimization ","0 Try running it in Pypy (https://pypy.org), it will run without changes to your code, and probably faster. Also, C++ might be an overkill, especially if you don't know it yet. Consider learning Go or D instead. share|improve this answer| follow |||| answered Jan 9 '18 at 8:47 ErezErez 80066 silver badges1414 bronze badges I'll have to see if I have the right privileges to install this. Interesting though, I didn't know there was an upgrade to CPython. Will check back in this week to confirm. I do agree that this would make it faster though. – Andi Jan 10 '18 at 3:31 You can get Pypy as a portable binary, so no privileges are necessary: github.com/squeaky-pl/… Also, it's not an official upgrade, but it's an incredibly well-made drop-in alternative. – Erez Jan 11 '18 at 9:18 i am going to mark this as complete - i ended up converting this into Cython which reduced the time from 30m to 20m for a 10GB/30MM lines in the file. i will also look to see what the portable version of PyPy does, no doubt it will be fast - did not want to leave the question hanging. Thank you for your help! – Andi Jan 12 '18 at 16:03 add a comment |"
292,"
Q: Parse Text with Python ","1 You can try this: file = open(""filename.txt"", ""r"") # This opens the original file output = open(""result.txt"", ""w"") # This opens a new file to write to seenSpecialStuff = 0 # This will keep track of whether or not the 'SpecialStuff' line has been seen. for line in file: if "";"" in line: seenSpecialStuff = 0 # Set tracker to 0 if it sees a semicolon. if seenSpecialStuff == 1: output.write(line) # Print if tracker is active if ""SpecialStuff"" in line: seenSpecialStuff = 1 # Set tracker to 1 when SpecialStuff is seen This returns a file named result.txt that contains: select numbers ,othernumbers words This code can be improved! Since this is likely a homework assignment, you'll probably want to do more research about how to make this more efficient. Hopefully it can be a useful starting ground for you! Cheers! EDIT If you wanted the code to specifically read the line ""SpecialStuff"" (instead of lines containing ""SpecialStuff""), you could easily change the ""if"" statements to make them more specific: file = open(""my.txt"", ""r"") output = open(""result.txt"", ""w"") seenSpecialStuff = 0 for line in file: if line.replace(""\n"", """") == "";"": seenSpecialStuff = 0 if seenSpecialStuff == 1: output.write(line) if line.replace(""\n"", """") == ""SpecialStuff"": seenSpecialStuff = 1 share|improve this answer| follow |||| edited Jun 20 '17 at 15:13 answered Jun 19 '17 at 18:23 cosinepenguincosinepenguin 1,29211 gold badge88 silver badges1818 bronze badges Thank you, this is really close to what I was looking for. The only problem is that there are some parts of the code that have strings like ""abcSpecialStuffpdq"" and so it's grabbing everything that follows. How could I change the code so it only grabs stuff following the string ""SpecialStuff"" ? – user3476463 Jun 20 '17 at 15:05 You can try making the ""if"" statement something like if line.replace(""\n"", """") == ""SpecialStuff"":, which would make it so that only the line that has exactly SpecialStuff in it would trigger making the tracker ""1""! That can be done for the other lines too, if you want it to only find specific occurrences! – cosinepenguin Jun 20 '17 at 15:11 I edited the answer to reflect that! If you needed to later also grab the information contained in ""abcSpecialStuffpdq"" you would have to add a separate ""if"" statement so that the code would recognize it. – cosinepenguin Jun 20 '17 at 15:17 add a comment |"
293,"
Q: Parsing text file in python ","5 Since this seems to come from ping command, you could use grep like this : grep -oP 'ttl=\d+\s+time=\K[\d\.]+' file Output : 17.2 12.6 11.7 Note : It's very simple to search SO or/and google to use this regex in pure python. Hint : Support of \K in regex Bonus Because I still have to play with python : (in a bash shell) : python2 <<< $'import re\nf = open(""/tmp/file"", ""r"")\nfor textline in f.readlines():\n\tmatches = re.finditer(""ttl=\d+\s+time=([\d\.]+)ms"", textline)\n\tresults = [float(match.group(1).strip()) for match in matches if len(match.group(1).strip())]\n\tif results:\n\t\tprint results[0]\nf.close()\n' share|improve this answer| follow |||| edited May 23 '17 at 12:06 Community♦ 111 silver badge answered Oct 3 '16 at 19:02 Gilles QuenotGilles Quenot 126k2626 gold badges182182 silver badges182182 bronze badges I would prefer having the python equivalent of grep – user192314 Oct 3 '16 at 19:07 3 Now you have the regex, all what you need is to search google a bit to use it – Gilles Quenot Oct 3 '16 at 19:08 I have been googling regex for a while and am getting stuck on its syntax rules – user192314 Oct 3 '16 at 19:32 Added python for fun – Gilles Quenot Oct 3 '16 at 21:11 @user192314 Next time, instead of cross posting, you can ask for clarification for things you might not understand. The answerer here has given a functional answer and a working Python solution with regex. – idjaw Oct 3 '16 at 22:36 | show 2 more comments"
294,"
Q: Run Python Debugger (pdb) in Sublime Text 3 ","5 python breakpoint plugin and check this link python breakpoint debugger Use ctrl+shift+b to toggle breakpoint in a line But its not preferable solution for debugging a software using a text editor. There are best IDE which makes your development lot easier visual studio community edition Pycharm Eclipse Komodo share|improve this answer| follow |||| answered Aug 17 '16 at 8:05 RajaramanRajaraman 17511 silver badge33 bronze badges 3 you've referenced the same sublime tool that I based my question on. – ecoe Aug 20 '16 at 15:52 7 The existence of IDE's is well known, but much as some people still prefer to use VIM, some people also prefer the light weight and still somewhat powerful sublime text. Having opinions isn't especially relevant to his question. – csga5000 Apr 6 '17 at 15:55 add a comment |"
295,"
Q: Python tkinter module text transitioning ","0 import time from tkinter import * tk = Tk() fps=30 increment=2 t = ('FPS: %s; Speed: %s'% (fps,increment)) canvas = Canvas(tk,height='500',width='500') canvas.pack() canvas.create_oval(175,100,325,250,fill='red',outline='black') canvas.create_line(50,100,450,100) canvas.create_line(50,450,450,450) canvas.create_text(250,30,text='Bounce!',fill='red',font=('Consolas',30)) w = canvas.create_text(250,75,text= t,fill='black',font=('Consolas',20)) #mainproblem while True: increment += 1 if increment % 2 == 0 and fps > 1: fps = fps - 1 t = ('FPS: %s; Speed: %s'% (fps,increment)) canvas.itemconfig(w, text= t) for x in range(0, int(200/increment)): canvas.move(1,0,increment) tk.update() time.sleep(1/fps) for x in range(0, int(200/increment)): canvas.move(1,0,-(increment)) tk.update() time.sleep(1/fps) share|improve this answer| follow |||| answered Dec 20 '14 at 16:38 Isuru MadusankaIsuru Madusanka 32622 silver badges1212 bronze badges 2 At least provide a minimum description as to how to fix the problem, so one does not have to compare the entire code, character by character, with the original. – tobias_k Dec 20 '14 at 17:03 add a comment |"
296,"
Q: Text and Button Python Tk GUI ","0 From what I understand you want to reverse the order of insertions? If so, than instead of 1.0 you can use INSERT: from Tkinter import * root = Tk() root.geometry(""300x400"") message_list = [""testing on file 0 successfull\n"", ""testing on file 1 successfull\n"", ""testing on file2 successfull\n""] message_list_idx = 0; def startProgram(): global message_list_idx if message_list_idx >= len(message_list): return t3 = message_list[message_list_idx] text.insert('1.0', t3) message_list_idx += 1 text = Text(root, height=8, width=25) text.grid(row=10, column=0, padx=20, pady=5) label = Label(root, text=""Status"",font=12) label.grid(row=9, column=0, padx=5, pady=5) button2=Button(root,text=""Start Program"", width=20,font=12, command=startProgram) button2.grid(row=7, column=0, padx=20, pady=10) button3=Button(root, text='Exit Program',width=20,font=12, command=root.destroy) button3.grid(row=8, column=0,padx=20, pady=10) root.mainloop() button2=Button(root,text=""Start Program"", width=20,font=12, command=startProgram) button2.grid(row=7, column=0, padx=20, pady=10) button3=Button(root, text='Exit Program',width=20,font=12, command=root.destroy) button3.grid(row=8, column=0,padx=20, pady=10) root.mainloop() share|improve this answer| follow |||| edited Feb 23 '15 at 5:31 answered Feb 23 '15 at 1:23 MarcinMarcin 32.3k44 gold badges5757 silver badges8686 bronze badges Thanks for your replay, actually not reverse the order of insertions, I want to update the text field one by one following the order of text t1, t2, t3. current code display all texts together. Any suggestion? – Patrick Feb 23 '15 at 1:38 So you want to clear the text filed before inserting text, or introduce some delay (e.g. 1s) before new text is shown? – Marcin Feb 23 '15 at 1:59 I want to print any new coming text accordingly in the text field after pressing the button, it is like tracking the procedure of a program. I tried to clear the previous inserted text and show the new coming but the above code just shows the last text content after pressing the button action, in other words, it only shows the last inserted text rather than display one text after another, even you put time delay between two text. – Patrick Feb 23 '15 at 4:05 @HotPat1000 Sorry, I still dont understand. Maybe you could amend your question to provide more info, some example input, screenshot, etc. – Marcin Feb 23 '15 at 4:16 @ Marcin Thanks for your patient. let me rephrase it, what I really want to do is that when I press the button ""Start Program"" in the GUI, the function ""startProgram"" should run and display text one by one, such as display text 1 first, and text 2 second and last is to show text 3. however, the code I wrote above does not do this job, it only displays all the text one time after run over the function ""startProgram"". So my question is that how I can display the text (t1, t2, t3) one by one in order inside the function. You may get it more clear if u can run the code above. THANKS. – Patrick Feb 23 '15 at 4:47 | show 2 more comments"
297,"
Q: Python Requests returning text ","3 The given text is not a json string, but a query string. You can use urllib.parse.parse_qs to get a dictionary or urllib.parse.parse_qsl to get a list of (key, value) pairs from the query string. >>> urllib.parse.parse_qs('status=ok&info=00005') {'status': ['ok'], 'info': ['00005']} >>> urllib.parse.parse_qsl('status=ok&info=00005') [('status', 'ok'), ('info', '00005')] >>> dict(urllib.parse.parse_qsl('status=ok&info=00005')) {'status': 'ok', 'info': '00005'} share|improve this answer| follow |||| answered Jul 1 '17 at 8:35 falsetrufalsetru 282k3939 gold badges529529 silver badges502502 bronze badges add a comment |"
298,"
Q: Python Text Parsing & Splitting ","0 Your problem is that replace function does the replacement for each occurrence of punctuation character. You can iterate through each character in utterance instead and construct a new target string with a proper replacement: def normalize(utterance): # Converting to lowercase & removing multiple white spaces utterance = ' '.join(utterance.lower().split()) # List of punctuations punctuations_list = [',','.','?',':',';','!',')','(','\''] target = utterance[0] for i in range(1, len(utterance) -1): ch = utterance[i] char_before = utterance[i-1] char_after = utterance[i+1] if ch in punctuations_list and not char_before.isdigit() and char_before not in punctuations_list: target += "" "" target += ch if ch in punctuations_list and not char_after.isdigit() and char_after not in punctuations_list: target += "" "" target += utterance[-1] return target share|improve this answer| follow |||| edited Jun 22 '17 at 13:29 answered Jun 22 '17 at 13:18 tarastaras 4,12599 gold badges2828 silver badges3535 bronze badges This is giving the wrong output: 'hank you :? meeting at 1:30p' – Zing_Yang Jun 22 '17 at 13:28 Thanks. Updated it – taras Jun 22 '17 at 13:30 add a comment |"
299,"
Q: How to encode text to base64 in python ","75 Remember to import base64 and that b64encode takes bytes as an argument. import base64 base64.b64encode(bytes('your string', 'utf-8')) share|improve this answer| follow |||| answered Apr 18 '14 at 23:50 Alexander EjbekovAlexander Ejbekov 3,4851717 silver badges1818 bronze badges 4 how do i remove the leading b when printing this? – sukhvir Apr 18 '14 at 23:53 4 You need to decode it from bytes into a string using some encoding -- e.g. ascii or utf8. (b'home'.decode('ascii') for example) – mgilson Apr 18 '14 at 23:54 add a comment |"
300,"
Q: Python text file into dictionary not working ","1 this is my way def read_dict(): file1 = open(""file.txt"", 'r') dict={} # read lines of all lines = file1.readlines() # Process one line at a time. for line in lines: line = line.split() if not line: continue dict[line[0]] = line[1:] file1.close() print(dict) read_dict() or (use with) you don't have to close file def read_dict(): with open(""file.txt"", 'r') as file1: dict={} # read lines of all lines = file1.readlines() # Process one line at a time. for line in lines: line = line.split() if not line: continue dict[line[0]] = line[1:] print(dict) share|improve this answer| follow |||| edited Nov 28 '17 at 16:50 answered Nov 28 '17 at 16:43 chandong83chandong83 7188 bronze badges add a comment |"
301,"
Q: Text Blink(when printing) in Python 2.7 ",0 [5m[31m and [0m are are ANSI escape codes for format and color. You have to enable your terminal to interpret them. share|improve this answer| follow |||| answered May 22 '18 at 13:55 toliveiratoliveira 83377 silver badges2121 bronze badges @TibiGhimbas What terminal emulator are you using? In many terminals blinking is just bright or bold text. – mx0 May 22 '18 at 19:13 add a comment |
302,"
Q: Python text-adventure game not working ","0 First of all, if you are working with python2.7 you need to know that input() if you give to it a number by console, this function cast to int, then you need to check value like if choice == 1 or just change input to raw_input()(this is the best way), also in if choice!='1' and choice!='2': you need to put else: then you avoid a lot of checks or unexpected values. If you are using python3 raw_input is the new input function so, you don't need to change that And, last if you want to use raw_input() or you are using python3 you need to change the random function to random.choice('1','2') because you are comparing a str with a int, here you have your code with this changes for python2.7: import random import time choice=0 def introDisplay(): print('This is the pre-game story.') time.sleep(1) print('It lasts for 5 lines.') time.sleep(1) print('When you can be arsed, fix this.') time.sleep(1) print('Thanks,') time.sleep(1) print('You, from 18/3/17') print() firstChoice() def firstChoice(): time.sleep(2) print('You come across a path, it splits at the end.') time.sleep(1) choice=raw_input('Which path do you take, the left path (1) or the right path (2)? \n') checkChoice(choice) def checkChoice(choice): correct=False if choice=='1' or choice=='2': correct_choice=random.choice(['1','2']) if choice==correct_choice: correct=True print('You chose the correct path') else: print('You dont chose the correct path') else: print('You decide to not take a path, and you die due to random circumstances.') time.sleep(1) print('Take a path next time, or at least take it correctly.') failScreen() def failScreen(): restart=True print('You have failed.') print('Do you want to retry?') restart1=input('Y or y = Yes. N or n = No. \n') if restart1=='y' or restart1=='Y': restart=True if restart1=='n' or restart1=='N': restart=False if restart1!='n' or restart!='N' or restart!='y' or restart!='Y': failScreen() if restart==True: introDisplay() if restart==False: exit() introDisplay() share|improve this answer| follow |||| edited Mar 18 '17 at 16:44 answered Mar 18 '17 at 16:39 JuanMi GabarronJuanMi Gabarron 25122 silver badges99 bronze badges All of this works, however the raw_input caused errors but after changing that to input everything works fine. – MobyCoding Mar 18 '17 at 18:03 add a comment |"
303,"
Q: Python Text Document Translation Comparison ","1 You might try something like this #######Test data #file1.txt = this is a test #file2.txt = this a test #results# #is def translation_comparison(): with open(""file1.txt"", 'r') as f1: f1 = f1.read().split() with open(""file2.txt"", 'r') as f2: f2 = f2.read().split() for word in f1: if word not in f2: print(word) translation_comparison() also it is good practice to use with open(""file1.txt"", 'r') as f1: f1 =f1.read().split() because when using with to open up files it will close the file when you're not using it. Python is pretty good at releasing and managing memory but it is always good habit to make sure you release it or call file1.close() when you are done. share|improve this answer| follow |||| edited Mar 17 '15 at 3:40 answered Mar 17 '15 at 3:09 reticentrootreticentroot 2,81222 gold badges1313 silver badges2626 bronze badges Note, this will work for small files, but the larger the file the longer it will take. for loops in python function at c speed, but that doesn't mean it won't take forever in a large file. This was just for demonstration purpose. – reticentroot Mar 17 '15 at 3:11 Thanks..this is helpful. The only problem is that both examples only return the first instance, rather than all instances, where a word isn't in the other text document. – new recruit 21 Mar 17 '15 at 3:17 Sorry about that, I fixed it. Now it will just print is. – reticentroot Mar 17 '15 at 3:41 One thing I thought about today, the method above work, but it will append all instances of a word not found. The best way to fix that is the convert the list that is created into a set. – reticentroot Mar 18 '15 at 1:49 add a comment |"
304,"
Q: Python file to open a text file and run other python files in the text file ","1 I don't think this post deserves down voting. But from now on I would suggest to OP to look for a solution yourself, and then if you can't find the answer post on stack overflow! from subprocess import call with open(""somefile.txt"", 'r') as f: some_files_to_run = [line.split('\n')[0] for line in f.readlines()] for file_to_run in some_files_to_run: call([""python"", file_to_run]) share|improve this answer| follow |||| answered Feb 4 '13 at 3:32 GregGreg 4,47711 gold badge1919 silver badges3030 bronze badges add a comment |"
305,"
Q: spliting text files using python [duplicate] ","-1 Try this. It open the file for reading as the file pointer fp. It looks at each line, one at a time, removes the newline characters, splits the line into a list on the space character, then converts each element in the list to an integer. In then forms a list of all of the lists of integers and stores it as the variable data. with open('filename.txt', 'r') as fp: data = [list(map(int, line.strip().split(' '))) for line in fp] share|improve this answer| follow |||| edited Jan 27 '17 at 19:35 answered Jan 27 '17 at 19:31 JamesJames 21k33 gold badges2525 silver badges4545 bronze badges 1 Why? What does it do? – Alastair McCormack Jan 27 '17 at 19:33 I added an explanation. – James Jan 27 '17 at 19:35 James, thanks. That was very useful. – j.p Jan 30 '17 at 14:04 add a comment |"
306,"
Q: Undefined variable 'player_damage' - Python text base game ","3 You are here defining a dictionary, so for that you need to pass a key variable that should be string, like: Sample_FIGHT = { ""player_damage"": ""You desperately try to stop the %s for %i damage"", ""enemy_damage"": ""%s gores you for %i damage"", ""player_win"": ""The %s collapses with a thunderous boom"", ""enemy_win"": ""You are squished"" } share|improve this answer| follow |||| answered May 21 '18 at 13:51 rahul mehrarahul mehra 35211 silver badge1010 bronze badges add a comment |"
307,"
Q: Sublime text not building python correctly ","1 It seems python isnt on your path, you need to add it so sublime knows where to eexecutable is See this answer on how to do it on windows How to add to the pythonpath in windows 7? It should be done by default on Mac, hope this helps share|improve this answer| follow |||| edited May 23 '17 at 10:29 Community♦ 111 silver badge answered Nov 23 '16 at 2:32 PadskiizPadskiiz 9011 silver badge1010 bronze badges Thanks! This really helped! – DePianoman Nov 23 '16 at 15:23 add a comment |"
308,"
Q: Why text I/O must be buffered in python 3? ","17 +50 This is an open bug, issue # 17404 (last update 2013-03-13): http://bugs.python.org/issue17404 share|improve this answer| follow |||| answered Sep 13 '16 at 16:28 phzx_munkiphzx_munki 89488 silver badges1313 bronze badges add a comment |"
309,"
Q: Text file in python ","0 with open('a.txt', 'r') as f: data = [] for line in f: data.append(float(line.split()[1])) print(len([i for i in data if i > 0.1])) # prints 2 for the following data: Smith 0.88 Johnson 0.68 Brown 0.04 share|improve this answer| follow |||| answered Feb 18 '19 at 18:25 Osman MamunOsman Mamun 1,95011 gold badge1212 silver badges1515 bronze badges 1 I'm glad it worked! You can accept the answer by clicking the right sign. – Osman Mamun Feb 18 '19 at 19:14 add a comment |"
310,"
Q: How to update HTML text that is displayed in Python Jupyter Notebooks? ","0 This worked for me: from IPython.display import display import ipywidgets as widgets import time out = widgets.HTML() display(out) out.value=""<p>Hi there!</p>"" time.sleep(1) out.value = ""<p>Go away!</p>"" share|improve this answer| follow |||| edited Apr 6 '19 at 15:50 answered Apr 6 '19 at 15:35 LucasBrLucasBr 37511 gold badge44 silver badges1717 bronze badges add a comment |"
311,"
Q: Create Dictionary from Text FIle ","1 Oh shoot it works! No, it shouldn't. It should give you a ""NameError: name 'b' is not defined"" message. If 'a' is set, then 'b' is either unset or left over from the previous interation when food[a] = [b] happens. A more pythonic way to approach this might be: file_name = 'file.dat' food = {} with open(file_name) as file: for line in file: pairs = line.split() food.update(zip(pairs[0::2], pairs[1::2])) PRODUCES {'Shake': '$4', 'Fries': '$3', 'Soda': '$2', 'Burger': '$5'} share|improve this answer| follow |||| answered Oct 31 '16 at 0:40 cdlanecdlane 25.7k44 gold badges1414 silver badges5353 bronze badges Thanks! What does the 'with' statement do? And what does zip do? And pairs? And the ::? Please explain your answer. I am a python noob. – Indigo2003 Oct 31 '16 at 3:23 @Indigo2003, the with statement is an OOP operator that invokes the object's (file's) cleanup code -- i.e. it calls close() on the file when the block is exited so we don't have to remember to do so. pairs is simply the name of my variable and is arbitrary. zip() takes two lists and interlaces them, like a clothes zipper. The [x::y] notation is a slice where pairs[0::2] are the even elements and pairs[1::2] are the odd elements. – cdlane Oct 31 '16 at 8:00 what does the 2 colons mean though? and when i made 2 lists list1 = [1, 2] list2 = ['a', 'b'] print(zip(list1, list2)) It said <zip object at 0x02DE2828> – Indigo2003 Oct 31 '16 at 18:03 add a comment |"
312,"
Q: Python get selected text ","2 Install xsel sudo apt-get install xclip xsel -y Save this as get-selected.py import os print(os.popen('xsel').read()) Select text Run python get-selected.py share|improve this answer| follow |||| edited Apr 29 '18 at 13:08 SomeGuyOnAComputer 2,53522 gold badges2020 silver badges4343 bronze badges answered Feb 13 '16 at 11:31 user3408094user3408094 2122 bronze badges add a comment |"
313,"
Q: Python text processing and parsing ","1 Maybe you should try some AST hacks? With python it is easy, just: import ast config= ast.parse( file('config.py').read() ) So know you have your parsed module. You need to extract assign to animals and evaluate it. There are safe ast.literal_eval function but since we make a call to dict it wont work here. The idea is to traverse whole module tree leaving only assigns and run it localy: class OnlyAssings(ast.NodeTransformer): def generic_visit( self, node ): return None #throw other things away def visit_Module( self, node ): #We need to visit Module and pass it return ast.NodeTransformer.generic_visit( self, node ) def visit_Assign(self, node): if node.targets[0].id == 'animals': # this you may want to change return node #pass it return None # throw away config= OnlyAssings().visit(config) Compile it and run: exec( compile(config,'config.py','exec') ) print animals If animals should be in some dictionary, pass it as a local to exec: data={} exec( compile(config,'config.py','exec'), globals(), data ) print data['animals'] There is much more you can do with ast hacking, like visit all If and For statement or much more. You need to check documentation. share|improve this answer| follow |||| answered Aug 27 '13 at 20:38 ArpegiusArpegius 5,1762626 silver badges4646 bronze badges add a comment |"
314,"
Q: Sublime Text 3 Output Python ","1 I got that thing as well. You can change it by Tools/Build with and choose ""Python"" instead of ""Python- Syntax Check"". It turns out that I clicked ""Syntax Check"" the first time I try to compile. My dumb mistake costs me a few minutes of panic. share|improve this answer| follow |||| answered Feb 23 '16 at 13:57 17571757 1122 bronze badges add a comment |"
315,"
Q: Python code to remove HTML tags from a string [duplicate] ","230 Using a regex Using a regex, you can clean everything inside <> : import re def cleanhtml(raw_html): cleanr = re.compile('<.*?>') cleantext = re.sub(cleanr, '', raw_html) return cleantext Some HTML texts can also contain entities, that are not enclosed in brackets such as '&nsbm'. If that is the case then you might want to write the regex as cleanr = re.compile('<.*?>|&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});') This link contains more details on this. Using BeautifulSoup You could also use BeautifulSoup additional package to find out all the raw text You will need to explicitly set a parser when calling BeautifulSoup I recommend ""lxml"" as mentioned in alternative answers (much more robust than the default one (i.e. available without additional install) 'html.parser' from bs4 import BeautifulSoup cleantext = BeautifulSoup(raw_html, ""lxml"").text But it doesn't prevent you from using external libraries, so I recommend the first solution. share|improve this answer| follow |||| edited Jul 23 '19 at 16:39 smkj33 9911 silver badge1212 bronze badges answered Oct 19 '12 at 21:26 c24bc24b 3,03455 gold badges2020 silver badges2424 bronze badges 10 if you want to compile regexp, best way is compile outside function. In you exemple every call cleanhtml must be compile regexp again – freylis Jun 20 '14 at 2:35 3 BeautifulSoup is good when the markup is heavy, else try to avoid it as it's very slow. – Ethan Jun 12 '15 at 12:48 Great answer. You forgot the colon at the end of def cleanhtml(raw_html) though :) – bjesus Sep 26 '16 at 18:29 FWIW, this will also remove XML another XHTML tags, too. – blacksite Jun 1 '17 at 19:11 2 Nice answer. You might want to explicitly set your parser in BeautifulSoup, using cleantext = BeautifulSoup(raw_html, ""html.parser"").text – Zemogle Dec 6 '17 at 16:32 | show 8 more comments"
316,"
Q: Formatting text file in Python ","0 the code to do so: with open('file_name.txt','r') as file: list_of_lines = file.readlines() new_lines_list = [] for line in list_of_lines: line = line.replace('\n','') #because each line end with this and we don't need it now (\n is the newline chr) new_lines_list.append('{0}|{0}\n'.format(line)) #the same as - new_lines_list.append(line+'|'+line+'\n') with open('file_name.txt','w') as file: string_to_write = ''.join(new_lines_list) file.write(string_to_write) if you don't understand the with statement: it is basically to open the file and at the end it will close itself (and even if some exception occur it will still close (I explain bad if you don't understand go here) share|improve this answer| follow |||| answered Jun 19 '16 at 13:00 Rptk99Rptk99 19422 silver badges1212 bronze badges add a comment |"
317,"
Q: Python text based game indentation ","4 I see it I see it! elif punch == ""2"": # you can't `elif punch` here, because you haven't defined punch (maybe) print""The monster eats your hand whole and you bleed to death. Game Over"" else: # else what? there's nothing here to ""else!"" print""Invalid Input. Game end"" Note that this kind of a game is best implemented using a ""finite state machine"", which may be a bit beyond your abilities at the moment. Just wanted to mention it since you currently have spaghetti code, and I don't want you to think that ""This is how code Should Look,"" it shouldn't. There is, however, no better way to do it at your current knowledge level :) share|improve this answer| follow |||| edited Feb 23 '14 at 22:26 answered Feb 23 '14 at 22:20 Adam SmithAdam Smith 40.1k77 gold badges4949 silver badges8282 bronze badges You're right on, but you messed up the formatting a bit. the print is correctly indented it's the else that is bad. – DeadChex Feb 23 '14 at 22:22 1 @DeadChex actually I missed an indent on everything. It's fixed now :) – Adam Smith Feb 23 '14 at 22:25 Good, just wanted to make sure the indents didn't bite you too – DeadChex Feb 23 '14 at 22:27 add a comment |"
318,"
Q: How to scroll text in Python/Curses subwindow? ","29 OK with window.scroll it was too complicated to move the content of the window. Instead, curses.newpad did it for me. Create a pad: mypad = curses.newpad(40,60) mypad_pos = 0 mypad.refresh(mypad_pos, 0, 5, 5, 10, 60) Then you can scroll by increasing/decreasing mypad_pos depending on the input from window.getch() in cmd: if cmd == curses.KEY_DOWN: mypad_pos += 1 mypad.refresh(mypad_pos, 0, 5, 5, 10, 60) elif cmd == curses.KEY_UP: mypad_pos -= 1 mypad.refresh(mypad_pos, 0, 5, 5, 10, 60) share|improve this answer| follow |||| answered Mar 26 '10 at 11:55 lecodesportiflecodesportif 8,53088 gold badges3030 silver badges5252 bronze badges add a comment |"
319,"
Q: Python: search text file ","2 Given this file: XXXX Testing123 YYYY hellow ZZZZ worldd AAAA Testing456 BBBB heyyy YYYY YoYo You can use a regex on a mmap file: import re import mmap with open(fn, 'r+') as f: mm=mmap.mmap(f.fileno(), 0) for m in re.finditer(r'^(XXXX.*?^\s+YYYY.*?)$', mm, flags=re.M | re.S): print m.group(1) If you only want the YYYY group, split the regex: with open(fn, 'r+') as f: mm=mmap.mmap(f.fileno(), 0) for m in re.finditer(r'^(XXXX.*?)^(\s+YYYY.*?)$', mm, flags=re.M | re.S): print m.group(2) If you want to lose the leading space, use: for m in re.finditer(r'^(XXXX.*?)^\s+(YYYY.*?)$', mm, flags=re.M | re.S): If it is a file that you are comfortable having fully in memory, you can skip mmap and just read the file into memory with f.read() With your comment, you can modify the regex to more accurate capture what you are looking for. Given the file: XXXX Testing123 YYYY hellow ZZZZ worldd AAAA Testing456 BBBB heyyy YYYY YoYo XXXX Testing123 ZZZZ worldd AAAA Testing456 BBBB heyyy YYYY YoYo XXXX Testing123 2 YYYY hellow ZZZZ worldd Demo regex In Python: with open(fn, 'r+') as f: mm=mmap.mmap(f.fileno(), 0) for m in re.finditer(r'^XXXX.*\n^\s+(YYYY.*)', mm, flags=re.M ): print m.group(1) Prints: YYYY hellow YYYY hellow You can also use a two element deque and test each line for the desired condition: from collections import deque with open(fn) as f: d=deque(maxlen=2) d.append(next(f)) for line in f: d.append(line) if d[0].startswith('XXXX') and 'YYYY' in d[1]: print d Prints: deque(['XXXX Testing123\n', ' YYYY hellow\n'], maxlen=2) deque(['XXXX Testing123 2\n', ' YYYY hellow\n'], maxlen=2) share|improve this answer| follow |||| edited Jul 10 '15 at 4:25 answered Jul 7 '15 at 3:38 dawgdawg 69.3k1313 gold badges104104 silver badges173173 bronze badges Yes it gets the full group. Is the m.group(1) an array? Because I need to print out just the YYYY. – aandroidtest Jul 7 '15 at 3:53 Just change the regex into two groups. – dawg Jul 7 '15 at 7:36 Thanks for the solution. But l have one issue. For e.g. YYYY hellow is not found under the XXXX group it continues to search and gives me the second occurence. Any idea how do I prevent that? – aandroidtest Jul 9 '15 at 2:53 What is the definitive grammar of the the occurrence of XXXX followed by a YYYY? Is it only if it is on the following line? – dawg Jul 9 '15 at 4:53 Means YYYY must be in the same list as XXXX. – aandroidtest Jul 9 '15 at 5:19 | show 2 more comments"
320,"
Q: xlwings: set foreground text color from Python ","0 xlwings is currently still a bit light on stylistic features. However, as described here, you can work around like this on Mac by accessing the underlying appscript object directly: some_rgb_vale = (22, 22, 200) xw.Range('A1').xl_range.font_object.color.set(some_rgb_vale) share|improve this answer| follow |||| edited Sep 28 '16 at 12:57 answered Feb 23 '16 at 22:40 Felix ZumsteinFelix Zumstein 4,43811 gold badge1717 silver badges4040 bronze badges Thanks Felix. The link no longer works. This set the foreground colour of A1 to red: Range(""A1"").xl_range.Font.ColorIndex = 3 More info on Excel colours: access-excel.tips/excel-vba-color-code-list, excel-easy.com/vba/examples/font.html – Carl Sep 28 '16 at 10:29 add a comment |"
321,"
Q: Python and Selenium - get text excluding child node's text ","5 You can remove the child node text from the all text all_text = driver.find_element_by_xpath(""//whatever"").text child_text = driver.find_element_by_xpath(""//subchild"").text parent_text = all_text.replace(child_text, '') share|improve this answer| follow |||| answered Jul 21 '17 at 13:22 GuyGuy 32.3k99 gold badges2828 silver badges6161 bronze badges 3 this could cause problems in case they both have the same text – Alon Gouldman Mar 15 '19 at 8:51 add a comment |"
322,"
Q: Is there a way to write formatted text from Python? ","8 Just been playing around with this assuming MS word, I found that you needed to wrap the document in '{}' and define the doctype, then start bold with '\b' and end with '\b0'. An example would be test = 'tester.rtf' out_file = open(test,'w') out_file.write(""""""{\\rtf1 This is \\b Bold \\b0\line\ }"""""") out_file.close() #thanks to the comment below Note the double '\' since python has special meanings for '\b' and '\r'. The full info came from http://www.pindari.com/rtf1.html, which also describes italics, font etc. Let me know if that worked for you. share|improve this answer| follow |||| answered Apr 23 '13 at 6:31 max k.max k. 46933 silver badges88 bronze badges add a comment |"
323,"
Q: Python text wrapping within a string template ","0 You can use PrettyTable which will automatically format the output into columns. from prettytable import PrettyTable x = PrettyTable([""ID"", ""Text"", ""Category""]) for (docId, text, category) in textList: x.add_row([docId, text, category]) print x share|improve this answer| follow |||| answered Feb 18 '16 at 3:32 Walid SaadWalid Saad 68111 gold badge55 silver badges1414 bronze badges add a comment |"
324,"
Q: Python: How to decode uuencoded text ","0 It is just the inverse of the original function.... Let me try and show you. I will use 2 common encoding type uu and base-64. Python 3 from codecs import decode,encode import uu orig=""Cat"" encoding_types=['uu','base-64'] for et in encoding_types: enc_data=encode(orig.encode(),et) un_enc_data=decode(enc_data,et) print(""\n\nEncoding : {}"".format(et)) print(""Orig : {}"".format(orig)) print(""Encoded : {}"".format(enc_data)) print(""byte UnEncoded: {}"".format(un_enc_data)) print(""utf8 UnEncoded: {}"".format(un_enc_data.decode())) This should output Encoding : uu Orig : Cat Encoded : b'begin 666 <data>\n#0V%T\n \nend\n' byte UnEncoded: b'Cat' utf8 UnEncoded: Cat Encoding : base-64 Orig : Cat Encoded : b'Q2F0\n' byte UnEncoded: b'Cat' utf8 UnEncoded: Cat We need the final .decode() as we initially converted the str data object into bytes with the encode(). share|improve this answer| follow |||| answered Jun 24 '18 at 2:25 Tim SeedTim Seed 3,19011 gold badge2222 silver badges2424 bronze badges add a comment |"
325,"
Q: Python Does Not Read Entire Text File ","23 Try: f = open(""filename.txt"", ""rb"") On Windows, rb means open file in binary mode. According to the docs, text mode vs. binary mode only has an impact on end-of-line characters. But (if I remember correctly) I believe opening files in text mode on Windows also does something with EOF (hex 1A). You can also specify the mode when using fileinput: fileinput.input(""filename.txt"", inplace=1, mode=""rb"") share|improve this answer| follow |||| edited Mar 28 '12 at 11:28 answered Mar 28 '12 at 11:09 codeapecodeape 82.8k2222 gold badges130130 silver badges159159 bronze badges That also works! I like that solution the most, because how easy it is to change the existing code. – user1297872 Mar 28 '12 at 11:20 How there ""that also works"" ? This is clearly your problem. What other approach did work as well? Ah, I see in the comments, specifying a byte-lenght to be read, instead of using ""readline"" – jsbueno Mar 28 '12 at 12:27 I faced exactly the same problem. It works perfectly! – Tao Chen Dec 3 '15 at 11:36 add a comment |"
326,"
Q: Print text with glut and python ","3 A lot of OpenGL programmers get caught by this (myself included). Although glRasterPos looks like it takes pixel coordinates, they are actually transformed by the modelview and projection matrices before use. Useful if you're trying to position text in 3D space, not so useful when you want some kind of overlay console or HUD. The old way to get around this was to push both projection and modelview matrices, set both to identity, draw text, pop both. Brian Paul of Mesa3D thought this was silly and added a new call glWindowPos which takes actual pixel coordinates in the window. It became standard in OpenGL 1.4 or so. Replace your glRasterPos with glWindowPos and see if that fixes the problem. share|improve this answer| follow |||| answered Oct 11 '12 at 22:37 HughHugh 1,34599 silver badges33 bronze badges add a comment |"
327,"
Q: Python - Reading a text file into dictionary ","7 You can do: for line in f: listedline = line.strip().split('=') # split around the = sign if len(listedline) > 1: # we have the = sign in there newDict[listedline[0]] = listedline[1] However, what do you want to do with the data stored in this dict? It will store everything as strings so your list will be a big string. If you need more refined data, it's not too hard but you will have to tell us what it is you want to accomplish with this dict. share|improve this answer| follow |||| answered Dec 5 '13 at 22:08 BooBoo 46733 silver badges66 bronze badges Using the data stored in this dictionary from the text-based database file, i'll be prompting the user for input which will then be used to read from the dictionary using the keys and values displayed above from the one example record. In terms of refining the data, I don't want to read the entire text file. I want to read the three keys and their corresponding values into one single dictionary within Python. Is this possible or is there an easier way of doing this? If you need me to elaborate any further, let me know. Thanks. – user3071524 Dec 8 '13 at 16:50 +1 for the simplicity. However stripping key and element may be useful: newDict[listedline[0].strip()] = listedline[1].strip() – hansolo Apr 2 '17 at 17:29 add a comment |"
328,"
Q: python read text file return strange values ","1 The open method in python has no encoding by default. open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) So doing this: with open('test.txt') as f: print(f.readlines()) will result in: ['þÿ\x00a\x00_\x00i\x00d'] To fix that add the proper encoding, as such: with open('test.txt', encoding='utf-16') as f: print(f.readlines()) which will give you: ['a_id'] share|improve this answer| follow |||| answered Mar 24 '16 at 0:46 LebLeb 11.1k55 gold badges4242 silver badges6565 bronze badges add a comment |"
329,"
Q: Simple, random encounters in a python text-adventure.. I´m stuck ","2 the dicts and your functions are really pointless as they are, they need to actual return something so you can randomly pick a pair: from random import choice # use to pick a random element from encounter_choice def enemy_bandit01(): return 'human', 'a sword' # just return a tuple def enemy_orc01(): return 'orc', 'a club' def enemy_wolf01(): return 'wolf', 'claws' encounter_choice = [enemy_bandit01, enemy_orc01, enemy_wolf01] # use str.format and unpack the tuple of race, weapon print('You fight against a {}. He has {}!'.format(*choice(encounter_choice)())) which may as well just become picking a random tuple from a list: from random import choice encounter_choice = [('human', 'a sword'), ( 'orc', 'a club'), ('wolf', 'claws') ] print('You fight against a {}. He has {}!'.format(*choice(encounter_choice))) *choice(encounter_choice) is equivalent to doing: race, weapon = choice(encounter_choice) print('You fight against a {}. He has {}!'.format(race, weapon)) share|improve this answer| follow |||| answered Sep 22 '16 at 19:41 Padraic CunninghamPadraic Cunningham 148k1717 gold badges164164 silver badges243243 bronze badges Thanks, that really helped me out! :) – BobJohn Sep 23 '16 at 8:42 add a comment |"
330,"
Q: Text Classification Using Python ","2 The error is due to this line: prediksi = klasifikasi.predict([test_vector]) Most scikit estimators require an array of shape [n_samples, n_features]. The test_vector output from TfidfVectorizer is already in that shape ready to use for estimators. You don't need to wrap it in square brackets ([ and ]). The wrapping makes it a list which is unsuitable. Try using it like this: prediksi = klasifikasi.predict(test_vector) But even then you will gt error. Because of this line: test_vector = vectorizer.fit_transform(test_text) Here you are fitting the vectorizer in a different way than what was learned by the klasifikasi estimator. fit_transform() is just a shortcut for calling fit() (learning the data) and then transform() it. For test data, always use transform() method, never fit() or fit_transform() So the correct code will be: test_vector = vectorizer.transform(test_text) prediksi = klasifikasi.predict(test_vector) #Output: array(['organisasi'], dtype='|S10') share|improve this answer| follow |||| answered Jun 14 '17 at 8:08 Vivek KumarVivek Kumar 23k66 gold badges4747 silver badges7777 bronze badges add a comment |"
331,"
Q: Hebrew Text in Python ","1 If your source text does indeed include a Hebrew text, the code-page-based character representation translated into universal UNICODE will allow you to construct the language corpus analytics, the presentation of the <<word>> content is not related to the content per-se, but depends on the UI-environment. As an example here, it is possible to both write and show Hebrew text, as the widget can properly handle the proper contexts ( visual representation of char(s), order of appearance, direction of flow ): aSequenceOfCHARs( ALEF, BET, MEM, NUN, AYIN, FINAL PE ) is displayed as: אבמנעף So your main focus -- the language corpus analytics in python -- may work with unicode text-elements independently of their output onto a presentation layer. share|improve this answer| follow |||| edited Oct 7 '14 at 5:33 answered Oct 7 '14 at 5:22 user3666197user3666197 24.7k44 gold badges3636 silver badges7272 bronze badges add a comment |"
332,"
Q: find article text Regex Python ","1 Maybe you could add: (?:\n^[ ]{5}[A-Za-zА-Яа-я0-9_\-:,\.«» ]+)* After the second capture group, as in: ^([A-Za-zА-Яа-я0-9_\-:,\.«» ]+)\n{2}^[ ]{5}([A-Za-zА-Яа-я0-9_\-:,\.«» ]+(?:\n^[ ]{5}[A-Za-zА-Яа-я0-9_\-:,\.«» ]+)*)$ regex101 demo share|improve this answer| follow |||| answered Oct 10 '13 at 21:12 JerryJerry 64.1k1212 gold badges8383 silver badges119119 bronze badges Thanks, its works. As I understand this non-captured group match only last row of article and this extend diapason of group. – Dmitry Oct 10 '13 at 22:14 1 @Dimm It matches each other row following the first article row. The way it works is that it tries to find the 5 spaces indent and if there's no match, it stops there. That's how it can match the last row of the article :) – Jerry Oct 11 '13 at 9:10 add a comment |"
333,"
Q: python - edit a text file ","2 I guess that you need to open the file, seek to the end, delete characters and save it. seek ( http://docs.python.org/library/stdtypes.html#file.seek ) accepts negative values (e.g. f.seek(-3, os.SEEK_END) sets the position to the third to last), so that you can easily go to the end of your file. http://docs.python.org/library/stdtypes.html#file-objects - this link may be a good starting point. share|improve this answer| follow |||| answered Sep 7 '10 at 9:22 dmitkodmitko 2,45922 gold badges1717 silver badges1515 bronze badges add a comment |"
334,"
Q: using Python for deleting a specific line in a file ","192 First, open the file and get all your lines from the file. Then reopen the file in write mode and write your lines back, except for the line you want to delete: with open(""yourfile.txt"", ""r"") as f: lines = f.readlines() with open(""yourfile.txt"", ""w"") as f: for line in lines: if line.strip(""\n"") != ""nickname_to_delete"": f.write(line) You need to strip(""\n"") the newline character in the comparison because if your file doesn't end with a newline character the very last line won't either. share|improve this answer| follow |||| edited Mar 22 '19 at 15:35 Boris 3,47744 gold badges3030 silver badges4141 bronze badges answered Jan 17 '11 at 4:44 houbysofthoubysoft 27.1k2020 gold badges8888 silver badges145145 bronze badges 2 why do we have to open and close it twice? – Ooker Jun 25 '14 at 13:48 3 @Ooker: You have to open the file twice (and close it in between) because in the first mode it is ""read-only"" because you are just reading in the current lines in the file. You then close it and re-open it in ""write mode"", where the file is writable and you replace the contents of the file sans the line you wanted to remove. – Devin Jul 1 '14 at 16:19 4 Why does Python not allow us to do this in one line? – Ooker Jul 1 '14 at 16:37 5 @Ooker, When you read a line, try to imagine a cursor moving along the line as it's read. Once that line has been read the cursor is now past it. When you try to write into the file you write where the cursor currently is. By re-opening the file you reset the cursor. – Waddas Aug 6 '14 at 20:48 4 Use the with compound! – Sceluswe Sep 20 '16 at 15:37 | show 3 more comments"
335,"
Q: Python accessing text file [closed] ","1 Using with open(...) as ... is preferred to open files (since they're then closed automatically). With the file opened, you're going to want to loop through the lines in your file using a for loop. Then separate the words in each line, and strip the white space from the words. Finally, you could use use % formatting to print the sentence you want for each line in the file. with open('personalnumbers.txt', 'r') as file_object: for line in file_object: number, name = [word.strip() for word in line.split(',')] print 'The personalnumber of %s is %s'%(name, number) share|improve this answer| follow |||| edited Sep 1 '17 at 16:56 stybl 8,10233 gold badges2121 silver badges4545 bronze badges answered Sep 1 '17 at 16:50 LuceLuce 6311 silver badge77 bronze badges add a comment |"
336,"
Q: Strange python type “Text”? ","9 You are looking at a subclass of unicode most likely. I think you have an instance of suds.sax.text.Text() here. Since this is a subclass, isinstance(obj, unicode) is True. It's value is already a unicode string, so decoding will trigger an implicit encode first with ASCII, which will indeed fail. You can convert the type to unicode by simply using: unicode(obj[cur_key]['value']) but take into account the value can still be XML escaped; the .unescape() method returns an unescaped version (it'll return self if the value wasn't escaped to begin with). When comparing with another string, make sure they are exactly equal. Unicode values can contain many 'hidden' values, like zero-width characters or combining characters that can be represented in combined form as well. Use repr(value) to get an escaped representation that makes such codepoints more obvious. The suds.sax.text.Text() class doesn't special-case equality testing, so your != test would work just the same as if the value was a unicode string instead of the subclass. share|improve this answer| follow |||| edited May 22 '14 at 10:27 answered May 22 '14 at 10:17 Martijn Pieters♦Martijn Pieters 807k196196 gold badges30223022 silver badges26652665 bronze badges add a comment |"
337,"
Q: How do I create a text file that contain a list of results in python ","1 You just need to open a file to write the prints: def menu(): target = 85 results = open(""results.txt"",'w') with open('homework.txt','r') as a_file: for l in a_file: name, number = l.split(',') number = int(number) results.write(name + ': ' + ('passed' if number>=target else str(target - number)) + '\n') input() share|improve this answer| follow |||| answered Jan 3 '17 at 17:53 Don CharlieDon Charlie 6155 bronze badges add a comment |"
338,"
Q: Get text from a program in Python ","1 No, you can't, not directly. There are various accessibility, etc. APIs you could use to try and ""read"" the user interface of another program, but that's certainly a lot more involved than just a simple get_text() style call. (And for Windows Notepad, you can enumerate the Notepad main window's child windows, find the edit/rich-text control it's using and send a WM_GETTEXT message (if my memory serves) and hope it sends you some of the current text back...) share|improve this answer| follow |||| answered Mar 25 at 12:57 AKXAKX 64.1k66 gold badges6666 silver badges8080 bronze badges add a comment |"
339,"
Q: Reading a text file in python [closed] ","0 Here is an example of reading and writing a text file below. I believe that you are looking for the ""open"" method. with open('New_Sample.txt', 'w') as f_object: f_object.write('Python sample!\n') f_object.write('Another line!\n') You simple open the file that you want and enter that file name as the first argument, and then you use 'w' as the second argument to write the file. If you Python file and text files are in different directories, you may have to also use the relative or absolute path to open the file. Since my Python file and text file are both in ""Downloads"" I do not have to do that in this scenario. Here is the code below to read in a text file: with open('New_Sample.txt') as f_object: for line in f_object: print(line.rstrip()) And here is the output: Python sample! Another line! You simply use the open method again and you can print the files in the .txt file line for line in a loop. I use rstrip() in this case though because you will have some white space when you attempt to print the lines. share|improve this answer| follow |||| answered May 17 '18 at 19:51 Simeon IkudaboSimeon Ikudabo 1,71111 gold badge44 silver badges1616 bronze badges add a comment |"
340,"
Q: python argparse print usage text after description ","5 The argparse module does not provide any option to add a ""prolog"". When the help is displayed it always start with usage:. The best you can do is to customize the usage text adding the version number, using the usage parameter when you instantiate the ArgumentParser: import argparse parser = argparse.ArgumentParser(usage='Any text you want\n') Note that the help will still start with usage:. A dirty workaround that might work is to start the usage message with a \r: >>> import argparse >>> usage = '\r{}\nusage: %(prog)s etc.'.format('Version a b'.ljust(len('usage:'))) >>> parser = argparse.ArgumentParser(usage=usage) >>> parser.parse_args(['-h']) Version a b usage: etc. optional arguments: -h, --help show this help message and exit I don't think that this usage of \r is portable. There are probably some terminals where this trick doesn't work. I've ljusted the version string to make sure that when the trick works, the whole usage: string disappears from string and you don't get things like v1.2e: when using short version strings. Note: you must manually create the whole usage text now. share|improve this answer| follow |||| edited Mar 13 '14 at 15:18 answered Mar 13 '14 at 15:10 BakuriuBakuriu 76.7k1515 gold badges160160 silver badges186186 bronze badges add a comment |"
341,"
Q: Watson Speech to Text Python ","0 Amazon Polly is a service that turns text into lifelike speech. Amazon Polly enables existing applications to speak as a first class feature and creates the opportunity for entirely new categories of speech-enabled products, from mobile apps and cars, to devices and appliances.Amazon Polly includes dozens of lifelike voices and support for multiple languages, so you can select the ideal voice and distribute your speech-enabled applications in many geographies. Amazon Polly is easy to use – you just send the text you want converted into speech to the Amazon Polly API, and Amazon Polly immediately returns the audio stream to your application so you can play it directly or store it in a standard audio file format, such as MP3. Amazon Polly supports Speech Synthesis Markup Language (SSML) tags like prosody so you can adjust the speech rate, pitch, or volume. Amazon Polly is a secure service that delivers all of these benefits at high scale and at low latency. You can cache and replay Amazon Polly’s generated speech at no additional cost. Amazon Polly lets you convert 5M characters per month for free during the first year, upon sign-up. Amazon Polly’s pay-as-you-go pricing, low cost per request, and lack of restrictions on storage and reuse of voice output make it a cost-effective way to enable speech synthesis everywhere. Here is link share|improve this answer| follow |||| answered Mar 14 '18 at 6:49 Abhijit JagtapAbhijit Jagtap 2,02222 gold badges2424 silver badges3636 bronze badges Hi I appreciate the help however the what I wanted to do is to get the user recorded audio sent via messenger and convert it to text so that my watson conversation will be able to respond. – John Michael Vallejo Mar 14 '18 at 7:10 Instead of Watson, you can use lex. – Abhijit Jagtap Jul 25 '18 at 8:49 add a comment |"
342,"
Q: Python filtering text ","1 This will probably work: lines_you_want = [] with open (""test.txt"",""r"") as myResults: lines = myResults.readlines() indexes_of_lines_you_want = [] # We create a list for the indexes of the lines you want to extract for i in range(len(lines)): if '*******' in lines[i]: # We check if the current line is a line full of stars indexes_of_lines_you_want.extend([i+2, i+3]) # We add the indexes current_index+2 and current_index+3 to our list for i in indexes_of_lines_you_want: lines_you_want.append(lines[i]) after that you can save the lines from the list lines_you_want to a .csv file like this import csv myfile = open('result.csv', 'w', newline='') writer = csv.writer(myfile) writer.writerow(lines_you_want) though you should probably put import csv in the beginning. share|improve this answer| follow |||| edited Sep 21 '15 at 23:15 answered Sep 21 '15 at 23:01 Ilya PeterovIlya Peterov 1,46599 silver badges2222 bronze badges add a comment |"
343,"
Q: Python write text to .tar.gz ","4 Yes, this is possible, but most likely not in the way you'd like to use it. .tar.gz is actually two things in one: gz or gzip is being used for compression, but this tool can only compress single files, so if you want to zip multiple files to a compressed archive, you would need to join these files first. This is what tar does, it takes multiple files and joins them to an archive. If you have a single long logfile, just gziping it would be easier. For this, Python has the gzip module, you can write directly into the compressed file: import gzip with gzip.open('logfile.gz', 'a') as log: # Needs to be a bytestring in Python 3 log.write(b""I'm a log message.\n"") If you definitely need to write into a tar-archive, you can use Python's tarfile module. However, this module does not support appending to a file (mode 'a'), therefore a tarfile might not be the best solution for logging. share|improve this answer| follow |||| answered Jul 6 '15 at 8:25 FinwoodFinwood 3,14411 gold badge1212 silver badges3434 bronze badges add a comment |"
344,"
Q: PYTHON: How can I display dot points in python text ",1 you can use Unicode for that. u'•' == u'\u2022 Please check this Click here share|improve this answer| follow |||| answered Oct 14 '18 at 8:56 Hi Im Naru7oHi Im Naru7o 721111 bronze badges add a comment |
345,"
Q: Censoring/masking the text python ","0 Create custom function with replace and pass it to list comprehension or to apply method with re.sub and words boundaries: import re df = pd.DataFrame({'ID':1, 'Note_Text':[ 'I served Mr. Mutiso, a ginger. Me, Julia. Linger'], 'Name_List':[['Mutiso','Julia','Linger']]}) def censor(text,L): for x in L: text = re.sub(r""\b{}\b"".format(x), x[0] + (len(x)-1)*'*',text) return text df['Censored_text1'] = [censor(a, b) for a,b in zip(df['Note_Text'], df['Name_List'])] df['Censored_text2'] = df.apply(lambda x: censor(x['Note_Text'], x['Name_List']), axis=1) print (df) ID Note_Text \ 0 1 I served Mr. Mutiso, a ginger. Me, Julia. Linger Name_List Censored_text1 \ 0 [Mutiso, Julia, Linger] I served Mr. M*****, a ginger. Me, J****. L***** Censored_text2 0 I served Mr. M*****, a ginger. Me, J****. L***** share|improve this answer| follow |||| edited Jan 21 at 12:41 answered Jan 21 at 11:46 jezraeljezrael 498k4040 gold badges595595 silver badges619619 bronze badges add a comment |"
346,"
Q: text file to python array [duplicate] ","1 I would use np.save() which will save it as a binary file and use np.load() to get it back. share|improve this answer| follow |||| answered Oct 13 '17 at 14:09 questquest 1,47811 gold badge88 silver badges1414 bronze badges add a comment |"
347,"
Q: Python reading \n on the end of a line ","0 All your lines have the newline; you need to remove it first before processing the line: value = line.rstrip('\n').split("","") Python doesn't remove it for you. The str.rstrip() method used here will remove any number of \n newline characters from the end of the line; there will never be more than one. You could also extend this to any whitespace, on both ends of the string, by using str.strip() with no arguments. You already start with strings, so there is no need to use str() calls here. If your lines are comma-separated, you could just use the csv module and have it take care of line endings: import csv irTable = {} with open(""devices.txt"", newline='') as file: for label, freq, state in csv.reader(file, skipinitialspace=True): irTable[label] = int(freq), state Demo: >>> from io import StringIO >>> import csv >>> demofile = StringIO('''\ ... lamp, 000000, False ... tv, 000000, False ... bedside, 000000, False ... pc, 000000, False ... bed tv, 000000, False ... ''') >>> irTable = {} >>> for label, freq, state in csv.reader(demofile, skipinitialspace=True): ... irTable[label] = int(freq), state ... >>> irTable {'lamp': (0, 'False'), 'tv': (0, 'False'), 'bedside': (0, 'False'), 'bed tv': (0, 'False'), 'pc': (0, 'False')} share|improve this answer| follow |||| edited Jan 3 '15 at 18:52 answered Jan 3 '15 at 18:46 Martijn Pieters♦Martijn Pieters 807k196196 gold badges30223022 silver badges26652665 bronze badges add a comment |"
348,"
Q: How to Passively Read Text File [Python] ","0 Not sure why I totally didn't think of it, but I ended up just making a copy of the file that I called temp. It's not the cleanest approach, but it works perfectly. I just put shutil.copyfile(""/mnt/win/text.txt"",""/mnt/win/temp.txt"") before the text = open(...) during the loop, so the original text file seems to be left mostly untouched by the program. Such a simple solution I kind of feel ridiculous for asking share|improve this answer| follow |||| answered Aug 29 '13 at 21:03 AsaajAsaaj 24233 silver badges99 bronze badges So what magic does shutil.copyfile employ that keeps it from asserting the file lock? Whatever it is, maybe you could do the same thing in your original program. – Robᵩ Aug 29 '13 at 21:05 I'm not sure. To be honest, it probably still locks the file. But in using it a number of times, I haven't run in to a problem. My guess is that it's such a short lock it doesn't matter. I would have to do more research though – Asaaj Aug 29 '13 at 21:09 add a comment |"
349,"
Q: Splitting words in running text using Python? ","5 Assuming your definition of ""word"" agrees with that of the regular expression module (re), that is, letters, digits and underscores, it's easy: import re fullwords = re.findall(r'\w+', thetext) where thetext is the string in question (e.g., coming from an f.read() of a file object f open for reading, if that's where you get your text from). If you define words differently (e.g. you want to include apostrophes so for example ""it's"" will be considered ""one word""), it isn't much harder -- just use as the first argument of findall the appropriate pattern, e.g. r""[\w']+"" for the apostrophe case. If you need to be very, very sophisticated (e.g., deal with languages that use no breaks between words), then the problem suddenly becomes much harder and you'll need some third-party package like nltk. share|improve this answer| follow |||| answered Jul 3 '10 at 17:27 Alex MartelliAlex Martelli 699k140140 gold badges11041104 silver badges13081308 bronze badges Thanks! This really helped me solve the issue. I had a look at nltk but unfortunately due to the C library installation dependency, I cannot use it with Google App Engine. – demos Jul 3 '10 at 17:45 add a comment |"
350,"
Q: draw hebrew text to image (python,PIL) [duplicate] ",3 Sounds like PIL was built without FreeType support. Install the FreeType dev files and rebuild PIL again. share|improve this answer| follow |||| answered Feb 2 '10 at 8:56 Ignacio Vazquez-AbramsIgnacio Vazquez-Abrams 645k118118 gold badges11581158 silver badges12341234 bronze badges add a comment |
351,"
Q: python-docx get Header text ","2 At the time you asked your question, this wasn't possible using the python-docx library. In the 0.8.8 release (January 7, 2019), header/footer support was added. In a Word document, each section has a header. There's a lot of potential wrinkles to headers (e.g. they can be linked from section to section or different on even/odd pages), but in the simple case, with one section and a non-complicated header, you just need to go through the paragraphs in the section header. from docx import Document document = Document(path_and_filename) section = document.sections[0] header = section.header for paragraph in header.paragraphs: print(paragraph.text) # or whatever you have in mind I'm working with a document that has the header laid out with a table instead of simple text. In that case, you'd need to work with the rows in header.tables[0] instead of the paragraphs. share|improve this answer| follow |||| answered Jan 20 '19 at 21:19 Matt SchoutenMatt Schouten 11133 bronze badges add a comment |"
352,"
Q: Text preprocessing in Python ","0 No package necessary in R (nor in Python if I'm not mistaken). First get everything split up (and remove that initial 5). I'm guessing you want the result as numbers, not strings: x<-""5 0:10 8:1 18:2 54:1 442:2 3784:1 5640:1 43501:1"" y<-as.integer(unlist(strsplit(x,split="" |:""))[-1]) feature<-y[seq(1,length(y),by=2)] [1] 0 8 18 54 442 3784 5640 43501 value<-y[seq(2,length(y),by=2)] [1] 10 1 2 1 2 1 1 1 If you want them side-by-side: cbind(feature,value) feature value [1,] 0 10 [2,] 8 1 [3,] 18 2 [4,] 54 1 [5,] 442 2 [6,] 3784 1 [7,] 5640 1 [8,] 43501 1 If you want to assign them to a data.table for analysis: library(data.table) dt<-data.table(feature=feature,value=value) > dt feature value 1: 0 10 2: 8 1 3: 18 2 4: 54 1 5: 442 2 6: 3784 1 7: 5640 1 8: 43501 1 Etc. share|improve this answer| follow |||| answered Jul 17 '15 at 14:57 MichaelChiricoMichaelChirico 27.1k1111 gold badges8282 silver badges137137 bronze badges add a comment |"
353,"
Q: Remove a text box, python ","0 You would need to use the MS Word API http://www.blog.pythonlibrary.org/2010/07/16/python-and-microsoft-office-using-pywin32/ share|improve this answer| follow |||| answered Mar 28 '12 at 3:34 BilljkBilljk 8,4011919 gold badges4747 silver badges6868 bronze badges add a comment |"
354,"
Q: TEXT compression in python ","4 Sure you can do this: import base64 import zlib compressed = 'eJwdktkNgDAMQxfqR+5j/8V4QUJQUttx3Nrzl0+f+uunPPpm+Tf3Z/tKX1DM5bXP+wUFA777bCob4HMRfUk14QwfDYPrrA5gcuQB49lQQxdZpdr+1oN2bEA3pW5Nf8NGOFsR19NBszyX7G2raQpkVUEBdbTLuwSRlcDCYiW7GeBaRYJrgImrM3lmI/WsIxFXNd+aszXoRXuZ1PnZRdwKJeqYYYKq6y1++PXOYdgM0TlZcymCOdKqR7HYmYPiRslDr2Sn6C0Wgw+a6MakM2VnBk6HwU6uWqDRz+p6wtKTCg2WsfdKJwfJlHNaFT4+Q7PGfR9hyWK3p3464nhFwpOd7kdvjmz1jpWcxmbG/FJUXdMZgrpzs+jxC11twrBo3TaNgvsf8oqIYwT4r9XkPnNC1XcP7qD5cW7UHSJZ3my5qba+ozncl5kz8gGEEYOQ' data = zlib.decompress(base64.b64decode(compressed)) Note that this is only 139 characters shorter. But it works: >>> data '2,3,5,1,13,7,17,11,89,1,233,29,61,47,1597,19,37,41,421,199,28657,23,3001,521,53,281,514229,31,557,2207,19801,3571,141961,107,73,9349,135721,2161,2789,211,433494437,43,109441,139,2971215073,1103,97,101,6376021,90481,953,5779,661,14503,797,59,353,2521,4513,3010349,35239681,1087,14736206161,9901,269,67,137,71,6673,103681,9375829,54018521,230686501,29134601,988681,79,157,1601,2269,370248451,99194853094755497,83,9521,6709,173,263,1069,181,741469,4969,4531100550901,6643838879,761,769,193,599786069,197,401,743519377,919,519121,103,8288823481,119218851371,1247833,11128427,827728777,331,1459000305513721,10745088481,677,229,1381,347,29717,709,159512939815855788121,' If your code limit really is so short, maybe you are supposed to calculate this data or something? What is it? share|improve this answer| follow |||| edited Jan 30 '11 at 20:39 answered Jan 30 '11 at 20:31 Lennart RegebroLennart Regebro 133k3636 gold badges197197 silver badges235235 bronze badges And how did you get the compressed value programatically ? :) – Quixotic Jan 30 '11 at 20:35 I did the same thing, but in reverse. – Lennart Regebro Jan 30 '11 at 20:40 Okay let me try to rephrase :) I want to know how you obtain the compressed value in that format ? since something like this ideone.com/EDftR is not giving me that value. – Quixotic Jan 30 '11 at 20:44 Yeah, I said reverse. You obviously have to reverse the order of the actions, ie base64.b64encode(zlib.compress(s)) – Lennart Regebro Jan 30 '11 at 21:08 I have up-voted this one :) Now I understand both of the solutions :) – Quixotic Jan 30 '11 at 21:37 add a comment |"
355,"
Q: take values from text file , python ","0 The pyparsing module may be of specific interest to you as well, https://pyparsing.wikispaces.com/ This can be used to parse string literals into an equation (things like * / ^ - +) without the use of eval() particularily important if you are on linux, insecure. This way, you could use ConfigParser to read in a section of variables, and the equations that go with, and evaluate them accordingly. share|improve this answer| follow |||| answered Feb 20 '15 at 15:11 Justin TokarchukJustin Tokarchuk 1741111 bronze badges Pyparsing is no longer hosted on wikispaces.com. Go to github.com/pyparsing/pyparsing – PaulMcG Aug 27 '18 at 12:59 add a comment |"
356,"
Q: Python text file strings into columns in Excel file ","0 Question: I'd like to extract these strings into excel file in the following format | A | B | C --------------------------------------------------- 1 | melted | swimming | liquified Three words in one line, but according to your Questions Data, you have 6 words in one text line. Can't reproduce your Output! 1 | melted, swimming, liquified, liquidity, liquidness, liquid This is working for me: one_line_of_data = ""melted, swimming, liquified, liquidity, liquidness, liquid\n"" for line in f: liw = [word.strip() for x,word in enumerate(line.split("",""))] data.append(liw[:3]) data.append(liw[3:]) for row_index in range(len(data)): for col_index in range(len(data[row_index])): sheet.write(row_index, col_index, data[row_index][col_index]) Tested with Python:3.4.2 share|improve this answer| follow |||| answered May 7 '17 at 14:30 stovflstovfl 11.9k66 gold badges1616 silver badges3838 bronze badges add a comment |"
357,"
Q: Format text file with python script ","1 reading the file, use str.strip to strip of the line and write to new file at the same time. strip will strip of tab or space or newline from both left and right side of the line with open('input.txt', 'r') as f, open('output.txt', 'w') as fo: for line in f: fo.write(line.strip()) # fo.write(line.strip() + '\n') # use this if wanna retain new line share|improve this answer| follow |||| answered Dec 6 '16 at 3:38 SkyccSkycc 2,76011 gold badge55 silver badges1515 bronze badges add a comment |"
358,"
Q: Underline html text in python [closed] ","1 Since underlining in HTML can be applied with text-decoration:underline then just put your text between tags by using format() or newer f-strings: >>> a = 'I WANT TO SHOW THIS TEXT UNDERLINED' >>> b = '<span style=""text-decoration:underline"">{}</span>'.format(a) >>>print(b) <span style=""text-decoration:underline"">I WANT TO SHOW THIS TEXT UNDERLINED</span> >>> Or even better you could use an external css script: styles.css: h3 { text-decoration:underline; } And just insert it between the <head></head> tags. share|improve this answer| follow |||| edited Jun 8 '18 at 20:21 answered Jun 8 '18 at 20:15 SimonSimon 7,36977 gold badges3636 silver badges5656 bronze badges 1 Thanks Simon!!!! your answer helped me to figure this out. I did this by using html.H3(children = 'I WANT TO SHOW THIS TEXT UNDERLINED', style ={ 'text-decoration':'underline'}) – marcovc Jun 8 '18 at 20:36 add a comment |"
359,"
Q: combine text after : by python ","3 import re one_line = ''.join(menu).replace('\n', ' ') print re.sub(', ([a-z]+:)', r'\n\1', one_line)[:-1] You might have to tweak the one_line to match your input better. share|improve this answer| follow |||| edited Sep 23 '15 at 8:52 answered Sep 23 '15 at 8:33 SorinSorin 10.2k1515 silver badges2222 bronze badges it print in one line – ArrayNo1 Sep 23 '15 at 9:30 it seems print in one line, and have not remove ',' when i am running in InteljIDEA – ArrayNo1 Sep 23 '15 at 10:01 @ArrayNo1 new line is system specific. Try replacing \n in the last line with either \r or \n\r or os.linesep + r'\1' (you'll need to import os for the last one). – Sorin Sep 23 '15 at 10:02 I got an answer it is working: one_line = ''.join(menu).replace('\n', ' ') list=one_line.split(',') s = ''; for x in list: if ':' in x: x='\n'+ x s = s + x +' ' print(s) – ArrayNo1 Sep 24 '15 at 0:35 add a comment |"
360,"
Q: Python Logging to Tkinter Text Widget ","11 In addition to the above answers: even though there are a lot of proposed solutions for this (here and also in this other thread), I was struggling quite a bit to make this work myself. Eventually I ran into this text handler class by Moshe Kaplan, which uses a ScrolledText widget (which is probably easier than the ScrollBar method). It took me some time to figure out how to actually use Moshe's class in a threaded application. In the end I created a minimal demo script that shows how to make it all work. As it might be helpful to others I'm sharing it below. In my particular case I wanted to log to both the GUI and to a text file; if you don't need that just remove the filename attribute in logging.basicConfig. import time import threading import logging try: import tkinter as tk # Python 3.x import tkinter.scrolledtext as ScrolledText except ImportError: import Tkinter as tk # Python 2.x import ScrolledText class TextHandler(logging.Handler): # This class allows you to log to a Tkinter Text or ScrolledText widget # Adapted from Moshe Kaplan: https://gist.github.com/moshekaplan/c425f861de7bbf28ef06 def __init__(self, text): # run the regular Handler __init__ logging.Handler.__init__(self) # Store a reference to the Text it will log to self.text = text def emit(self, record): msg = self.format(record) def append(): self.text.configure(state='normal') self.text.insert(tk.END, msg + '\n') self.text.configure(state='disabled') # Autoscroll to the bottom self.text.yview(tk.END) # This is necessary because we can't modify the Text from other threads self.text.after(0, append) class myGUI(tk.Frame): # This class defines the graphical user interface def __init__(self, parent, *args, **kwargs): tk.Frame.__init__(self, parent, *args, **kwargs) self.root = parent self.build_gui() def build_gui(self): # Build GUI self.root.title('TEST') self.root.option_add('*tearOff', 'FALSE') self.grid(column=0, row=0, sticky='ew') self.grid_columnconfigure(0, weight=1, uniform='a') self.grid_columnconfigure(1, weight=1, uniform='a') self.grid_columnconfigure(2, weight=1, uniform='a') self.grid_columnconfigure(3, weight=1, uniform='a') # Add text widget to display logging info st = ScrolledText.ScrolledText(self, state='disabled') st.configure(font='TkFixedFont') st.grid(column=0, row=1, sticky='w', columnspan=4) # Create textLogger text_handler = TextHandler(st) # Logging configuration logging.basicConfig(filename='test.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') # Add the handler to logger logger = logging.getLogger() logger.addHandler(text_handler) def worker(): # Skeleton worker function, runs in separate thread (see below) while True: # Report time / date at 2-second intervals time.sleep(2) timeStr = time.asctime() msg = 'Current time: ' + timeStr logging.info(msg) def main(): root = tk.Tk() myGUI(root) t1 = threading.Thread(target=worker, args=[]) t1.start() root.mainloop() t1.join() main() Github Gist link to above code here: https://gist.github.com/bitsgalore/901d0abe4b874b483df3ddc4168754aa share|improve this answer| follow |||| edited May 23 '17 at 12:02 Community♦ 111 silver badge answered Jan 31 '17 at 14:14 johanjohan 61466 silver badges1414 bronze badges For those of you getting the TclStackFree: incorrect freePtr error, the answer above solves this. Using self.widget.after(0, function_to_execute) ensures that the widget is modified by the thread it belongs to. – Felix May 5 '17 at 8:49 add a comment |"
361,"
Q: Passing a complete LPCSTR to a c++ dll from python text ",0 Finally fix this. Turns out that I have to encode the text before I pass it to dll. So instead of doing this sampledll.Display('Some Text') It should be done like this sampledll.Display('Some Text'.encode('utf-8)) share|improve this answer| follow |||| answered May 31 '17 at 9:22 MonSh1rEMonSh1rE 10977 bronze badges add a comment |
362,"
Q: Picture and text scraping with python ","1 Please never use regular expressions, it's not made for parsing html. Normally I make use of the following combination of tools: requests module lxml.html beautifulsoup4 to detect the website encoding A approach would look like this and I hope you get the idea (The code just illustrated the concept, not tested, won't work): import lxml.html import requests from cssselect import HTMLTranslator, SelectorError from bs4 import UnicodeDammit # first do the http request with requests module like r = requests.get('http://example.com') html = r.read() # Try to parse/decode the HTML result with lxml and beautifoulsoup4 try: doc = UnicodeDammit(html, is_html=True) parser = lxml.html.HTMLParser(encoding=doc.declared_html_encoding) dom = lxml.html.document_fromstring(html, parser=parser) dom.resolve_base_href() except Exception as e: print('Some error occured while lxml tried to parse: {}'.format(e.msg)) return False # Try to extract all data that we are interested in with CSS selectors! try: results = dom.xpath(HTMLTranslator().css_to_xpath('some css selector to target the DOM')) for e in results: # access elements like print(e.get('href')) # access href attribute print(e.text_content()) # the content as text # or process further found = e.xpath(HTMLTranslator().css_to_xpath('h3.r > a:first-child')) except Exception as e: print(e.__cause__) share|improve this answer| follow |||| answered Jan 12 '14 at 1:13 Nikolai TschacherNikolai Tschacher 1,29911 gold badge1111 silver badges2020 bronze badges add a comment |"
363,"
Q: How to add python text to HTML website? [closed] ","1 use code tag but if you need hightlighting the check: highlight.js <code>print('Hello')</code> share|improve this answer| follow |||| answered Dec 8 '16 at 7:59 metmirrmetmirr 3,38722 gold badges1313 silver badges3030 bronze badges add a comment |"
364,"
Q: How to print multiple lines of text with python ","45 You can use Triple quotes (single ' or double ""): a = """""" text text text """""" print(a) share|improve this answer| follow |||| answered Jan 24 '16 at 19:20 JRazorJRazor 1,8951212 silver badges2121 bronze badges add a comment |"
365,"
Q: Python PIL -> centering long text ","1 You are interested in the 'align' argument in http://pillow.readthedocs.io/en/5.2.x/reference/ImageDraw.html#PIL.ImageDraw.PIL.ImageDraw.ImageDraw.text - it can be ""left"", ""center"" or ""right"". draw.text(((MAX_W - w) / 2, (MAX_H - h) / 2),astr,(255,255,255),font=font,align=""center"") share|improve this answer| follow |||| answered Jul 27 '18 at 21:36 radarhereradarhere 46055 silver badges1111 bronze badges add a comment |"
366,"
Q: python regex specific blocks of text from large text file ","0 Regex isn't the fastest way to search a string. You also compounded the problem by having a very big string (35MB). Reading an entire file into memory is generally not recommended because you may run into memory issues. Judging from your regex pattern, it seems like you want to capture 4-line groups that start with an 11-digit string and end with some time-line string. Try this code: import re start_pattern = re.compile(r'^\d{11}$') end_pattern = re.compile(r'^\d{4}\D+\d{2}\D+\d{1}\D+\d{2}\D+\d{2}\D+\d{2}\D+\d{1}$') capturing = 0 capture = '' with open('output.txt', 'w') as output_file: with open('input.txt', 'r') as input_file: for line in input_file: if capturing > 0 and capturing <= 4: capturing += 1 capture += line elif start_pattern.match(line): capturing = 1 capture = line if capturing == 4: if end_pattern.match(line): output_file.write(capture + '\n') else: capturing = 0 It iterates over the input file, line by line. If it finds a line matching the start_pattern, it will read in 3 more. If the 4th line matches the end_pattern, it will write the whole group to the output file. share|improve this answer| follow |||| answered Oct 6 '15 at 15:22 Code DifferentCode Different 63k1212 gold badges105105 silver badges135135 bronze badges This works like a charm, thank you very much for the answer! – Rob Oct 6 '15 at 15:52 add a comment |"
367,"
Q: Python technique or simple templating system for plain text output ","12 There are quite a number of template engines for python: Jinja, Cheetah, Genshi etc. You won't make a mistake with any of them. share|improve this answer| follow |||| answered Jun 17 '11 at 12:36 jammonjammon 3,12033 gold badges1717 silver badges2929 bronze badges add a comment |"
368,"
Q: Errors when trying to remove parentheses in python text ","1 From the python docs - To match the literals '(' or ')', use \( or \), or enclose them inside a character class: [(] [)]. So use one of the above patterns instead of the plain brackets in your regex. e.g.REMOVE_LIST = [""\(A mass = 200 GeV\)""] EDIT: The issue seems to be with your use of \b in the regex - which according to the docs linked above also matches the braces. My seemingly-working example is, import re # Test input myTestString = ""someMess (A mass = 200 GeV) and other mess (remove me if you can)"" replaceWith = ""HEY THERE FRIEND"" # What to remove removeList = [r""\(A mass = 200 GeV\)"", r""\(remove me if you can\)""] # Build the regex remove = r'(' + '|'.join(removeList) + r')' regex = re.compile(remove) # Try it! out = regex.sub(replaceWith, myTestString) # See if it worked print(out) share|improve this answer| follow |||| edited Jul 17 '15 at 14:02 answered Jul 17 '15 at 13:03 James ElderfieldJames Elderfield 2,06111 gold badge2727 silver badges2929 bronze badges I tried REMOVE_LIST = [""\(A mass = 200 GeV\)""] as well as REMOVE_LIST = [""[(]A mass = 200 GeV[)]"" both with and without the` r'(\w*)'` part inside place_holder. Without this nothing was removed at all, with it I got TypeError: 'str' object cannot be interpreted as an integer – Ciara Jul 17 '15 at 13:27 The \b cannot match anything, it is a word boundary assertion (and works like a look-around that consumes nothing). So, \b does not match braces, it just asserts the position between a word and a non-word character. – Wiktor Stribiżew Jul 20 '15 at 7:24 add a comment |"
369,"
Q: Runnig python from sublime text extremely slow ","2 Just the first run, since the second it's fast. Because it takes time to load python interpreter, parsing the byte-codes and actually running. First: Hello from Python [Finished in 5.4s] Second and later: Hello from Python [Finished in 0.1s] share|improve this answer| follow |||| answered Jul 5 '14 at 0:08 Victor PanisaVictor Panisa 39511 silver badge88 bronze badges That shouldn't take five whole seconds even the first time. Python does not take that long to start up. There aren't even any imports in that script. – kindall Jul 5 '14 at 2:35 it was not the second or the third run that ran fast, but after a couple of runs now is normal. Not exactly sure why. – Matej Jul 5 '14 at 15:55 add a comment |"
370,"
Q: python text to speech with that can read variables contents ","2 You're passing it the wrong arguments. The second argument to say is a name to give the utterance, not more stuff to say. Concatenate the strings to get what you want: engine.say(""my name is "" + name, ""saymyname"") Similarly, os.system only takes one argument. You need to build a single string to pass to it: os.system(""say hello my name is "" + name) share|improve this answer| follow |||| answered Mar 3 '14 at 5:40 user2357112 supports Monicauser2357112 supports Monica 187k2020 gold badges268268 silver badges355355 bronze badges Thanks this is what I was looking for. But one other question. can i have this module read only one variable ? or can i pass in multiple variables ? – Matt Mar 3 '14 at 6:06 You can build a string with the contents of as many variables as you want. – user2357112 supports Monica Mar 3 '14 at 6:20 add a comment |"
371,"
Q: Writing nicely formatted text in Python ","27 If you can get a list of all filenames first, then you could do something like: max_width = max(len(filename) for filename in filenames) for filename in filenames: f.write(filename.ljust(max_width+1)+..whatever else..) If you can't get a list of all filenames first, then there's no way to make sure that everything will line up, because there's no way to know if you'll later get a file whose name is really long. In a case like this, though, I would usually just assume that N columns is generally sufficient, for some N, in which case you can just do something like: f.write('%-40s %6s %10s %2s\n' % (filename, type, size, modified)) share|improve this answer| follow |||| answered May 30 '12 at 17:28 Edward LoperEdward Loper 12.3k55 gold badges3535 silver badges4848 bronze badges add a comment |"
372,"
Q: cleaning text data using python ","0 If I understand your problem correctly, from the symptoms you're describing, your issue is a classic one: you need to copy a list and modify the copy without modifying the original. This has been answered on stack overflow already, see here: How to clone or copy a list? For your specific example, the solution is to modify your line where you assign to df[""zip""] to this: df[""zip""] = df[""Af_split_split""][:] That slice operator with no numbers will create a new copy of the list (instead of creating a pointer) so that modifications to the copy do not affect the original. share|improve this answer| follow |||| edited May 23 '17 at 11:53 Community♦ 111 silver badge answered Mar 2 '17 at 23:10 JawguyChooserJawguyChooser 1,39699 silver badges2727 bronze badges add a comment |"
373,"
Q: Create Text Document (Python) [closed] ","38 f = open('file.txt','w') a = input('is python good?') f.write('answer:'+str(a)) f.close() Easy no? :) share|improve this answer| follow |||| answered Feb 14 '12 at 21:45 ingframiningframin 53955 silver badges77 bronze badges 2 The str(a) seems to make things too complex. Could you pick one version of Python (3.x or 2.x) and use either input or raw_input to make this simpler? – S.Lott Feb 14 '12 at 21:46 In which sense too complex? str(value) is a standard command of python, if you use str() on a string it doesn't hurts, if you use it on numbers it converts them to strings making this snippet working. This code works both on version 2.7 and 3.x, where is the problem? – ingframin Feb 15 '12 at 11:42 If you use raw_input you don't need str at all. That would be simpler. If you choose to claim this is Python 3, and use input, you don't need str at all. That would be simpler. – S.Lott Feb 15 '12 at 22:27 Thanks, it's more clear now :-) – ingframin Feb 16 '12 at 23:30 add a comment |"
374,"
Q: Python text spacing and aligning while fetching webpages via BeautifulSoup ","0 as you're not giving a reproducible example, I'll just go on with a few advices, and all code below has been untested, so it's to be considered for the algorithmic idea, not a direct copy/paste. You have two strategies to nail this problem: you're setting ahead from parsing the width of each column ; you're getting the size of each column based on the largest cell. Strategy ①: single pass, but fixed header column width For the first strategy you can do it in one loop (as you're doing), but you need a way to treat differently the first cell of a row from the others, so you can give it a larger size. So that would be: ### within your try/except block: # take the first cell to show off the team name on 20 columns # and strip it if it's longer than 20 columns. I like to add # three dots to strings I'm cutting, so here it goes: if len(row_team[0]) > 20: out_l = ['{}…'.format(row_team[0][:19])] else: # the ljust() method pads the right side of your string # with spaces out_l = [row_team[0].ljust(20)] for stat in row_team[1:]: # for each stat, parse it as float, and reinterpret it so # it's a ' 0.00' format, you might want to do 5.2f if some # values are in the 100s out_l.append(""{: 4.2f}"".format(float(stat))) # printing out the line, by making a string out of the list # using the ' '.join() method, adding a single space between # elements out = ' '.join(out_l) print(out) # write the line with a carriage return f.write('{}\n'.format(out)) if(i == 16 and flag == 0): # here I'm centering the string's middle at 40 columns # considering a full width of 80 columns. If you set 10 # columns for a string that's 18 characters, it's going # to have no effects! out = ""Western Conference"".center(80) print() # empty line print(out) print() # empty line # print the string surrounded by empty lines f.write(""\n{}\n\n"".format(out)) BTW, to avoid having to manage i as: i = 0 for whatever: something i = i + 1 you can do: for i, row in enumerate(tableStats.find_all('tr')[2:]): and i will be incremented for each value. Which would give you an output like: Golden State Warrio… 67.00 7.00 0.90 40-05 San Antonio Spurs 62.00 12.00 0.83 39-06 ^^^^^- this is not handled with the code above, cf the end of my post. strategy ②: with two passes For the second strategy, you need to build a matrix (so basically a list of lists), as a first pass: # init the matrix as an empty list stats_matrix = [] for row in tableStats.find_all('tr')[2:]: row_team = row.find_all(""td"") # build a list, starting with the first cell: line = [row_team[0]] # find out what's the largest string for the first column max_header_size = max(max_header_size, len(row_team[0]) for stat in row_team[1:]: # then all the other cells as floats line.append(float(stat)) # add it to the matrix: stats_matrix.append(line) then once it's done, you can use the max_header_size to format your first column: for line in stats_matrix: # show the first cell with a padding on the right of size ""max_header_size"" out = [line[0].ljust(max_header_size)] for stat in line[1:]: # print each stat, which was stored as float, as a ' 0.00' string out.append(""{: 4.2f}"".format(stat)) # show on standard output print(' '.join(line)) # and write to file (with extra \n at the end) f.write('{}\n'.format(' '.join(line))) and then you should see it all well formatted. N.B.: all that being said, this code won't work with your data set, as the last value is NOT a float, but a score (NN-NN). So it's up to you to fix it so the last element is not being taken as a float. If I were you, I'd consider this option (for the second strategy): … # iterate over the stats, leaving out the first and last value for stat in line[1:-1]: # do stuff with the floats score = line[-1].split('-') # split the string in two values line.append(score) # store the value as a tuple and then within the second loop: … for stat in line[1:-1]: … line.append('{:02d}-{:02d}'.format(score[0], score[1])) # show on standard output print(' '.join(line)) … Then you should have an output like: Golden State Warriors 67.00 7.00 0.90 40-05 San Antonio Spurs 62.00 12.00 0.83 39-06 HTH share|improve this answer| follow |||| edited Mar 30 '16 at 19:19 answered Mar 30 '16 at 19:10 zmozmo 21.8k33 gold badges4444 silver badges7878 bronze badges add a comment |"
375,"
Q: Python: text log file processing and transposing rows to columns ","0 The solution I came up with is a bit messy but it works, check it out below: import sys import re import StringIO ifile = open(sys.argv[1],'r') #Input log file as command-line argument ofile = open(sys.argv[1][:-4]+""_formatted.csv"",'w') #output formatted log txt stringOut = """" i = 0 flagReturn = True j = 0 reVal = re.compile(""Pid[\s]+(.*)\nName[\s]+(.*)\n[-]+\<br\>(.*)\<br\>"") #Regex pattern for separating the Pid & Name from the variables reVar = re.compile(""(.*)[ ]+(.*)"") #Regex pattern for getting vars and their values reVarStr = re.compile("">>> [0-9]+.(.*)=(.*)"") #Regex Pattern for Struct reVarStrMatch = re.compile(""Struct(.*)+has(.*)+members:"") #Regex pattern for Struct check for lines in ifile.readlines(): if(i>8): #Omitting the first 9 lines of Garbage values if(lines.strip()==""----------------------------------------------""): #Checking for separation between PID & Name group and the Var group j+=1 #variable keeping track of whether we are inside the vars section or not (between two rows of hyphens) flagReturn = not flagReturn #To print the variables in single line to easily separate them with regex pattern reVal if(not flagReturn): stringTmp = lines.strip()+""<br>"" #adding break to the end of each vars line in order for easier separation else: stringTmp = lines #if not vars then save each line as is stringOut += stringTmp #concatenating each lines to form the searchable string i+=1 #incrementing for omitting lines (useless after i=8) if(j==2): #Once a complete set of PIDs, Names and Vars have been collected j=0 #Reset j matchObj = reVal.match(stringOut) #Match for PID, Name & Vars line1 = ""Pid,Name,"" line2 = matchObj.group(1).strip()+"",\""""+matchObj.group(2)+""\"","" buf = StringIO.StringIO(matchObj.group(3).replace(""<br>"",""\n"")) structFlag = False for line in buf.readlines(): #Separate each vars and add to the respective strings for writing to file if(not (reVarStrMatch.match(line) is None)): structFlag = True elif(structFlag and (not (reVarStr.match(line) is None))): matchObjVars = reVarStr.match(line) line1 += matchObjVars.group(1).strip()+"","" line2 += matchObjVars.group(2).strip()+"","" else: structFlag = False matchObjVars = reVar.match(line) try: line1 += matchObjVars.group(1).strip()+"","" line2 += matchObjVars.group(2).strip()+"","" except: line1 += line.strip()+"","" line2 += "" ,"" ofile.writelines(line1[:-1]+""\n"") ofile.writelines(line2[:-1]+""\n"") ofile.writelines(""\n"") stringOut = """" #Reseting the string ofile.close() ifile.close() EDIT This is what I came up with to include the new pattern as well. I suggest you do the following: Run the parser script on a copy of the log file and see where it fails next. Identify and write down the new pattern that broke the parser. Delete all data in the newly identified pattern. Repeat from Step 1 till all patterns have been identified. Create individual regular expressions pattern for each type of pattern and call them in separate functions to write to the string. EDIT 2 structFlag = False RBYflag = False for line in buf.readlines(): #Separate each vars and add to the respective strings for writing to file if(not (reVarStrMatch.match(line) is None)): structFlag = True elif(structFlag and (not (reVarStr.match(line) is None))): matchObjVars = reVarStr.match(line) if(matchObjVars.group(1).strip()==""RBY"" and not RBYFlag): line1 += matchObjVars.group(1).strip()+"","" line2 += matchObjVars.group(2).strip()+""**"" RBYFlag = True elif(matchObjVars.group(1).strip()==""RBY""): line2 += matchObjVars.group(2).strip()+""**"" else: if(RBYFlag): line2 = line2[:-2] RBYFlag = False line1 += matchObjVars.group(1).strip()+"","" line2 += matchObjVars.group(2).strip()+"","" else: structFlag = False if(RBYFlag): line2 = line2[:-2] RBYFlag = False matchObjVars = reVar.match(line) try: line1 += matchObjVars.group(1).strip()+"","" line2 += matchObjVars.group(2).strip()+"","" except: line1 += line.strip()+"","" line2 += "" ,""` NOTE This loop has become very bloated and it is better to create a separate function to identify the type of data and return some value accordingly. share|improve this answer| follow |||| edited Sep 29 '19 at 21:37 answered Sep 15 '19 at 23:55 Akatsuki777Akatsuki777 2955 bronze badges I am getting the following error while trying to parse the actual file:Traceback (most recent call last): File ""A:\Vint.py"", line 49, in <module> line1 += matchObjVars.group(1).strip()+"","" AttributeError: 'NoneType' object has no attribute 'group' – AWA Sep 16 '19 at 6:53 Add a print (""\n""+line+""\n"") before matchObjVars = reVar.match(line) and post the final line printed before the error. – Akatsuki777 Sep 16 '19 at 7:34 There are some empty lines at the end and before that following was the last line: USER ABC – AWA Sep 16 '19 at 7:47 There are some variables which don't have a value and they seems to be causing this issue, so do i need to replace the empty string with some value? – AWA Sep 16 '19 at 8:05 1 unfortunately its same error at the following line: line2 = matchObj.group(1).strip()+"",\""""+matchObj.group(2)+""\"","" – AWA Sep 16 '19 at 12:47 | show 6 more comments"
376,"
Q: My python text file appears as empty ","2 Print out your n and h in ""for n,h in zip(name,hb)"". You'll see, that zip give you an amount of tuples. As your opening the file again and again, it will get overridden (as you dont use append mode). Have a look at: https://docs.python.org/2/tutorial/inputoutput.html share|improve this answer| follow |||| answered May 13 '16 at 9:28 mutilismutilis 53333 silver badges1515 bronze badges add a comment |"
377,"
Q: Render Latex text with python ","1 You have to add to your code these lines to render latex text by your own installed software (by default matplotlib use MathText: http://matplotlib.org/api/mathtext_api.html): from matplotlib import rcParams rcParams['text.usetex'] = True The second problem is that you have to put your latex string to one line (and you forget $-brackets for matrices): import matplotlib.pyplot as plt from matplotlib import rcParams rcParams['text.usetex'] = True txte = r""The \emph{characteristic polynomial} $\chi(\lambda)$ of the $3 \times 3$~matrix \\ $\left( \begin{array}{ccc} a & b & c \\ d & e & f \\g & h & i \end{array} \right) $ \\is given by the formula\\ $ \chi(\lambda) = \left| \begin{array}{ccc} \lambda - a & -b & -c \\ -d & \lambda - e & -f \\ -g & -h & \lambda - i \end{array} \right|. $"" plt.text(0.0, 0.0, txte, fontsize=14) ax = plt.gca() ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) plt.show() share|improve this answer| follow |||| answered Jul 3 '16 at 9:32 SerenitySerenity 23.7k1616 gold badges7474 silver badges8585 bronze badges add a comment |"
378,"
Q: Processing Urdu Bidirectional text in text editors and Python ","0 First, don't rely on bidi text appearing correctly in a Word file. It doesn't guarantee that the same text would appear correctly when in some other environment. Microsoft Word has its own way of handling bidirectional text in current and legacy versions which is not necessarily the way Unicode-compliant text-editors (like gedit) would handle that text. This might or might not be resolved eventually as Microsoft would implement a newer version of Unicode Bidirectional Algorithm in products. Secondly, the reason which you don't see the copied text properly is that your text environment (including here) doesn't support bidi text properly and it's not even possible to have right-to-left text displayed. I copied your sample string in a Unicode-compliant text-editor and change the direction to right and this is the result which is correct. Now to be able to process your text in that Word file using Python you need to improvise a bit. You can export the text content as Unicode text and then process it with Python. Or in case you want to process the text content in-place (inside Word), you might be able to get some satisfactory results out of OLE component scripting from your Python. See the related question here. share|improve this answer| follow |||| edited May 23 '17 at 12:04 Community♦ 111 silver badge answered Apr 16 '13 at 20:26 ShervinShervin 1,6721616 silver badges2424 bronze badges add a comment |"
379,"
Q: Python Graphics Text ","1 I don't know what packages and modules you are using. So I can't exact answer for your question. However, you can get the information of reader function by using help. help function give the document for argument. This is example code for sympy.sqrt function import sympy help(sympy.sqrt) ## it gives you a helpful information. In your case, use the following code help(font.reader) share|improve this answer| follow |||| answered Dec 2 '16 at 3:51 HulkHulk 27111 silver badge88 bronze badges add a comment |"
380,"
Q: extract a line from text file python [closed] ","4 with open('path_to_file', 'rb') as f: for line in f: if line.startswith('Error'): do_something_with_line(line) You might also want to strip the line with line.strip(). share|improve this answer| follow |||| edited Jul 8 '13 at 14:55 answered Jul 7 '13 at 21:15 Maciej GolMaciej Gol 12.1k22 gold badges2626 silver badges4343 bronze badges add a comment |"
381,"
Q: insert text on image[2], python ","1 It's actually pretty easy to do, for those who might need it, here is the code to do it: from PIL import Image from PIL import ImageDraw from PIL import ImageFont img = Image.open('rect.png') var = input(""Please enter something: "") print (""you entered"", var) font = ImageFont.truetype(""arial.ttf"", 20) draw = ImageDraw.Draw(img) draw.text((0,0), var, (255,255,0), font=font) draw = ImageDraw.Draw(img) img.save(""a_test.png"") img.show() share|improve this answer| follow |||| answered May 16 '18 at 8:21 A.khouA.khou 38811 silver badge1515 bronze badges add a comment |"
382,"
Q: Formating a text file in python 2 ",0 with open('iplist.txt') as fd: my_list = [line.strip().rstrip('.txt') for line in fd] print ' '.join(my_list) outputs: 1231231 231231 24141241 3123123 341241241 4123412 assuming this: $ cat iplist.txt 1231231.txt 231231.txt 24141241.txt 3123123.txt 341241241.txt 4123412.txt Please note that you do not need to close the filehandle when using the with construct. As soon as it moves out of scope the filehandle will be automatically closed. share|improve this answer| follow |||| answered Aug 29 '11 at 13:14 Fredrik PihlFredrik Pihl 38.2k55 gold badges6666 silver badges117117 bronze badges add a comment |
383,"
Q: Python string text to set of points ","0 So for a 3D Model you perhaps need more than only some few points think about the letter ""S"". Created some code to get generic the coordinates. You can play around with the size of the font, and picture size, to get more or less points. The arial.ttf was downloaded from https://github.com/JotJunior/PHP-Boleto-ZF2/blob/master/public/assets/fonts/arial.ttf from PIL import Image from PIL import ImageDraw from PIL import ImageFont import numpy as np import cv2 print np.__version__ def getPixelColor(pixel, x, y): try: return pixel[x,y] except IndexError: return (255,255,255) def hasNeighbourColor(color, pixels, x, y): if getPixelColor(pixels,x-1,y-1) == color or getPixelColor(pixels,x,y-1) == color or getPixelColor(pixels,x+1,y-1) == color \ or getPixelColor(pixels,x-1,y) == color or getPixelColor(pixels,x+1,y) == color\ or getPixelColor(pixels,x-1,y+1) == color or getPixelColor(pixels,x,y+1) == color or getPixelColor(pixels,x+1,y+1) == color: return True return False def createMapOfLetter(letter): print ""LETTER %s:"" % letter print ""---------"" img = Image.new('RGB', (230, 230), ""white"") d = ImageDraw.Draw(img) font = ImageFont.truetype(""arial.ttf"", 300) d.text((15, -50), letter, fill=(0, 0, 0), font=font) img.save('letter-%s.png' % letter, 'png') pixels = img.load() ans = [] for x in range(230): for y in range(230): if pixels[x,y] == (0,0,0): if hasNeighbourColor((255,255,255), pixels, x, y) and not hasNeighbourColor((255,0,0), pixels, x, y): pixels[x,y] = (255,0,0) ans.append([x,y]) for x in range(230): for y in range(230): if pixels[x,y] != (255,0,0): pixels[x,y] = (255,255,255) img.save('letter_map-%s.png' % letter, 'png') print ans createMapOfLetter('A') The script creates the letter-[letter].png which looks like this. Left A right S And then creates a list of coordinates. The coordinates in a picture look like this. The script automatically creates a letter_map-[letter].png. Left A, Right S The output of the coordinates for A look like this it. [[15, 221], [17, 215], [17, 221], [19, 221], [21, 221], [22, 202], [23, 221], [25, 194], [25, 221], [27, 221], [29, 221], [30, 181], [31, 221], [33, 173], [33, 221], [35, 221], [37, 221], [38, 160], [39, 221], [41, 152], [41, 221], [43, 221], [45, 218], [46, 139], [49, 131], [52, 198], [53, 195], [54, 118], [54, 192], [57, 110], [61, 172], [62, 97], [62, 169], [63, 166], [65, 89], [67, 156], [69, 156], [70, 76], [71, 156], [73, 68], [73, 156], [75, 156], [76, 134], [77, 129], [77, 156], [78, 55], [78, 134], [79, 156], [80, 134], [81, 47], [81, 118], [81, 156], [82, 134], [83, 156], [84, 134], [85, 107], [85, 156], [86, 34], [86, 134], [87, 156], [88, 134], [89, 26], [89, 96], [89, 156], [90, 134], [91, 156], [92, 134], [93, 85], [93, 156], [94, 13], [94, 134], [95, 156], [96, 134], [97, 7], [97, 74], [97, 156], [98, 71], [98, 134], [99, 7], [99, 156], [100, 134], [101, 7], [101, 156], [102, 134], [103, 7], [103, 56], [103, 156], [104, 134], [105, 7], [105, 156], [106, 46], [106, 134], [107, 7], [107, 156], [108, 134], [109, 7], [109, 35], [109, 156], [110, 31], [110, 134], [111, 7], [111, 156], [112, 134], [113, 7], [113, 156], [114, 37], [114, 134], [115, 7], [115, 40], [115, 156], [116, 43], [116, 134], [117, 7], [117, 46], [117, 156], [118, 49], [118, 134], [119, 7], [119, 156], [120, 134], [121, 7], [121, 156], [122, 134], [123, 7], [123, 63], [123, 156], [124, 66], [124, 134], [125, 7], [125, 156], [126, 134], [127, 74], [127, 156], [128, 134], [129, 156], [130, 134], [131, 85], [131, 156], [132, 134], [133, 156], [134, 134], [135, 96], [135, 156], [136, 134], [137, 32], [137, 156], [138, 104], [138, 134], [139, 37], [139, 156], [140, 134], [141, 42], [141, 156], [142, 115], [142, 134], [143, 47], [143, 156], [144, 134], [145, 52], [145, 123], [145, 156], [146, 134], [147, 57], [147, 156], [148, 134], [149, 62], [149, 156], [151, 67], [151, 156], [153, 72], [153, 156], [155, 77], [155, 156], [157, 82], [157, 156], [159, 87], [160, 161], [164, 172], [168, 183], [171, 191], [175, 202], [178, 210], [180, 139], [182, 144], [182, 221], [184, 149], [184, 221], [186, 154], [186, 221], [188, 159], [188, 221], [190, 164], [190, 221], [192, 169], [192, 221], [194, 174], [194, 221], [196, 179], [196, 221], [198, 184], [198, 221], [200, 189], [200, 221], [202, 194], [202, 221], [204, 199], [204, 221], [206, 221], [208, 221], [210, 221], [212, 221]] The output for the coordinates of S look like this [[30, 162], [31, 167], [35, 179], [38, 52], [39, 187], [41, 81], [43, 193], [47, 198], [49, 28], [52, 203], [57, 101], [57, 161], [58, 19], [61, 172], [62, 174], [64, 15], [66, 52], [66, 68], [67, 49], [67, 181], [68, 73], [68, 108], [69, 45], [70, 76], [70, 109], [71, 42], [71, 185], [74, 10], [75, 81], [77, 36], [78, 190], [78, 218], [80, 34], [85, 220], [86, 31], [89, 30], [89, 116], [89, 221], [90, 88], [91, 5], [91, 196], [93, 89], [97, 4], [98, 28], [103, 120], [107, 93], [107, 121], [111, 94], [111, 122], [127, 28], [128, 4], [130, 127], [134, 223], [135, 100], [136, 30], [139, 6], [139, 31], [140, 130], [140, 222], [143, 131], [146, 8], [148, 133], [154, 136], [156, 44], [157, 189], [157, 217], [159, 49], [161, 141], [162, 15], [162, 109], [163, 61], [163, 143], [164, 110], [166, 111], [167, 149], [167, 212], [168, 19], [169, 171], [177, 118], [177, 205], [182, 200], [188, 129], [189, 49], [190, 132], [190, 189], [195, 178], [197, 150]] share|improve this answer| follow |||| answered Mar 4 '16 at 13:43 KordiKordi 2,15911 gold badge1111 silver badges1111 bronze badges @sevatster You're welcome. if you need a higher resolution of the points, just say it, just convert the picture before reading the points into a black and white image. – Kordi Mar 4 '16 at 16:45 add a comment |"
384,"
Q: Extract text between digits - Python ","1 Ok, so if I understand correctly, you want to capture everything between the section numbers. Here's the regex string I came up with: regex = r'(?:\d\.){4}.(.+?)(?:\d\.){4}' Let's break that down a little: (?:\d\.){4} this is our 4 numbers followed by a period. the (?:) makes it a non-capturing group, so we can look for this pattern to count it 4 times, but not add it to our matches. (.+?) This is the part we want to capture. When parentheses are used without ?:, it makes a capture group, and it is what we are matching. .+? means one or more of any character, non-greedy. The question mark is the non-greedy part, and it means we don't keep matching characters forever, we stop when we get to the next part of the expression. (?:\d\.){4} We end with our section pattern again because we want to capture between two sections Here is the code we use to grab what we want: p = re.compile(regex, flags=re.DOTALL) The DOTALL flag allows us to keep newlines, typically . matches any character except newline. sections = p.findall(text) where text is your string to search through The findall method returns a list of the capturing groups we matched. ['A section\n\nSome text. Some other text, too. And stuff. And even more text on the next line.\n\n', ""Some sections are really great\n\nWelcome to this section. Which is probably better than others. And I can't even begin to explain how great it is.\n\n""] share|improve this answer| follow |||| edited Jun 9 '16 at 20:47 answered Jun 9 '16 at 20:10 sajattacksajattack 71611 gold badge66 silver badges2323 bronze badges It looks like in your case your solution drops the last section: ""1.2.3.7. What? A new section? Dang right it's a new section! Aren't you even ready for it? So many new sections can be used for text you'll never read."" It appears like he only wants things that have section headers, and he wants them stripped. Unfortunately it doesn't look like what will have a section header and what won't isn't well defined in the documents. – Maurice Reeves Jun 9 '16 at 20:28 Oh yeah I didn't think of that. – sajattack Jun 9 '16 at 20:34 I asked him for more details just to see if we could get to a better solution, but haven't heard back yet. Your solution is very close, so long as we can figure out what the next piece is after this particular section. Maybe there's a clean break, and then with a small modification your solution would be complete. – Maurice Reeves Jun 9 '16 at 20:36 1 Perhaps if sections end with 2 newlines or some such we can match that at the end instead of the next section number. – sajattack Jun 9 '16 at 20:38 That's what I'm wondering too. If needs more help or can't figure it out, he'll be back. :-D – Maurice Reeves Jun 9 '16 at 20:38 | show 2 more comments"
385,"
Q: What is the JavaScript equivalent of python's .text? ","1 WebElement.getText() is the javascript equivalent of getting text element in python when using WebdriverJS. This should work for you. I have created an async function and easy to understand for loop. Also, since getText() return a promise so i am using async - await to get the text and push it to the followers_temp array. async function pushTextIn() { for (let i = 0; i < followers_elems.length; i++) { let text = await followers_elems[i].getText(); console.log('pushing text: ', text); followers_temp.push(text); } return followers_temp; } share|improve this answer| follow |||| edited Jun 29 '18 at 18:13 answered Jun 29 '18 at 18:05 PJAutomatorPJAutomator 32811 silver badge1111 bronze badges Pleasure to help, can you upvote the answer if it helped thanks. – PJAutomator Jun 29 '18 at 18:36 Sure thing! I tried getText() method earlier and resolved the promise with .then(). But I was getting typeerrors that getText is not a function. – Barani Jun 29 '18 at 18:39 add a comment |"
386,"
Q: Loading text data in Python [closed] ","0 Hi Anthony, Parsing your file depends on the format it is using. Assuming your file is a sequence of numbers separated by a new line, here's how I would proceed: First of all, you have to open your file. To do so, you could use the standard open function. open takes as a first argument the path of the file you want to work with, and a second argument being the mode, which could be 'r' to read, 'w' to overwrite, and 'a' to append text to the file as well as the binary variants (default is 'r'). Not talking about open's other parameters. number_file = open('numbers.txt') # implicitly read mode numbers = [] # the list in which we're going to store all our numbers # iterates over each line of the file for line in number_file: n = int(line) # parsing the string into an integer numbers.append(n) # adding the number to your list number_file.close() The cool thing is, open is also what we call in Python a context manager. Behind this jargon lies a really simple concept which is doing action before and after a given code. The with keyword allows you to use a context manager: with open('numbers.txt') as number_file: for line in number_file: n = int(line) numbers.append(n) If you didn't notice, you don't have to close your file using context managers, because open is taking care of that. That's why it is recommended rather than the previous method: You can't forget to close your file, thus it's less likely to have an OS error if you open the file twice (e.g. if you want to modify and save a serialized object). share|improve this answer| follow |||| edited Aug 31 '16 at 17:50 answered Aug 31 '16 at 17:43 baxbaxwalanuksiwebaxbaxwalanuksiwe 1,35477 silver badges1919 bronze badges add a comment |"
387,"
Q: remove single quote from text with python ","0 with open('C://file.txt') as filein, open('C://fout.txt','w') as fileout: for line in filein: line=line.replace(""'"","""") fileout.write(line) share|improve this answer| follow |||| answered Sep 10 '14 at 9:15 German PetrovGerman Petrov 1,28711 gold badge1414 silver badges2020 bronze badges add a comment |"
388,"
Q: Python MemoryError loading text files ","2 You are trying to load too much into memory at once. This can be because of the process size limit (especially on a 32 bit OS), or because you don't have enough RAM. A 64 bit OS (and 64 bit Python) would be able to do this ok given enough RAM, but maybe you can simply change the way your program is working so not every page is in RAM at once. What is cFile_list used for? Do you really need all the pages in memory at the same time? share|improve this answer| follow |||| answered Jun 23 '11 at 16:10 John La RooyJohn La Rooy 239k4545 gold badges315315 silver badges458458 bronze badges cFile_list is a big list of documents. It ends up becoming the training and test set for a Naive Bayes Classifier. What would the alternative be as far as not having everything in memory at the same time? – Greg Jun 23 '11 at 16:57 1 @Greg, can you change your program to loop through the filenames. For each filename, read the file, clean up the file, feed file to the classifier, close the file. This way only one file needs to be in ram at once. – John La Rooy Jun 23 '11 at 23:52 add a comment |"
389,"
Q: Python Reportlab PDF - Centering Text on page ","13 The reportlab canvas has a drawCentredString method. And yes, they spell it like that. We’re British, dammit, and proud of our spelling! Edit: As for text objects, I'm afraid you don't. You can do something along those lines, though: from reportlab.pdfbase.pdfmetrics import stringWidth from reportlab.rl_config import defaultPageSize PAGE_WIDTH = defaultPageSize[0] PAGE_HEIGHT = defaultPageSize[1] text = ""foobar foobar foobar"" text_width = stringWidth(text) y = 1050 # wherever you want your text to appear pdf_text_object = canvas.beginText((PAGE_WIDTH - text_width) / 2.0, y) pdf_text_object.textOut(text) # or: pdf_text_object.textLine(text) etc. You can use other page sizes, obviously. share|improve this answer| follow |||| edited Sep 20 '10 at 22:44 answered Sep 20 '10 at 22:04 Jim BrissomJim Brissom 24.4k22 gold badges3232 silver badges3333 bronze badges Thanks Jim, I used p.drawCentredString(4.25*inch, 500, 'some text') that works great, but how do I use drawCentredString with .textOut or .textLine? p.drawCentredString(center, 500, header); – jhanifen Sep 20 '10 at 22:20 add a comment |"
390,"
Q: Python - Text input within a Tkinter canvas ",4 Ok I did some more reading up on what a tuple actually is and I was able to fix the error. Just had to remove the x= and y= so that the coordinates of the window are by themselves. share|improve this answer| follow |||| answered Jul 5 '13 at 14:43 ThatsNotMyNameThatsNotMyName 51211 gold badge77 silver badges1919 bronze badges add a comment |
391,"
Q: Speech to Text using Python ","1 ""very accurate"" is depends on your accent. i suggest you: Google Speech Recognition English is not the best choice for accuracy, you main language is. (accent) first, you have to install python 2.x/3.x and pyaudio. then there is an example named ""Recognize speech input from the microphone"" on that website, you can use it for simple translation. share|improve this answer| follow |||| edited Jun 17 '16 at 13:12 answered Jun 17 '16 at 12:44 chnselimchnselim 22522 silver badges1212 bronze badges add a comment |"
392,"
Q: How to get selected text from Python Tkinter Text widget ","2 Exactly like tobias_k mentions in his comment, the order in which the event bindings are executed is key here, because you are trying to get the selected text before the text is actually selected. You can see the order of binding execution using the bindtags() widget method. When you do this for a Text widget you will see something like ('.38559496', 'Text', '.', 'all') Which means that the order, from left to right, of binding event execution is so that first events that are unique to this specific widget are evaluated, then those specific to the widget class, then those to your root window and finally everything else on application level (source). Your double-click event is on widget level, since it is applied only to that specific widget, but the actual selection of the text is an event on the Text class level. Therefore, you will have to rearrange the order so that the class events come before the widget events. You can get the order by calling bindtags without arguments and then define a new order by calling it again with a tuple containing the order: order = self.area.bindtags() self.area.bindtags((order[1], order[0], order[2], order[3])) This makes sure that the selection of the text is performed before you try to read the selection. share|improve this answer| follow |||| edited May 23 '17 at 12:14 Community♦ 111 silver badge answered Apr 28 '15 at 9:23 fhdrsdgfhdrsdg 8,26722 gold badges2727 silver badges4848 bronze badges add a comment |"
393,"
Q: Opening a text file in Python ","7 The file name is looked up in the current working directory, which is not necessarily the same as the directory the script lives in. Try using the full path to the file, or make sure the working directory is correct. You can check the working directory py printing os.getcwd() from inside your script (make sure you import os). If you are on a case-sensitive file system, check the case of your file name. share|improve this answer| follow |||| answered Nov 2 '11 at 15:41 Sven MarnachSven Marnach 420k9090 gold badges807807 silver badges736736 bronze badges 3 what he means is say your running your script from home like this: home/user/> python /programming/python/script.py its thinking that the dracula.txt is in /home/user/ – John Riselvato Nov 2 '11 at 15:44 @JohnRiselvato ah ok I will try including the path to the file then. Thanks for the help both of you :) – George Burrows Nov 2 '11 at 15:45 1 Remember if this is the correct answer for you, click the check button. – John Riselvato Nov 2 '11 at 15:48 It works! Thanks :D, although in retrospect I regret double checking by using print, the list is 50,000 words long haha. – George Burrows Nov 2 '11 at 15:49 @JohnRiselvato I shall do, however it's saying I have to wait for 3 minutes :( – George Burrows Nov 2 '11 at 15:51 add a comment |"
394,"
Q: PYTHON programming: Search and Replace text in a file [closed] ","1 If you enter a file path such as ""E:\\search_replace\\srtest.txt"", oldFileName will be ""old-E:\\search_replace\\srtest.txt"" and tempFileName will be ""temp-E:\\search_replace\\srtest.txt"", neither of which are valid. Try doing something like this instead: oldFileName = ""{}\\old-{}"".format(*os.path.split(fileToSearch)) tempFileName = ""{}\\temp-{}"".format(*os.path.split(fileToSearch)) share|improve this answer| follow |||| answered Jul 6 '11 at 0:37 MRABMRAB 17.7k55 gold badges3030 silver badges3030 bronze badges add a comment |"
395,"
Q: remove unknown code in text using python ","0 What you see as unknown codes is a — and quotation marks “. So these are normal characters encoded in utf-8. If you really want to remove them: text_without_unicode_chrs = ''.join(ch for ch in text if ch<'\x80') share|improve this answer| follow |||| answered Nov 8 '14 at 18:13 DanielDaniel 36.3k44 gold badges3737 silver badges6767 bronze badges 2 Looks a bit ugly, why not text.decode(""ascii"", errors=""ignore"")? – Tim Pietzcker Nov 8 '14 at 18:19 add a comment |"
396,"
Q: Python module for editing text in CLI ","2 You could have a look at urwid, a curses-based, full-fledged UI toolkit for python. It allows you to define very sophisticated interfaces and it includes different edit box types for different types of text. share|improve this answer| follow |||| answered Jun 1 '09 at 8:22 PhilSPhilS 1,17966 silver badges1313 bronze badges Unfortunately it does not work for windows at the moment. Even if you install a windows curses framework. – ProfVersaggi Jan 17 '16 at 19:49 add a comment |"
397,"
Q: Does Python have an argc argument? ","112 In python a list knows its length, so you can just do len(sys.argv) to get the number of elements in argv. share|improve this answer| follow |||| answered Nov 20 '11 at 13:54 sepp2ksepp2k 323k4545 gold badges626626 silver badges644644 bronze badges import sys # count the arguments arguments = len(sys.argv) - 1 print (""the script is called with %i arguments"" % (arguments)) – Teo Nov 22 '19 at 10:57 add a comment |"
398,"
Q: Python: Open .blend file as text ","1 While the blend file is a binary data file, you will also find that many of them will also be compressed. The easiest way would be to let blender read the file. To automate the task you can start blender in background mode from the CLI and have it run a python script using the blender API that extracts the info you want. blender -b --python extractor.py If you are using python to do that, you can use subprocess.run(). subprocess.run(['blender', '-b', '--python', 'extractor.py']) However if you need to do this on a machine without blender installed, it is possible to read a blend file from python without blender, depending on your needs you may find the answers here and here to be helpful. share|improve this answer| follow |||| answered May 26 '17 at 1:50 samblersambler 5,57911 gold badge1111 silver badges2222 bronze badges add a comment |"
399,"
Q: Text file opening in python ","0 with open(workfile, 'r') as f: for line in f: print line If you don't use the context manager (the with statement) you will need to explicitly call f.close(), for example: f = open('workfile', 'r') line = f.readline() print line f.close() share|improve this answer| follow |||| answered Feb 8 '17 at 21:53 Amaury LarancuentAmaury Larancuent 30311 silver badge77 bronze badges add a comment |"
400,"
Q: Using Sublime Text 2 with Portable Python ","2 I ran into the same problem and after a bit of troubleshooting, here is my solution: 1) Use the build system: { ""cmd"": [""\\Portable Python 2.7.6.1\\App\\python.exe"", ""-u"", ""$file""], ""file_regex"": ""^[ ]*File \""(...*?)\"", line ([0-9]*)"", ""selector"": ""source.python""} This build will open your program in Python Portable. Instead of specifying your flash drive letter, using ""\"" will go to the path relative to the root of the current drive. 2) At the end of your code add the following line to prevent force closing: os.system(""pause"") Also, don't forget to import the ""os"" module: import os share|improve this answer| follow |||| edited Nov 27 '14 at 14:37 answered Nov 27 '14 at 14:31 adb16xadb16x 57944 silver badges2121 bronze badges add a comment |"
401,"
Q: Python finding bolded text in RTF ","0 No, not easily. Certainly not within the scope of a StackOverflow answer. The problem is that RTF is a proprietary format, with special ""syntax"" that describes the format. There are libraries that make attempts to read it, which are described here: Is there a Python module for converting RTF to plain text? However, even if one of those would read the text for you, it would be unlikely to be telling you the format. Afterall, how would it tell you? Your best bet may be finding an RTF to HTML converter (at least one is referred to in the question that I pointed to), then using BeautifulSoup to find the bolded HTML elements. share|improve this answer| follow |||| edited May 23 '17 at 12:16 Community♦ 111 silver badge answered Jun 7 '15 at 13:23 GreenAsJadeGreenAsJade 12.9k99 gold badges5050 silver badges8585 bronze badges add a comment |"
402,"
Q: Extracting a section of text from text file using python ","0 Without regex to get list of paragraphs. with open('input.txt', 'r') as input: my_list = [] para = '' for line in input: if line != '\n': para += line else: my_list.append(para) para = '' my_list.append(para) for item in my_list: print(item) share|improve this answer| follow |||| answered Jul 29 '15 at 5:24 MorishiriMorishiri 63977 silver badges1818 bronze badges add a comment |"
403,"
Q: Python Text Widget appending tab characters ( “\t”) ","0 You could either find the index of the first tab character directly: self.column[:self.column.find('\t')] or use regex: import re m = re.match('(\d+)', self.column) s_num = m.group(0) share|improve this answer| follow |||| answered Jan 15 '17 at 18:46 xnxxnx 19.3k66 gold badges4646 silver badges8383 bronze badges Thanks for answering... I was looking for doing some with regex actually, but I didn't understand the answer, I mean... I know where the characters are, is the tab char that separate the columns, if I throw a print, it will be like: 1\t,2\t....9\t,10,11,12,13...the 9th first numbers are single digits, so the first 9th char of the 2 column are tabs...After I match them with the regex, how I prevent them to be inserted? – Guibax Jan 15 '17 at 19:03 I'm not really sure how you're using what you currently have as column[0:2], but can you not just replace this with m.group(0) after matching each line as above? – xnx Jan 15 '17 at 19:14 the group was inserting just the last enumerator on the list...but I could do it with a list comprehension..thanks anyway – Guibax Jan 17 '17 at 15:51 add a comment |"
404,"
Q: python-escpos direct printing text ","3 I am currently maintaining python-escpos. (over at https://github.com/python-escpos/python-escpos) With ""File""-printer you have to call printer.flush() at the moment, otherwise the print buffer will not be sent. I have opened an issue whether we should adapt the behaviour to that of the other printers. share|improve this answer| follow |||| answered Mar 12 '16 at 10:54 Patrick KanzlerPatrick Kanzler 11888 bronze badges 2 Just an update: v2.1.2 of python-escpos does now automatically flush after every command. – Patrick Kanzler Aug 2 '16 at 14:17 I am using your scripts, adapted it to my EPSON TM-T88IV and V, changed both of the IDs to 0x04b8, 0x0e02 or 0x04b8, 0x0202, and I get an error message TypeError: __init__() got an unexpected keyword argument 'profile' Do have an idea, what I can do ? Thanks for your help. Michael – ddlab Oct 4 '17 at 16:07 You should probably create a new question for this. On first glance you probably have version 2.2 or something like this. Please make sure to check the right documentation for this version. The current version 3 is only available when you install preview-versions with pip (it is currently in alpha) – Patrick Kanzler Oct 8 '17 at 18:39 add a comment |"
405,"
Q: Editing text file with python ","0 os.system('kate ""{}""'.format(onlyfiles[int(choice) - 1])) Better solution would be: subprocess.call(['vim', '{}'.format(onlyfiles[int(choice) - 1])]) share|improve this answer| follow |||| edited Dec 12 '14 at 22:11 answered Dec 12 '14 at 22:01 jgrittyjgritty 10k33 gold badges2828 silver badges5454 bronze badges add a comment |"
406,"
Q: Text not displaying - Python (PyGame) ","1 In your current code, this line is the problem: text.blit(text, (self.x, self.y)). This code is drawing some text onto itself. You want to draw the text onto the screen. Replace that line with this line: screen.blit(text, (self.x, self.y)). This means that you need to have a screen argument for your Text's draw function. So def draw(self): should be def draw(self,screen):. Finally, you must pass the self.screen argument, so self.p1Score.draw() should be self.p1Score.draw(self.screen). The final code, with all changes made should be: class Player(pygame.sprite.Sprite): def __init__(self, x, color): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((10, 100)) self.image.fill(color) self.rect = self.image.get_rect() self.rect.y = HEIGHT / 2 - self.rect.height / 2 self.rect.centerx = x self.displacement = 8 self.score = 0 def update(self): # update function self.constrain(0, HEIGHT - self.rect.height) def constrain(self, min, max): # constrian player to walls if self.rect.y > max: self.rect.y = max if self.rect.y < min: self.rect.y = min class Text(): def __init__(self, x, y, color, text, font): self.x = x self.y = y self.color = color self.text = text self.font = font def draw(self,screen): text = self.font.render(self.text, False, self.color) screen.blit(text, (self.x, self.y)) class Game(pygame.sprite.Sprite): def __init__(self): # get sprite object pygame.sprite.Sprite.__init__(self) # init pygame pygame.init() # init pygame modules pygame.mixer.init() pygame.font.init() # set window self.screen = pygame.display.set_mode((WIDTH, HEIGHT)) # set title pygame.display.set_caption(TITLE) # holds whether the game is running for not self.running = True # get timer function self.clock = pygame.time.Clock() # set fonts self.scoreText = pygame.font.Font(""assets/fonts/square.TTF"", 25) self.titleFontLight = pygame.font.Font(""assets/fonts/square.TTF"", 50) # group for sprites self.allSprites = pygame.sprite.Group() def new(self): # creates a new game # make players self.player1 = Player(20, RED) self.player2 = Player(WIDTH - 30, BLUE) self.ball = Ball() self.p1Score = Text(100, 100, (RED), str(self.player1.score), self.scoreText) self.p2Score = Text(WIDTH - 10, 10, (WHITE), str(self.player2.score), self.scoreText) # put players inside groups self.allSprites.add(self.player1) self.allSprites.add(self.player2) # run game loop self.gameLoop() def gameLoop(self): # main game loop while self.running: self.allSprites.update() self.update() self.clock.tick(FPS) def update(self): # updates game self.draw() pygame.display.update() def draw(self): # draws to screen # set background color self.screen.fill(BLACK) # draw to screen self.ball.draw(self.screen) self.p1Score.draw(self.screen) self.allSprites.draw(self.screen) if __name__ == '__main__': Game().new() share|improve this answer| follow |||| answered May 26 '17 at 13:18 pydudepydude 1,1081414 silver badges2323 bronze badges add a comment |"
407,"
Q: python conditional check in text [closed] ","1 I'm assuming you wanted to do something like: for letter in i: Also you need to compare against the string literals 'J' and 'K': if letter == 'J': # do first stuff elif letter == 'K': # do other stuff Also note that comparison is with ==, assignment is with =. You have this incorrect in your if blocks. Also, indentation is very important in Python, make sure your tabs (or spaces) are aligned properly. share|improve this answer| follow |||| answered Aug 17 '14 at 14:46 CoryKramerCoryKramer 84.8k1212 gold badges110110 silver badges160160 bronze badges thank you about the == notice, i totally overlooked it, on I took your advice and it overcame my trouble but if i have multiple if can i use something else that elif? – George Aug 17 '14 at 15:11 You can do if letter == 'J' or letter == 'K' or letter == 'M' etc. – CoryKramer Aug 17 '14 at 15:14 add a comment |"
408,"
Q: Text file manipulation with Python ","1 try something like the following. (untested...you can learn a bit of python by debugging!) create python file ""parser.py"" import sys f = open('filename.txt', 'r') for line in f: txt = line.strip() if txt == '': sys.stdout.write('\n\n') sys.stdout.flush() sys.stdout.write( txt + ';') sys.stdout.flush() f.close() and in a shell, type: python parser.py > output.txt and see if output.txt is what you want. share|improve this answer| follow |||| answered May 28 '15 at 18:38 user1269942user1269942 2,8161919 silver badges2929 bronze badges I'd upvote more if I could. Nice, simple and effective example with plenty of features that will keep any beginner busy :P Doesn't open('filename.txt', 'r') open the file as read only? OP should keep this in mind if trying to overwrite/edit the same file. docs.python.org/2/tutorial/inputoutput.html – Izaac Corbett May 28 '15 at 18:49 @BiTinerary I didn't want any beginner accidentally overwriting their data ;) – user1269942 May 28 '15 at 19:07 I thought as much, good call. Just wanted to make a point of it because it confused my debugging when I first tinkered around with it. – Izaac Corbett May 28 '15 at 20:29 add a comment |"
409,"
Q: Python: load text as python object ","4 Lifted almost straight from the pyparsing examples page: # read text from web page import urllib page = urllib.urlopen(""https://sites.google.com/site/iminside1/paste"") html = page.read() page.close() start = html.index(""<pre>"")+len(""<pre>"")+3 #skip over 3-byte header end = html.index(""</pre>"") text = html[start:end] print text # parse dict-like syntax from pyparsing import (Suppress, Regex, quotedString, Word, alphas, alphanums, oneOf, Forward, Optional, dictOf, delimitedList, Group, removeQuotes) LBRACK,RBRACK,LBRACE,RBRACE,COLON,COMMA = map(Suppress,""[]{}:,"") integer = Regex(r""[+-]?\d+"").setParseAction(lambda t:int(t[0])) real = Regex(r""[+-]?\d+\.\d*"").setParseAction(lambda t:float(t[0])) string_ = Word(alphas,alphanums+""_"") | quotedString.setParseAction(removeQuotes) bool_ = oneOf(""true false"").setParseAction(lambda t: t[0]==""true"") item = Forward() key = string_ dict_ = LBRACE - Optional(dictOf(key+COLON, item+Optional(COMMA))) + RBRACE list_ = LBRACK - Optional(delimitedList(item)) + RBRACK item << (real | integer | string_ | bool_ | Group(list_ | dict_ )) result = item.parseString(text,parseAll=True)[0] print result.data[0].dump() print result.data[0].segments[0].dump(indent="" "") print result.data[0].segments[0].flights[0].dump(indent="" - "") print result.data[0].segments[0].flights[0].flightLegs[0].dump(indent="" - - "") for seg in result.data[6].segments: for flt in seg.flights: fltleg = flt.flightLegs[0] print ""%(airline)s %(airlineCode)s %(flightNo)s"" % fltleg, print ""%s -> %s"" % (fltleg[""from""].code, fltleg[""to""].code) Prints: [['index', 0], ['serviceClass', '??????'], ['prices', [3504, ... - eTicketing: true - index: 0 - prices: [3504, 114.15000000000001, 89.769999999999996] - segments: [[['indexSegment', 0], ['stopsCount', 0], ['flights', ... - serviceClass: ?????? [['indexSegment', 0], ['stopsCount', 0], ['flights', [[['index', 0], ... - flights: [[['index', 0], ['time', 'PT2H45M'], ['minAvailSeats', 9], ... - indexSegment: 0 - stopsCount: 0 - [['index', 0], ['time', 'PT2H45M'], ['minAvailSeats', 9], ['flight... - - flightLegs: [[['flightNo', '309'], ['eTicketing', 'true'], ['air... - - index: 0 - - minAvailSeats: 9 - - stops: [] - - time: PT2H45M - - [['flightNo', '309'], ['eTicketing', 'true'], ['airplane', 'Boe... - - - airline: ????????? - - - airlineCode: UN - - - airplane: Boeing 737-500 - - - availSeats: 9 - - - classCode: I - - - eTicketing: true - - - fareBasis: IPROW - - - flightClass: ECONOMY - - - flightNo: 309 - - - from: - - [['code', 'DME'], ['airport', '??????????'], ... - - - airport: ?????????? - - - city: ?????? - - - code: DME - - - country: ?????? - - - terminal: - - - fromDate: 2010-10-15 - - - fromTime: 10:40:00 - - - time: - - - to: - - [['code', 'TXL'], ['airport', 'Berlin-Tegel'], ... - - - airport: Berlin-Tegel - - - city: ?????? - - - code: TXL - - - country: ???????? - - - terminal: - - - toDate: 2010-10-15 - - - toTime: 11:25:00 airBaltic BT 425 SVO -> RIX airBaltic BT 425 SVO -> RIX airBaltic BT 423 SVO -> RIX airBaltic BT 423 SVO -> RIX EDIT: fixed grouping and expanded output dump to show how to access individual key fields of results, either by index (within list) or as attribute (within dict). share|improve this answer| follow |||| edited Jul 23 '12 at 16:26 drunkbn 6055 bronze badges answered Aug 30 '10 at 16:47 PaulMcGPaulMcG 51.8k1111 gold badges7979 silver badges116116 bronze badges Wow. +1 for kicking ass and for choosing YAML as output ;) – user395760 Aug 30 '10 at 16:54 Is that YAML? It's just what the dump() method prints out. – PaulMcG Aug 30 '10 at 16:57 Thanks for pyparsing discovery, very useful – DominiCane Sep 2 '10 at 11:30 add a comment |"
410,"
Q: Text correction Damereau Levenshtein python ","0 Well, there exists a ready-made Python package to solve your problem. I think the method distance(string1, string2) should at least solve half your problem. share|improve this answer| follow |||| answered Apr 3 '17 at 10:43 w1n5rxw1n5rx 50144 silver badges1212 bronze badges thanks, but this package doesn't consider transposition – user2807016 Apr 4 '17 at 9:33 add a comment |"
411,"
Q: Read text from images in python ","1 The Google Vision API might help. It is able to pull out what objects are present in an image as well as other information (brands, colors, face detection etc). It can pull out text pretty reliably too. https://cloud.google.com/vision/ Here is some example code from their website using the Python Client Library: import io import os # Imports the Google Cloud client library from google.cloud import vision # Instantiates a client vision_client = vision.Client() # The name of the image file to annotate file_name = os.path.join( os.path.dirname(__file__), 'resources/wakeupcat.jpg') # Loads the image into memory with io.open(file_name, 'rb') as image_file: content = image_file.read() image = vision_client.image( content=content) # Performs label detection on the image file labels = image.detect_labels() print('Labels:') for label in labels: print(label.description) share|improve this answer| follow |||| answered Jul 7 '17 at 10:34 RHSmith159RHSmith159 1,19055 silver badges1010 bronze badges add a comment |"
412,"
Q: Python AES text encryption script ","1 That's normal, because AES (and most modern cryptosystems) is dealing with encrypting the actual byte values, not the ASCII values. With an incorrect key, the data won't be decrypted correctly, resulting in ranges outside of the normal ASCII values. If you're looking for something that encrypts/decrypts ASCII, look into some of the ""classic"" ciphers: Caesar shift, vigenere, etc. share|improve this answer| follow |||| answered Nov 13 '14 at 13:51 Curtis MattoonCurtis Mattoon 3,94322 gold badges1919 silver badges3030 bronze badges 1 Will just add that the right term for the mentioned ""classic"" ciphers is classical/polyalphabetic ciphers. – go2 Nov 13 '14 at 13:57 I used hex ASCII values because in almost every AES encryption references plaintext examples were like this:5e390f7df7a69296a7553dc10aa31f6b So i thought they might be ASCII values. – HBS Nov 13 '14 at 14:02 1 That's because it's content-agnostic, so it deals in bytes (encrypting files, IO streams, etc), plus the references and (good) tutorials are also discussing the algorithm and low-level details. You can use the unhexlify and hexlify functions in the binascii library in Python to convert back and forth. hex_plaintext = binascii.hexlify(message) docs.python.org/3/library/binascii.html – Curtis Mattoon Nov 13 '14 at 14:12 @CurtisMattoon Thank you it was so helpful. Could you recommend some good and high-level detailed references? – HBS Nov 13 '14 at 14:18 add a comment |"
413,"
Q: Python Beautiful Soup crawl text data ","0 Not sure these methods are the best but give them a try: from bs4 import BeautifulSoup as soup from collections import deque input = """"""<div style=""1st level""> <div style=""2nd level"">Here is text 1</div> <div style=""2nd level"">Here is text 2</div> <div style=""2nd level"">Here is text 3</div> <div style=""2nd level"">Here is text 4</div> Here is text 5 and this is the part I want to get. <div>"""""" web_soup = soup(input) reviews = web_soup.find('div', style=""1st level"") print reviews.contents[-2] print deque(reviews.strings, maxlen=1).pop() both prints: Here is text 5 and this is the part I want to get. FYI, I've used deque to get the last element from the strings generator. And, FYI, lxml + xpath would do the job easier by making use of text(). Hope that helps. share|improve this answer| follow |||| answered Jun 18 '13 at 18:58 alecxealecxe 376k9090 gold badges800800 silver badges986986 bronze badges add a comment |"
414,"
Q: Append text in every line of txt in Python ","1 If you just want to append word on each line this works fine file_name = 'YOUR_FILE_NAME.txt' #Put here your file with open(file_name,'r') as fnr: text = fnr.readlines() text = """".join([line.strip() + ': [word]\n' for line in text]) with open(file_name,'w') as fnw: fnw.write(text) But there are many ways to do it share|improve this answer| follow |||| edited Oct 24 '15 at 11:25 answered Oct 24 '15 at 11:15 MarkMark 1461111 bronze badges 1 with is definitely better than just opening and closing. – palsch Oct 24 '15 at 11:25 OTOH it is never a good idea to modify the original file - if you screw up, you have trouble starting over again – jake77 Oct 24 '15 at 13:12 add a comment |"
415,"
Q: how to underline text in python 3.6.5 ","2 In Python, arbitrary unicode characters can be expressed with \uXXXX where XXXX is a four-digit hex number identifying the code point. Wikipedia shows the use of ""combining low line"" (U+0332). Since it's a combining character, you need to place it after each character you want to be underlined. So this code should print aaau̲zzz (u should be underlined in most browsers). print('aaau\u0332zzz') Note that this doesn't seem to work very well. My gnome-terminal (which identifies as GNOME Terminal 3.26.2 Using VTE version 0.50.3 +GNUTLS), using Monospace Regular font, mis-renders the underline on the following character: But if I copy the resulting text and paste it onto Stack Overflow, it seems to render correctly (Chrome on Linux): aaau̲zzz Unless I format it as code: aaau̲zzz In which case it doesn't ""combine"" at all. Here's a screenshot of the above, in case your browser renders it differently: share|improve this answer| follow |||| edited Jun 23 '18 at 17:58 Mark Tolonen 113k1414 gold badges130130 silver badges193193 bronze badges answered Jun 23 '18 at 13:29 Jonathon ReinhartJonathon Reinhart 106k2626 gold badges196196 silver badges266266 bronze badges How do I get the underline to show up under the character though? – Timothy Pulliam Jun 23 '18 at 13:43 Proper rendering likely depends on your terminal, font, etc. – Jonathon Reinhart Jun 23 '18 at 14:25 Interestingly, on Windows and Chrome this answer renders aaauzzz with the u underlined in your copy/paste, but code format it underlines the first z. If I edit your question, the rendering below the edit window is correct for both. Rendering is inconsistent in the same browser, much less different ones :^) – Mark Tolonen Jun 23 '18 at 18:03 Firefox on Windows renders the u̲ in the code block properly imgur.com/a/Kugov8R However Edge renders the underline on the z character – phuclv Jun 24 '18 at 5:02 add a comment |"
416,"
Q: Python Binary? to Text/String ","3 As mentioned above the file is in utf-16. Here's an easy way to open files with encoding: import codecs InFile = codecs.open(path_to_registry, encoding='utf-16') share|improve this answer| follow |||| answered Jun 30 '14 at 18:01 jaimejaime 1,88311 gold badge1111 silver badges1919 bronze badges For future reference, how did you determine the encoding? – James Jun 30 '14 at 19:59 @TechMedicNYC Google :-) – jaime Jun 30 '14 at 20:48 add a comment |"
417,"
Q: Parsing text files using Python ","15 To get you started: result = [] regex = re.compile( r""""""^-*\s+ (?P<name>.*?)\s+ \((?P<email>.*?)\)\s+ (?:changed\s+status\s+from\s+(?P<previous>.*?)\s+to|became)\s+ (?P<new>.*?)\s+@\s+ (?P<date>\S+)\s+ (?P<time>\S+)\s+ -*$"""""", re.VERBOSE) with open(""inputfile"") as f: for line in f: match = regex.match(line) if match: result.append([ match.group(""name""), match.group(""email""), match.group(""previous"") # etc. ]) else: # Match attempt failed will get you an array of the parts of the match. I'd then suggest you use the csv module to store the results in a standard format. share|improve this answer| follow |||| edited Jul 15 '10 at 8:01 answered Jul 15 '10 at 7:52 Tim PietzckerTim Pietzcker 274k5353 gold badges421421 silver badges490490 bronze badges add a comment |"
418,"
Q: read a unicode text file using python [closed] ","0 ""Unicode"" mode on Windows generally means UTF-16LE with a byte-order marker (BOM). If you're on Python 2.X, open the file with codecs.open(filename, encoding='utf-16') as described in the Unicode How-To section on reading Unicode data. If you're on 3.x, you can just use open(filename, encoding='utf-16'). Writing it out again will depend on what encoding you're trying to write to. share|improve this answer| follow |||| edited Feb 4 '15 at 3:16 answered Sep 16 '14 at 21:38 Peter DeGlopperPeter DeGlopper 29k44 gold badges6666 silver badges6969 bronze badges thanks Peter what are text files normally encoded with? – yoshiserry Sep 16 '14 at 21:55 Totally depends on where they come from. utf-8 is fairly common, since it's a superset of ascii. But you have to know what your tools are expecting, if you're looking to write the contents back out. – Peter DeGlopper Sep 16 '14 at 21:57 add a comment |"
419,"
Q: Problems with text file appending in python ","1 I think you should open the file and then CLOSE the file after you append to it: def main() : print(""Hello and welcome to customer email program!"") done = False while not done : email = str(input(""What's your email address? "")) if ""@"" in email : if not email.lower() == email : email = email.lower() done = True f = open(""emails.txt"" ""a"") f.write(email) f.close() else : print(""Please type in a valid email address, ""+email+"" isn't a valid email address"") main() Does this fit your needs? share|improve this answer| follow |||| answered Sep 9 '14 at 19:15 andrewandrew 41866 silver badges1313 bronze badges add a comment |"
420,"
Q: Text file to python dictionary ","0 You can iterate over the file and find id, name pairs: import re file_data = [i.strip('\n').lstrip() for i in open('filename.txt')] line_data = [[int(b) if b.isdigit() else b[1:-1] if b.startswith(""'"") else b for b in re.split(':\s*', i)] for i in file_data if re.findall('[a-zA-Z]+:\s', i)] grouped_data = [line_data[i:i+2] for i in range(0, len(line_data), 2)] last_data = dict([[b[-1] for b in i][::-1] for i in grouped_data]) Output: {'abc': 1, 'def': 2} share|improve this answer| follow |||| edited Mar 23 '18 at 14:35 answered Mar 23 '18 at 14:32 Ajax1234Ajax1234 51.9k44 gold badges3838 silver badges7171 bronze badges add a comment |"
421,"
Q: Text file Trauma in Python ","1 The readline method on a stream (open file) retains the terminating newline ""\n"", so if the first line of configure.txt is test.txt, you will be writing to a file named ""test.txt\n"". Use: testCamp = testCamp.rstrip() (after reading the line) to remove all trailing white space (blanks, tabs, newlines), or equivalently: testCamp = text_file.readline().rstrip() The write method on a stream opened to a non-interactive file (such as ""test.txt"" or ""test.txt\n"") buffers (hangs on to) data. It gets pushed out—""flushed"", as in exposing a bird when hunting (see meaning 3 of the verb form of flush)—by invoking the flush method or by closing the stream. The with context manager will close the stream (as noted in comments and other answers). Use it for reading the configuration file, too (it works for read streams as well as write streams). share|improve this answer| follow |||| edited Aug 11 '13 at 10:45 answered Aug 11 '13 at 10:25 torektorek 264k3232 gold badges343343 silver badges446446 bronze badges That makes sense, I have re-jigged it as you suggest and all seems to be working well. – AimSkyward Aug 16 '13 at 21:08 add a comment |"
422,"
Q: Separate command for clicking each line in Python Tkinter Text Widget ","1 I think you can do something like this. tkHyperlinkManger does it ( http://effbot.org/zone/tkinter-text-hyperlink.htm ) Since you're already coloring the lines differently, I assume you're using tag_config. Then all you need is tag_bind to bind a callback to the region of text. share|improve this answer| follow |||| answered May 15 '12 at 19:36 mgilsonmgilson 239k4646 gold badges486486 silver badges588588 bronze badges add a comment |"
423,"
Q: writing a python dictionary to a text file python ","3 If you want to keep your existing format, for whatever reason, then try this: with open ('wvtc_data.txt', 'w') as fp: for p in main_dic.items(): fp.write(""%s:%s\n"" % p) Note that this doesn't preserve the order of keys. Also, in most cases it's much better to use standard formats for serialization. share|improve this answer| follow |||| answered Oct 10 '13 at 12:44 georggeorg 182k4242 gold badges240240 silver badges332332 bronze badges add a comment |"
424,"
Q: Loop in python from user input ","0 # IMPORT import gtts as gTTS import os # FUNCTIONS def get_answer(prompt): answer = input(prompt) # 1 while not (answer == ""yes"" or answer == ""no"" # 2 while answer not in (""yes"", ""no""): # 3 while answer not in [""yes"", ""no""]: while answer not in (""yes"", ""no""): answer = input(prompt) return answer # TTS while True: myText = input(""Enter your text: "") language = 'en' output = gTTS.gTTS(text=myText, lang=language, slow=False,) output.save(""output.mp3"") os.system(""start output.mp3"") # Restart if get_answer(""quit (yes or no?): "") == yes: break #break out of the loop share|improve this answer| follow |||| answered Feb 17 at 15:49 DittoslashDittoslash 2011 silver badge66 bronze badges add a comment |"
425,"
Q: Read text between two delimiters uisng Python ","-2 So you can use regular expression library of python. Here is the snippet for your query: import re f=open('file') for i in f: st=i txt=re.findall('REPLACE(.*?);',st) print "" "".join(txt) Output: VIEW xyz_REPORTS.D_ACCT AS LOCK ROW FOR ACCESS SELECT CT_ACTIV_CD, CCT_STAT_CD, UM_ACCT_IN, NTRN_ACCT_IN, EW_HSHLD_IN, NTR_HSHLD_IN, CASE WHEN RETL_ACCT_IN = 'N' THEN 'X' ELSE NTR_HSHLD_IN END AS NTR_HSHLD_CD, W_CREAT_TS, W_LOAD_TS FROM I_DATA.D_ACCT_FIN share|improve this answer| follow |||| edited Mar 29 '18 at 8:09 answered Mar 29 '18 at 7:36 Siddharth ManglaSiddharth Mangla 122 bronze badges add a comment |"
426,"
Q: Full text search engine for Python ",7 Have you looked at Whoosh? It's pure Python. share|improve this answer| follow |||| answered Nov 28 '10 at 16:16 nmichaelsnmichaels 41.6k1010 gold badges9292 silver badges120120 bronze badges 2 just thought I would mention this..I recently tried whoosh for 1.5 million rows input data to be indexed..while whoosh was very easy to install and run..it worked fast enough for a smaller index for me and was slow for my index size : 1000 search results in 15 seconds..it may be good for a smaller index size – Abhi Aug 9 '12 at 17:42 add a comment |
427,"
Q: Python Text Cleaning Remove spaces between non alpha numeric characters ","0 Here's a possible solution to your question: import re file = """""" 7 u p, S a k s F i f t h A v e, A u d i A 4, C a n o n A 7 5 """""" print re.sub(r""([A-Za-z0-9])\ *([A-Za-z0-9])\ *"", r""\1\2"", file) I think re.sub is a good way to go here. share|improve this answer| follow |||| answered Aug 31 '16 at 11:11 BPLBPL 8,57255 gold badges2727 silver badges7575 bronze badges x y ## will result in xy##. Anyway, it's not a good idea to encourage questions that show neither research nor effort by spoon feeding them a solution. – spectras Aug 31 '16 at 11:43 @spectras That's true, this solution won't work for your counterexample, nice catch. On the other hand, I don't agree with your point of view of not encouraging these type of low quality questions, why not? why not being proactive and trying to create good answers? – BPL Aug 31 '16 at 11:50 Admittedly, that's my own personal opinion on a recurring controversial point on SO. I don't personally downvote because of that, but I do mention it in a comment every now and then, when question shows especially low effort and answer is very unlikely to be useful to other people. Anyway, either linked meta question is a better place than here to discuss this :) – spectras Aug 31 '16 at 12:04 @spectras Fair enough, in any case your downvote makes sense because the code isn't solving properly the question as you've proved with your counterexample ;) – BPL Aug 31 '16 at 12:06 @user113531> the point being, if you just copy-paste the title of your question in google, you instantly get re.sub as first answer (well, second now). Right in the google preview, without even having to actually open the link. Worth trying before posting the question on SO. – spectras Aug 31 '16 at 22:16 add a comment |"
428,"
Q: News Scrolling Text in Python ","4 Here's a program using Tkinter to scroll text in a box. See 1 and 2 regarding options for label(); see 3 about the after() method. import Tkinter as tk root = tk.Tk() deli = 100 # milliseconds of delay per character svar = tk.StringVar() labl = tk.Label(root, textvariable=svar, height=10 ) def shif(): shif.msg = shif.msg[1:] + shif.msg[0] svar.set(shif.msg) root.after(deli, shif) shif.msg = ' Is this an alert, or what? ' shif() labl.pack() root.mainloop() share|improve this answer| follow |||| answered Dec 15 '12 at 21:20 James Waldby - jwpat7James Waldby - jwpat7 7,81311 gold badge1717 silver badges3535 bronze badges add a comment |"
429,"
Q: python pdf to text convert [closed] ",2 If your PDF contains only images (e.g. from a scanned page) then you won't be able to extract any text. share|improve this answer| follow |||| answered May 24 '11 at 7:47 Andreas JungAndreas Jung 1 actually my pdf contain gre word list text no image is there. Dont knw y its not showing any output. – gur May 24 '11 at 7:53 and as said: provide a reference to the related pdf file – Andreas Jung May 24 '11 at 7:54 actually my pdf contain gre word list text no image is there. Dont knw y its not showing any output. i think there might be something wrong in my code. – gur May 24 '11 at 7:56 1 @Gur: you are repeating yourself - we need a sample document - either you provide it in order to get help or nobody can/will actually help you. – Andreas Jung May 24 '11 at 8:05 1 It is working for me with any PDF document – Andreas Jung May 24 '11 at 8:19 | show 5 more comments
430,"
Q: Python read from text file ","2 How about file=open('IH.txt','r') text="""" for i in range(6): text += file.readLine() print(text) file.close() read() reads the entire file. readline() reads only a line. The loop has 5 iterations and in each iteration, the next line from the file is appended to text via concatenation. You might also want to do exception handling. share|improve this answer| follow |||| edited Oct 22 '17 at 10:14 answered Oct 22 '17 at 9:54 J...SJ...S 4,53711 gold badge1313 silver badges3333 bronze badges 1 for i in range(6): text += file.readline #this works, thank you – UKcodeNovice Oct 22 '17 at 10:08 Actually, that is repeating in each iteration like you say, I just want it to print 5 lines in one go then stop, I later want to print the next 5 lines and stop again. Any idea? – UKcodeNovice Oct 22 '17 at 16:32 @UKcodeNovice What do you want to do after reading the lines? Would it be okay to make the lines as a list? – J...S Oct 22 '17 at 17:05 I want to ask a question about the lines – UKcodeNovice Oct 22 '17 at 17:06 @UKcodeNovice Have a look at islice from itertools package. But even that is a form of loop it seems. – J...S Oct 22 '17 at 17:18 add a comment |"
431,"
Q: python writing to text file options ","1 I would stay away from trying to manipulate the file directly - I am even not sure if this is working. It is much simpler to read in the file and then overwrite the new file - as long as your input file will never exceed your memory: with open('file.txt') as fin: # get lines of the file as list lines = fin.readlines() # Overwrite file.txt with new content with open('file.txt','w') as fout: fout.write('Python\n') for line in lines: fout.write('Test: ' + line.strip() + ' -t\n') If memory is of concern and you don't mind to get a new file: with open('file.txt') as fin: fout = open('newfile.txt','w') fout.write('Python\n') for line in fin: fout.write('Test: ' + line.strip() + ' -t\n') fout.close() share|improve this answer| follow |||| answered Mar 25 '14 at 12:10 oekopezoekopez 79977 silver badges1717 bronze badges add a comment |"
432,"
Q: can I split some text on python? ","0 This would strip away the beggining of the text and then the last of the text. raw_input() or input() could be used to have the user determine what part of the code to be stripped out text = ""let the blue sky meet the blue sea"" bluee = text.split(""the"", 1)[1] skyy = bluee.split(""meet"", 1)[0] print skyy share|improve this answer| follow |||| answered May 29 '19 at 2:59 JROSJROS 6799 bronze badges add a comment |"
433,"
Q: Writing a text file using python ","0 with open('fileName', 'w') as f: f.write(yourString) where >>> yourString = """"""MY_FILE = E:\test.jpg BAND_SUBSET = ( 1 0 0 ) SPATIAL_SUBSET1 = ( 25.0 50.0 ) SPATIAL_SUBSET2 = ( 25.0 50.0 ) PARA1 = ( 0.0 0.0 0.0 0.0 0.0 0.0 ) END = END"""""" You could replace the values by using the following code. with open('fileName', 'w') as f: f.write(yourString.format(band_subset, spatial_subset_1, spatial_subset_2, para1)) where yourString = """"""MY_FILE = E:\test.jpg BAND_SUBSET = {} SPATIAL_SUBSET1 = {} SPATIAL_SUBSET2 = {} PARA1 = {} END = END"""""" Always use the help function or consult The Python Docs. write(...) write(str) -> None. Write string str to file. share|improve this answer| follow |||| edited Jul 24 '13 at 8:19 answered Jul 24 '13 at 8:12 Sukrit KalraSukrit Kalra 24.2k55 gold badges5454 silver badges6767 bronze badges yes, but i have to input band_subset, para1 etc from program, i mean they are defined as variable Sukrit Kalra @Sukrit Kalra – user2536218 Jul 24 '13 at 8:15 How are your variables stored? As Tuples? – Sukrit Kalra Jul 24 '13 at 8:16 as string @Sukrit Kalra – user2536218 Jul 24 '13 at 8:18 thank you , i accepted your answer. could you please explain me the meaning of """""" @Sukrit Kalra – user2536218 Jul 24 '13 at 9:32 add a comment |"
434,"
Q: Character showing up as diamond question mark only at end of line (Python>Text) ","2 As you haven't shared any code snippet I would recommend you a generic way of reading and writing utf-8 files using the codecs module as: # Reading utf-8 encoded file with codecs.open(""in.txt"", ""r"", encoding=""utf-8"") as input_data: data = input_data.read() # Write utf-8 encoded file with codecs.open(""out.txt"", ""w"", encoding=""utf-8"") as output_data: output_data.write(data) And BTW I tested it on the given character だ and it works pretty fine. share|improve this answer| follow |||| answered Jan 23 '17 at 17:48 ZdaRZdaR 16.2k44 gold badges4646 silver badges6262 bronze badges add a comment |"
435,"
Q: Elided or “Hidden” Text in a Python Tkinter Text Widget ","3 Below example produces a Text widget that has elided text in it, using tags: import tkinter as tk root = tk.Tk() text = tk.Text(root) text.pack() text.tag_config('mytag', elide=True) text.insert('end', ""This text is non-elided."") text.tag_add('mytag', '1.13', '1.17') def toggle_elision(): # cget returns string ""1"" or ""0"" if int(text.tag_cget('mytag', 'elide')): text.tag_config('mytag', elide=False) else: text.tag_config('mytag', elide=True) tk.Button(root, text=""Toggle"", command=toggle_elision).pack() root.mainloop() share|improve this answer| follow |||| answered Dec 19 '17 at 4:47 NaeNae 8,09944 gold badges2323 silver badges5353 bronze badges add a comment |"
436,"
Q: How to print blinking text in python ","2 from termcolor import colored, cprint cprint('\nJames Everleigh', 'red', attrs=['blink']) cprint also allows bold, underlined, etc. and a small range of colors. share|improve this answer| follow |||| edited Apr 6 '18 at 22:05 user6655984 answered Apr 6 '18 at 22:00 Kenneth BruinsslotKenneth Bruinsslot 2122 bronze badges add a comment |"
437,"
Q: Splitting text file in python ","2 Quotation marks are how python demarcates strings. The actual object string does not contain the quotations marks. Try printing each string with print and you'll see those quotes disappear. The other thing to note is the trailing newline in your lines, because file lines are read in with the trailing newline by default, so you'll need to call str.strip() to get rid of it. I'd recommend iterating over f, the file object, so doing this becomes easier. f_contents = [] with open (""testpractice01.txt"" , ""r"") as f: for line in f: f_contents.append(line.strip()) for line in f_contents: print(line) share|improve this answer| follow |||| answered Oct 15 '17 at 18:07 cs95cs95 203k4545 gold badges323323 silver badges392392 bronze badges add a comment |"
438,"
Q: Text Shift function in Python ","8 Looks you're doing cesar-cipher encryption, so you can try something like this: strs = 'abcdefghijklmnopqrstuvwxyz' #use a string like this, instead of ord() def shifttext(shift): inp = raw_input('Input text here: ') data = [] for i in inp: #iterate over the text not some list if i.strip() and i in strs: # if the char is not a space """" data.append(strs[(strs.index(i) + shift) % 26]) else: data.append(i) #if space the simply append it to data output = ''.join(data) return output output: In [2]: shifttext(3) Input text here: how are you? Out[2]: 'krz duh brx?' In [3]: shifttext(3) Input text here: Fine. Out[3]: 'Flqh.' strs[(strs.index(i) + shift) % 26]: line above means find the index of the character i in strs and then add the shift value to it.Now, on the final value(index+shift) apply %26 to the get the shifted index. This shifted index when passed to strs[new_index] yields the desired shifted character. share|improve this answer| follow |||| edited Sep 7 '16 at 17:48 answered Jan 20 '13 at 12:45 Ashwini ChaudharyAshwini Chaudhary 199k4343 gold badges378378 silver badges425425 bronze badges @AmpiSevere what was your input? – Ashwini Chaudhary Jan 20 '13 at 13:00 A shifted (ununderstandble) piece of text consisting of words, spaces and ending with a period and one apostroph in there by the way. – Ampi Severe Jan 20 '13 at 13:13 @AmpiSevere I've fixed the code to handle such characters(i.e .,;"") as well. – Ashwini Chaudhary Jan 20 '13 at 13:20 Okay, no error but now I get no return, and after the function definition I actually call it. I've had this issue almost on everything, I don't understand where it comes from. – Ampi Severe Jan 20 '13 at 13:30 @AmpiSevere try calling the function like this: print shifttext(3) – Ashwini Chaudhary Jan 20 '13 at 13:31 | show 4 more comments"
439,"
Q: Sublime text python build different result ","0 I tried this in my terminal: a=1000 b=1000 a==b True a is b True the Python is operator has funny, sometimes undefined functionality when dealing with integers. I suspect the mismatch above is due to Python trying to do an optimization in the Sublime case (and my terminal) and thus the objects are actually the same whereas the other case it's saving them as two separate variables. You should NOT use the is operator to do integer comparison, but rather ==. Another good reason == is suggested for comparison (while no longer integer comparison) is the following case: a=1000 b=1000.0 a==b True a is b False share|improve this answer| follow |||| answered Feb 8 '17 at 1:12 Daniel DobalianDaniel Dobalian 2,71122 gold badges99 silver badges2323 bronze badges add a comment |"
440,"
Q: Python: normalizing a text file ","3 You could read the synonym file and convert it into a dictionary, table: import re table={} with open('synonyms','r') as syn: for line in syn: match=re.match(r'(\w+)\s+=\s+(.+)',line) if match: primary,synonyms=match.groups() synonyms=[synonym.lower() for synonym in synonyms.split()] for synonym in synonyms: table[synonym]=primary.lower() print(table) yields {'word3_1': 'word3', 'word3_3': 'word3', 'word3_2': 'word3', 'contr': 'contracting', 'contract': 'contracting', 'contractor': 'contracting', 'contra': 'contracting', 'identify': 'identification', 'contractors': 'contracting', 'word3_n': 'word3', 'ID': 'identification'} Next, you could read in the text file, and replace each word with its primary synonym from table: with open('textfile','r') as f: for line in f: print(''.join(table.get(word.lower(),word) for word in re.findall(r'(\W+|\w+)',line))) yields identification identification identity contracting contracting contracting medicine medicine medicine re.findall(r'(\w+|\W+)',line) was used split each line while preserving whitespace. If whitespace is not of interest, you could also use the easier line.split(). table.get(word,word) returns table[word] if word is in table, and simply returns word if word is not in the synonym table. share|improve this answer| follow |||| edited Sep 11 '11 at 10:52 answered Sep 10 '11 at 21:55 unutbuunutbu 635k125125 gold badges14101410 silver badges14151415 bronze badges Whitespace splitting will append trailing punctuation - for example ""Show me your ID."" if split on whitespace, won't give the nice clean ""ID"" string to convert to ""identification"". Upper/lower case will need to be handled too. – PaulMcG Sep 11 '11 at 7:36 1 @Paul McGuire: Thanks for the comment. I changed \s+|\S+ to \w+|\W+ to separate punctuation from words, and added code to handle case. @Pradeep: These changes have unlikely but possibly problematic consequences: words with punctuation (like can't) in the synonym list will no longer match, and words whose meaning changes with case (Polish is a nationality, but polish is a verb) may get replaced by the same synonym. These issues can be handled with more code, but let's not do that unless it affects your situation. – unutbu Sep 11 '11 at 11:47 add a comment |"
441,"
Q: how to put text btween html tag with python webdriver ","1 You can use selenium to get the element right before you want to add the text, then use javascript to insert the text immediately after the end of that element. For example: node = driver.find_element_by_xpath(""//span[@class='py']"") script = ""arguments[0].insertAdjacentHTML('afterEnd', arguments[1])"" driver.execute_script(script, node, ""the new text"") Here is a complete working example, assuming that the website has at least one div (which it does at the time I write this): from selenium import webdriver driver = webdriver.Firefox() driver.get(""http://www.example.com"") node = driver.find_element_by_xpath(""/html/body/div"") script = ""arguments[0].insertAdjacentHTML('afterEnd', arguments[1])"" driver.execute_script(script, node, ""the new text"") share|improve this answer| follow |||| answered Jun 1 '16 at 19:45 Bryan OakleyBryan Oakley 264k2828 gold badges362362 silver badges520520 bronze badges add a comment |"
442,"
Q: Python / Selenium - Not able to extract text from text box ","0 Looking at the id as landingPageDataForm_attributeMap_STRUCTURE_DATA_REQUIRED and name as attributeMap.STRUCTURE_DATA_REQUIRED it seems both the attributes are dynamically generated. Hence we need to construct a dynamic xpath or css to get all the WebElements first. We need to store the WebElements in a List then traverse the List of WebElements to retrive the value of value field which is either TRUE or FALSE as follows: PC = driver.find_elements_by_xpath(""//input[contains(@id, 'landingPageDataForm')][@class='text medium']"") for x in PC: text = x.getText(); print(text) share|improve this answer| follow |||| edited Aug 5 '17 at 10:54 answered Aug 5 '17 at 10:23 DebanjanBDebanjanB 79k2121 gold badges9696 silver badges147147 bronze badges Tried using the xpath given, It is extracting other text box since given xpath is wrong.. – Abdul Aug 5 '17 at 10:51 but its not working.. I have tried several different combination of xpaths including yours.. Its not throwing any error.. but not getting desired output too – Abdul Aug 5 '17 at 21:35 add a comment |"
443,"
Q: Python selenium: Get dynamic update of text of an element [duplicate] ","1 You can potentially run into stale element exceptions in addition to what you are already seeing. I suggest that you find the element each time you want to read the text. This will ensure that you have an active element and should not run into the above exception. while True: text_status = WebDriverWait(driver,TIMEOUT).until(EC.presence_of_element_located((By.CLASS_NAME, ""text--34J_5 uppercase--ii2bO font-small--19CMC""))) print(text_status.text) share|improve this answer| follow |||| answered Mar 25 '19 at 22:27 RKelleyRKelley 1,03677 silver badges1212 bronze badges That is possible. But that is not a problem at the moment. Unfortunately, your answer does not help me to solve my problem. Thanks anyway for your efforts. – Lukas Nothhelfer Mar 25 '19 at 22:51 add a comment |"
444,"
Q: Extract from text with python and regex ","1 If your string is following the consistent format of user at the start of a line and double newlines ending a quote, you could use this: (?m)^[^:\n]+:\s?((?:.+\n?)*) It uses multiline mode and matches the start of a line, followed by characters that are neither : nor newline, folllowed by :. Then captures all following lines with content. Here's a demo on regex101. share|improve this answer| follow |||| answered Nov 19 '16 at 20:36 Sebastian ProskeSebastian Proske 7,69522 gold badges2424 silver badges3434 bronze badges add a comment |"
445,"
Q: Using python and spacy text summarization ",0 You have to create a RESTFUL Api using FLASK or DJAngo with some UI elements and call your model. Also you can use displacy( Spacy UI Bro) Directly on your system. share|improve this answer| follow |||| answered Dec 4 '19 at 11:43 SyenixSyenix 17177 bronze badges So first i have to create REST api using FLASK after that i can call that model. right? and using displacy i don't know how to do if you have any idea how to do than please provide me some links . – deval LazzyDevs Dec 4 '19 at 11:55 yes you are right using desplacy it's working...i am trying and little it's showing output....thank you so much. – deval LazzyDevs Dec 4 '19 at 12:15 add a comment |
446,"
Q: UnicodeEncodeError only with str(text) in Python ","1 Because message (and msg.text) are not str but unicode objects. To call str() you need to specify utf-8 as the encoding again. Your __str__ method should look like: def __str__(self): return self.text.encode('utf-8') unicode can be implicitly encoded to str if it contains only ASCII characters, which is why you only see the error when the input contains an umlaut. share|improve this answer| follow |||| answered Mar 27 '15 at 20:35 rezcarezca 9666 bronze badges When i change it, the umlauts will not be printed. They will be printed, if I also change the print line to str(msg).decode('utf-8'). That's a strange way. – ebaharilikult Mar 27 '15 at 20:44 It worked for me, but my terminal encoding is UTF-8. Maybe yours isn't? Your extra .decode('utf-8') is converting it back to unicode, which would then get encoded properly for your terminal. Is there a reason for not using unicode (or Python 3)? str == bytes in Python 3, which means if you use it for text you're implicitly assuming ASCII encoding (which breaks when you have non-ASCII text). – rezca Mar 27 '15 at 20:50 add a comment |"
447,"
Q: Manage python packages in Sublime text 3 ","0 You need to point SublimeRepl to the right interpreter. Add to Settings -> Package Settings -> SublimeREPL -> Settings - User { ""default_extend_env"": {""PATH"": ""{PATH}:/usr/lib/python2.7""} } share|improve this answer| follow |||| answered Jul 16 '15 at 9:18 RailslideRailslide 4,43122 gold badges2525 silver badges3333 bronze badges add a comment |"
448,"
Q: XPath taking text with hyperlinks (Python) ",0 The links themselves are nodes that you need to descend. /html/body/div[3]/div[3]/div[4]/div/p[1]//text() share|improve this answer| follow |||| answered Jul 16 '18 at 3:46 Ignacio Vazquez-AbramsIgnacio Vazquez-Abrams 645k118118 gold badges11581158 silver badges12341234 bronze badges add a comment |
449,"
Q: Textbox for Output in Python (Text-Based Game) ","0 You can create a handler for your textbox such that: class textbox_handler: def __init__(self, text): self.data = [] self.text = text #text is your tk text widget. def write(self, s): self.data.append(s) def print_out(self): for line in data: self.text.insert('end',line) Then, in your main function: import sys def main(): handler = textbox_handler(text1) sys.stdout = handler print ""sample text."" print ""sample text #2."" print ""another sample text."" #or you can use without setting sys.stdout print >>handler, ""yet another sample text."" Each print statement calls write method of the handler object. You can simply retrieve those strings from handler.data. share|improve this answer| follow |||| answered Jun 16 '12 at 20:44 HgegHgeg 52566 silver badges1717 bronze badges add a comment |"
450,"
Q: Python canvas text position changes with font ","1 By default, the x,y represents the center point of the text. If you make the font larger or smaller, the center remains the same but the edges will change depending on the size of the text. Add anchor='nw' to the create_text command to have the coordinates reoresent the upper-left (northwest) corner of the text. share|improve this answer| follow |||| answered Oct 21 '16 at 12:01 Bryan OakleyBryan Oakley 264k2828 gold badges362362 silver badges520520 bronze badges add a comment |"
451,"
Q: Split string in python. Many text lines ","1 Use a for-loop. This would return one line at a time from the file(no need to store all lines in memory) and you can apply strip, split on it. with open(""text_file.txt"") as inputFile: for line in inputFile: var1, var2 = line.strip().split("","") share|improve this answer| follow |||| answered Nov 13 '13 at 6:47 Ashwini ChaudharyAshwini Chaudhary 199k4343 gold badges378378 silver badges425425 bronze badges add a comment |"
452,"
Q: Output text from PowerShell, read into Python ","3 Your problem is the encoding of the redirect operators is unicode (UTF16-LE). $CS = Get-CimInstance -ClassName CIM_ComputerSystem $OS = (Get-CimInstance -ClassName Win32_OperatingSystem).Caption $CPU = Get-CimInstance -ClassName Win32_Processor @"" $($CS.Manufacturer) $($CS.Model) $OS $($CPU.Name) $($CPU.NumberOfCores) $($CPU.NumberOfLogicalProcessors) $($CPU.ThreadCount) $($CPU.MaxClockSpeed) $($CPU.CurrentClockSpeed) ""@ | Out-File -FilePath C:\Mike\Output\SysConfig.txt -Encoding UTF8 Cleaned up: $CS = Get-CimInstance -ClassName CIM_ComputerSystem $OS = (Get-CimInstance -ClassName Win32_OperatingSystem).Caption $CPU = Get-CimInstance -ClassName Win32_Processor ""{0}`n{1}`n{2}`n{3}`n{4}`n{5}`n{6}`n{7}`n{8}"" -f @( $CS.Manufacturer $CS.Model $OS $CPU.Name $CPU.NumberOfCores $CPU.NumberOfLogicalProcessors $CPU.ThreadCount $CPU.MaxClockSpeed $CPU.CurrentClockSpeed ) | Out-File -FilePath C:\Mike\Output\SysConfig.txt -Encoding UTF8 share|improve this answer| follow |||| edited Dec 28 '17 at 19:24 answered Dec 22 '17 at 16:48 Maximilian BurszleyMaximilian Burszley 13.8k33 gold badges2222 silver badges4141 bronze badges add a comment |"
453,"
Q: Read and search text file in Python? ","2 This may do the trick read_file = open(""file.txt"", ""r"") data = read_file.read() read_file.close() file_content = re.sub(""\d+"", """", data) word = your_word if word in file_content: newfile = open(""newfile.txt"", ""w"") print >> newfile, word newfile.close() share|improve this answer| follow |||| answered Aug 19 '13 at 19:22 Βασιλης ΙωσηφιδηςΒασιλης Ιωσηφιδης 75966 silver badges2020 bronze badges add a comment |"
454,"
Q: Python Text Widget - Displays Nothing on GUI ","0 I'm pretty sure in order to display a GUI you need: root = tkinter.Tk() root.title(""YOUR TITLE HERE"") And then you need to use pack() after whatever you want to be displayed on the GUI and once that's done you need root.mainloop() to display the GUI altogether. share|improve this answer| follow |||| answered Oct 10 '15 at 10:03 InkblotInkblot 44111 gold badge66 silver badges1414 bronze badges add a comment |"
455,"
Q: Parsing text file in python ","2 Regex does the trick if you are needing to replace any text between ""[%"" and ""%]"". The code would look something like this: import re newstring = re.sub(""\[%.*?%\]"",newtext,oldstring) The regex used here is lazy so it would match everything between an occurrence of ""[%"" and the next occurrence of ""%]"". You could make it greedy by removing the question mark. This would match everything between the first occurrence of of ""[%"" and the last occurrence of ""%]"" share|improve this answer| follow |||| answered Apr 5 '10 at 20:38 objectiveseaobjectivesea 57711 gold badge66 silver badges2121 bronze badges add a comment |"
456,"
Q: python PIL draw multiline text on image ","46 You could use textwrap.wrap to break text into a list of strings, each at most width characters long: import textwrap lines = textwrap.wrap(text, width=40) y_text = h for line in lines: width, height = font.getsize(line) draw.text(((w - width) / 2, y_text), line, font=font, fill=FOREGROUND) y_text += height share|improve this answer| follow |||| edited Apr 17 '15 at 9:50 answered Oct 8 '11 at 16:46 unutbuunutbu 635k125125 gold badges14101410 silver badges14151415 bronze badges 1 Thanks a lot! just copy and paste and it works like a charm. You are the best :) – user985541 Oct 8 '11 at 16:55 What does 40 represent? – User Apr 17 '15 at 2:21 2 @User 40 represents maximum characters. Meaning it will allow a maximum or 40 characters before it wraps to a new line. But if one word is 10 characters and then next is 31, it will wrap right after the first word since it cannot fit the first and second word on the line. – teewuane Sep 3 '15 at 8:25 1 @teewuane how about doing this based on pixels? We are not always dealing with monospace fonts – User Nov 21 '17 at 23:22 1 @User If you want to get the size of an string of text you can use PIL.ImageDraw.ImageDraw.textsize which returns a tuple of (width, height) in pixels. You could use this implement a solution that tries the longest possible string before the width is over some threshold – Seabass77 Feb 26 '19 at 21:38 add a comment |"
457,"
Q: Python text parsing and saving as html ","1 It's a matter of indenting the lines to the right level. The HTML footer must be printed at the indentation level of the header lines, not indented within the loop. Try this: import sys, os, glob import cgi for filename in glob.glob(""./*.LOG""): name = os.path.splitext(filename)[0] with open(filename, 'r') as logFile, open('%s_ERROR!.html' % name, 'w') as outfile: outfile.write(""<html>\n<head>\n</head>\n<body><p>"") allLines = logFile.readlines() printList = [] for line in allLines: if ('ERROR' in line) or ('error' in line): printList.append(line) for item in printList: # Note: HTML-escape value of item outfile.write(cgi.escape(item) + '<br>') outfile.write(""</p></body>\n</html>"") Note that you don't need to use printList - you could just emit the HTML code as you go through the log. Consider breaking this into smaller functions for reusability and readability. share|improve this answer| follow |||| edited Aug 2 '16 at 23:38 answered Aug 2 '16 at 11:22 OwenOwen 1,4871010 silver badges1414 bronze badges Hi thank for the help, unfortunately I'm not quite there yet. I was getting an EOL related error which was fixed by correcting the quotation on the last line, but now I'm getting an error on the os.path line saying logfile is not defined(understandable), I then tried splitting line 7 and putting ""with open(filename, 'r') as logFile"" before ""name+os.path..."" but that in turn throws an error on the remaining of the line; ""open(name + '_ERROR!.html', 'w') as outfile:"" as invalid syntax =/ – Skyone Aug 2 '16 at 14:02 Fixed the error (sorry about that) - please try the updated answer. – Owen Aug 2 '16 at 23:40 Brilliant thank you! Now I just have to figure out how to wrap the keywords in the correct tags so theyre a different colour. Side question; Is something like this; <font color = ""XXXXXX"">keyword</font> the best way to go or should I go with css. Thanks again. – Skyone Aug 3 '16 at 10:50 Use CSS if you can. – Owen Aug 3 '16 at 10:52 add a comment |"
458,"
Q: Python, cannot open a text file ","1 The result would be empty string not empty list and it's because your file size is larger than your memory(based on your python version and your machine)! So python doesn't assigned the file content to a variable! For getting rid of this problem you need to process your file line by line. with open('/home/user/android.txt') as f : for line in f: #do stuff with line share|improve this answer| follow |||| edited May 23 '17 at 10:26 Community♦ 111 silver badge answered Jun 29 '15 at 7:26 KasramvdKasramvd 87.2k1010 gold badges120120 silver badges147147 bronze badges 2 there is not such thing as ""python string object capacity"". You might get MemoryError but you won't get an empty string just because a file is too large to fit in memory. – jfs Jun 29 '15 at 8:35 @J.F.Sebastian Indeed.I just wanted to make it understandable for OP. Actually it's the memory and can be used for any object! – Kasramvd Jun 29 '15 at 8:46 add a comment |"
459,"
Q: Python catch text inside {} ","2 You can use a combination of capture groups and findall to extract all needed properties >>> import re >>> s = ""{NOKIA}_B {Lumia 640 XL}_M {LTE}_O {8GB}_S"" >>> matches = re.findall(r""\{([0-9a-zA-Z ]*)\}"", s) >>> print matches ['NOKIA', 'Lumia 640 XL', 'LTE', '8GB'] share|improve this answer| follow |||| answered Jun 29 '15 at 15:06 KetouemKetouem 3,2221515 silver badges2626 bronze badges add a comment |"
460,"
Q: Parse Text in Python (Django) ","1 You can simply use Python Regexp: import re text = ""Link(base_url=u'http://www.bing.com/search?q=site%3Asomesite.com', url='http://www.somesite.com/prof.php?pID=478', text='SomeSite - Professor Rating of Louis Scerbo', tag='a', attrs=[('href', 'http://www.somesite.com/prof.php?pID=478'), ('h', 'ID=SERP,5105.1')])Link(base_url=u'http://www.bing.com/search?q=site%3Asomesite.com', url='http://www.somesite.com/prof.php?pID=527', text='SomeSite - Professor Rating of Jahan \xe2\x80\xa6', tag='a', attrs=[('href', 'http://www.somesite.com/prof.php?pID=527'), ('h', 'ID=SERP,5118.1')])Link(base_url=u'http://www.bing.com/search?q=site%3Asomesite.com', url='http://www.somesite.com/prof.php?pID=645', text='SomeSite - Professor Rating of David Kutzik', tag='a', attrs=[('href', 'http://www.somesite.com/prof.php?pID=645'), ('h', 'ID=SERP,5131.1')])"" # Create the regexp object to match the value of 'url' r = re.compile("" url='(.*?)',"", ) # Print all matches print r.findall(text) >>>['http://www.somesite.com/prof.php?pID=478', 'http://www.somesite.com/prof.php?pID=527', 'http://www.somesite.com/prof.php?pID=645'] share|improve this answer| follow |||| answered Aug 9 '13 at 1:27 Thierry J.Thierry J. 1,7751111 silver badges2121 bronze badges Thank you for the answer. Can you see my updated code? Is there someway to set my 'domainLinkOutputAsString' as a string? It appears its looking for a string or buffer. – CodeTalk Aug 9 '13 at 2:02 1 __str__ is a function. You should replace domainLinkOutputAsString = domainLinkOutput.__str__ by domainLinkOutputAsString = domainLinkOutput.__str__() or even better by domainLinkOutputAsString = str(domainLinkOutput) – Thierry J. Aug 9 '13 at 2:06 I think r = re.compile("" url='(.*?)',"", ) is wrong, because now I am getting no output . I updated my code to make sure I am not doing anything wrong. Could I just use a regular expression like: '^[a-zA-Z0-9]+\.[a-zA-Z]{1,4}$' to match somehow? – CodeTalk Aug 9 '13 at 2:13 What is the output of print domainLinkOutputAsString ? – Thierry J. Aug 9 '13 at 2:15 It outputs nothing, but page source shows: <generator object _filter_links at 0x10273e2d0> – CodeTalk Aug 9 '13 at 2:19 | show 3 more comments"
461,"
Q: Python - Text to List but not as strings ","1 You can use ast.literal_eval: import ast pObjects = map(ast.literal_eval, pObjects) share|improve this answer| follow |||| answered Nov 24 '17 at 2:34 Ajax1234Ajax1234 51.9k44 gold badges3838 silver badges7171 bronze badges pObjects = map(ast.literal_eval, pObjects) was giving me an error but I did get it working using regular eval(), pObjects.append(eval(line)) – Xydis Nov 24 '17 at 2:46 add a comment |"
462,"
Q: formating text, rows to columns in python ","0 I am assuming that you have stored the following content in file: key_Value.txt scannerId '00000000 00000000 00000001'B, hour 7, minute 4, second 38, millisec 238, events internal-soho-ds-missing-call : { ue-context valid : 3045, rnv-module-id valid : 22, c-id-1 valid : 12623, rnv-id-1 valid : 2, event-trigger valid : event-value-event-1a (15), action valid : event-value-no-action (11), scrambling-code-cell-1 valid : 446, cpoid-ec-no-cell-1 valid : 29, rsdd-cell-1 valid : 12, scrambling-code-trigger-cell valid : 2, cpoid-ec-no-trigger-cell valid : 23, rsdd-trigger-cell valid : 9, } this should solve your problem: import string intermediate="""" with open(""key_Value.txt"", 'r') as fin: for line in fin: intermediate+=line.strip() + ""\n"" i = intermediate.splitlines() heading = [] values = [] splitstring = "" "" for s in i: if ""{"" in s: splitstring = "" : "" if not ((""{"" in s) or (""}"" in s)): try: s=string.replace(s,"","","""") key = s.split(splitstring,1) key[1]=string.replace(key[1],"" : "","""") key[1]=string.replace(key[1],"" :"","""") heading.append(key[0]) values.append(key[1]) except: pass head="""" for h in heading: head+=h+""\t"" val="""" for v in values: val+=v+""\t"" print head print val Hope this helps. share|improve this answer| follow |||| edited Dec 27 '14 at 11:30 answered Dec 27 '14 at 2:49 Ganesh Kamath - 'Code Frenzy'Ganesh Kamath - 'Code Frenzy' 4,1593434 silver badges4545 bronze badges add a comment |"
463,"
Q: How to open a text file with Python GUI? [closed] ","0 The open() function works the same whether or not you also use tkinter. The difference is how you ask a user for a filename, if that is necessary. (And if the filename is either hard-coded or retrieved from sys.argv, it will not be.) In 3.x, you might use fname = input(""File name? "") # raw_input in 2.x With tkinter from tkinter.filename import askopenfilename as ask fname = ask() # you might want to pass a beginning directory With tkinter, you can combine the ask and open steps with from tkinter.filename import askopenfile as ask ofile = ask() # you might want to pass a beginning directory but you will then not know what filename the user gave, which you might want to know so it can be saved. share|improve this answer| follow |||| answered Jan 2 '15 at 21:53 Terry Jan ReedyTerry Jan Reedy 13.9k11 gold badge2727 silver badges4545 bronze badges add a comment |"
464,"
Q: Set up Python 3 build system with Sublime Text 3 ","77 The reason you're getting the error is that you have a Unix-style path to the python executable, when you're running Windows. Change /usr/bin/python3 to C:/Python32/python.exe (make sure you use the forward slashes / and not Windows-style back slashes \). Once you make this change, you should be all set. Also, you need to change the single quotes ' to double quotes "" like so: { ""cmd"": [""c:/Python32/python.exe"", ""-u"", ""$file""], ""file_regex"": ""^[ ]*File \""(...*?)\"", line ([0-9]*)"", ""selector"": ""source.python"" } The .sublime-build file needs to be valid JSON, which requires strings be wrapped in double quotes, not single. share|improve this answer| follow |||| answered May 19 '14 at 15:11 MattDMoMattDMo 83.2k1717 gold badges195195 silver badges200200 bronze badges Thank you so much! This was causing me so much trouble. My apologies again for freaking out before, that was immature. – user3002473 May 19 '14 at 15:29 there's one more difference in the question vs this answer (which solved my problem+thanks); in the question, ""..."" is a character all on it's own, instead of 3 . characters. – emmagras Feb 7 '15 at 21:46 1 @shuboy2014 As I stated in my answer, you need to use forward slashes as path delimiters. After changing that, there's no reason why your path wouldn't work, as long as that is where Python is installed. – MattDMo Oct 21 '16 at 17:26 add a comment |"
465,"
Q: python submit html form (sublime text plugin) ","1 You need to install the Requests library. For example: pip install requests share|improve this answer| follow |||| answered Apr 27 '14 at 16:41 TNTTNT 2,3921414 silver badges2222 bronze badges 1 I don't think so I'll try to create sublime text plugin and it must work without install something like that – David Apr 27 '14 at 16:44 1 What don't you think? If you want to import requests you need to have it. – TNT Apr 27 '14 at 16:56 no, I need to use something already I have (sublime text have) – David Apr 27 '14 at 17:15 This answer shows a solution without extra libraries: stackoverflow.com/a/681182/1547831 (Python2 only) – TNT Apr 27 '14 at 17:36 add a comment |"
466,"
Q: Python. Comparing two text document's contents ","0 If you just want to compare if the two files contain the same text, you can just compare them as a whole, instead of a line by line basis: filehandle1.read() == filehandle2.read() Also dont forget to close your files after opening them. share|improve this answer| follow |||| answered Jan 14 '15 at 3:40 MarcinMarcin 32.3k44 gold badges5757 silver badges8686 bronze badges add a comment |"
467,"
Q: Python Memory Error reading Text - Fastest solution? ","0 If you are reading a large file and then storing the lines in arrays, then you are actually doubling the required memory size. One source can be if you are using line = input.readlines(). If that is the source of the problem, you can replace that with this: for item in input: function(item) that iterates over each line. Also consider using the csv library if your text file is a CSV. (source) share|improve this answer| follow |||| edited May 23 '17 at 10:31 Community♦ 111 silver badge answered Feb 27 '14 at 7:48 philshemphilshem 20.3k55 gold badges4242 silver badges102102 bronze badges I think you will need to add some code and input to totally diagnose the issue. Also, consider posting some timing results for what you have tried. – philshem Feb 27 '14 at 8:31 Each file takes about +- 3 min to process (when iterating over lines). +- 4 million lines per text file. – Coryza Feb 27 '14 at 9:20 How do you read lines without iterating over lines?If you are using linux, you might find it faster to parse with grep or awk first, into smaller files, and then read those files as needed. – philshem Feb 27 '14 at 9:33 add a comment |"
468,"
Q: Reading text file into python ","2 You are probably looking for something like the Counter collection which is a very efficient dictionary for counting hashable objects, such as words. See, How to read large file, line by line in python for an explanation of why readlines is not a good approach for large files, while the approach listed in the link, and below, turns the file into an iterable which is more memory efficient. You didn't specify your file sizes, but text analysis often deals with huge files, so that is probably worth mentioning. Putting these two together, you could do something like this. from collections import Counter c=Counter() with open('Reviews') as f: for line in f: for word in line.split("" ""): c[word]+=1 EDIT: you might want to split on *** or something else, but this gives the general idea. share|improve this answer| follow |||| edited May 23 '17 at 12:02 Community♦ 111 silver badge answered Aug 5 '14 at 21:55 John PowellJohn Powell 10.5k66 gold badges4747 silver badges6161 bronze badges add a comment |"
469,"
Q: Python text file Excel, inserting number into excell not in correct format ","0 You can use the int method to convert the string number to an integer before saving the xlsx file. Ex: from openpyxl import load_workbook wb=load_workbook('template.xlsx') ws=wb.active ws['A11']=int(""1"") #ws['F22']=int(m5F[0]) wb.save('template.xlsx') share|improve this answer| follow |||| answered Feb 9 '18 at 7:29 RakeshRakesh 64.3k1515 gold badges5050 silver badges8484 bronze badges Please accept ans(tick symbol) if it solved your problem. – Rakesh May 7 '19 at 3:56 add a comment |"
470,"
Q: python pygubu tkinter text scrollbar ","0 I set it manually and it works. With scrollbar is this problem, that you have to first create scrollbar and widget, and after that you can assign one to another. You can't assign scrollbar to widget when you create widget and scrollbar doesn't exist yet. And you can't assign widget to scrollbar when you create scrollbar and widget doesn't exist yet. So you have to first create one object without assigning to second object. Next you can create second object with assigning to first object. And after that you can assign first object to second object. It can be more readable to create first both objects and after that assign one to another. import tkinter as tk import pygubu class Application: def __init__(self, master): self.builder = builder = pygubu.Builder() builder.add_from_file('mainwindow.ui') self.mainwindow = builder.get_object('mainWindow', master) # get objects self.logBox = builder.get_object('logBox', master) self.yscrollbar = builder.get_object('yscrollbar', master) # assign scrollbar to text widget #<property name=""yscrollcommand"">yscrollbar.set</property> #<property name=""command"">logBox.yview</property> self.logBox['yscrollcommand'] = self.yscrollbar.set self.yscrollbar['command'] = self.logBox.yview # set some text for test for x in range(20): self.logBox.insert('end', '\nHello World!') if __name__ == ""__main__"": root = tk.Tk() app = Application(root) root.mainloop() share|improve this answer| follow |||| answered Feb 1 '17 at 20:34 furasfuras 71.6k77 gold badges5353 silver badges8383 bronze badges add a comment |"