[Solved] How can I use python to add index numbers for the data saved in the text file [duplicate]

[ad_1] You are looking for the enumerate function: https://docs.python.org/2/library/functions.html#enumerate For instance: with open(“newfile.txt”, “w”) as output: for index, line in enumerate(open(“myfile.txt”)): output.write(“%d %s” % (index + 1, value)) 1 [ad_2] solved How can I use python to add index numbers for the data saved in the text file [duplicate]

[Solved] Not sure why If Statement is not working. (Pygame)

[ad_1] Surfaces doesn’t have an object for retrieving the file name, but maybe you could use another variable to hold the file name? #load images bg_filename = “start.png” background = pygame.image.load(bg_filename) #keep looping through while 1: #clear the screen before drawing it again screen.fill(0) #draw the screen elements screen.blit(background, (0,0)) #update the screen pygame.display.flip() #loop … Read more

[Solved] python 3 Acess file and check number of time phrase comes up [closed]

[ad_1] Assuming that the count result applies to the whole file you can use a collections.Counter: from collections import Counter with open(‘input.tsv’) as infile: counts = Counter(infile.read()) for c in ‘SF’: print ‘{}: {}’.format(c, counts.get(c)) This has the advantage of allowing you to obtain counts of any character (not just “S” and “F”) with one … Read more

[Solved] Python Machine Learning

[ad_1] Maybe do some Exploratory data analysis first to see if you can figure out a pattern between your target variable and features? It would also be good to extract some features from your date/time variables rather than using them as integers (like weekday_or_not, seasons etc.) You can also try transforming your features (log, sqrt) … Read more

[Solved] Open and read all text Files in your directory and filter them using regular expression, python

[ad_1] This is using glob rather than listdir, but this might be a possible method. No regular expressions involved here either though. import glob folder_path = “C:\Temp” file_pattern = “\*.txt” search_string = “hello” match_list = [] folder_contents = glob.glob(folder_path + file_pattern) for file in folder_contents: print(“Checking”, file) read_file = open(file, ‘rt’).read() if search_string in read_file: … Read more

[Solved] Python GUI – Calculator drop-down menu [closed]

[ad_1] It took some effort, but here goes: from tkinter import * from tkinter.ttk import * def left_side(): “””Left “”” global left_entry, right_entry, answer_label, integer_combo left_entry.get() def right_side(): “””Right””” global left_entry, right_entry, answer_label, integer_combo right_entry.get() def combo_calc(): “””Combobox basic Calculator””” global left_entry, right_entry, answer_label, integer_combo if integer_combo.get() == “+”: answer_label[‘text’] = str(int(left_entry.get()) + int(right_entry.get())) elif … Read more

[Solved] What kind of number 9.00000000e-01 is?

[ad_1] >>> import numpy as np >>> x = np.arange(-1,1,0.1) >>> print(x) [ -1.00000000e+00 -9.00000000e-01 -8.00000000e-01 -7.00000000e-01 -6.00000000e-01 -5.00000000e-01 -4.00000000e-01 -3.00000000e-01 -2.00000000e-01 -1.00000000e-01 -2.22044605e-16 1.00000000e-01 2.00000000e-01 3.00000000e-01 4.00000000e-01 5.00000000e-01 6.00000000e-01 7.00000000e-01 8.00000000e-01 9.00000000e-01] >>> type(x[0]) <type ‘numpy.float64’> >>> print(x.tolist()) [-1.0, -0.9, -0.8, -0.7000000000000001, -0.6000000000000001, -0.5000000000000001, -0.40000000000000013, -0.30000000000000016, -0.20000000000000018, -0.1000000000000002, -2.220446049250313e-16, 0.09999999999999964, 0.19999999999999973, 0.2999999999999998, 0.3999999999999997, 0.49999999999999956, … Read more

[Solved] Python, I want to find file each subfolder on a folder

[ad_1] The simplest way, assuming you do not wish to go further down in the tree is: import os filepaths = [] iterdir = os.scandir(path_of_target_dir) for entry in iterdir: filepaths.append(entry.path) Update: A list comprehension makes is faster and more compact: (Strongly Recommended) import os iterdir = os.scandir(path_of_target_dir) filepaths = [entry.path for entry in iterdir] If … Read more