Python Learning - Finding Answers

In this blog, we are sharing some of the scenario arose while working on a project and we are providing the steps to get the work done. I am sure, there would be multiple ways to achieve the outcome but I am sharing my solutions.

 

Q1:  How do we extract id value from html "a" attribute? ID value was indicating page number and wanted to find ID value corresponding to last page.

Solution: I have extracted html tag and output available was

Now I wanted to extract "btn112" value. Above string was stored CompCount[3]

I have used following code to get the result.

Q2: How do I remove a part of input string? In the above example, I have "btn112" and I want number 112.

Solution: In this example, I know that "btn" would be common but last numerals can be 1,2 or 3 digits. So, better to remove the first 3 characters.

I found the length of string using len() function and selected characters from 4 position (index value 3 as indexing start from 0) and till the length of input string.

Then I can convert  to integer value using int()

Q3: How do we append or vertically combine data frames in Python?

Solution: In one of the project, we wanted to create a new data frame in the first step and then keep creating and appending the data frame with the base data frame. In panda package, we have append() function to append/vertically combining the data frame.

Q4: How can we find all the files from a directory or folder?

Solution: We had a list of xlsx files in a folder /path and we wanted to get all the list of files in a list, so that we can read those files. Here is the code, we have used.

Q5: How can you read an excel or xlsx file in Python?

Solution: I had a list of excel or xlsx files in a folder and wanted to read all files and append the content into a data frame. And below Python code is used to read the files one by one and append into a data frame.

 

Leave a Comment