leading-zero

Leading '0' error in Python in spite of its absence

Leading '0' error in Python in spite of its absence Question: from dateutil.parser import parse import re from datetime import datetime def date_fun(date): try: dt = parse(date) dt = dt.strftime(‘%d/%m/%Y’) dmy_split=re.split(‘[- / ]’, dt) # error here: if (eval(dmy_split[0])>=1 and eval(dmy_split[0])<=12 and eval(dmy_split[1])>=1 and eval(dmy_split[1])<=12 or len(dmy_split[2])==2): print("ambiguous") # Format matched else: print("True") except ValueError: …

Total answers: 1

Remove only leading zeros from sorted dictionary keys

Remove only leading zeros from sorted dictionary keys Question: This is my code file_name = input() shows = {} with open(file_name, ‘r’) as file: lines = file.readlines() for i in range(0, len(lines), 2): season = lines[i].strip(‘n’) name = lines[i+1].strip(‘n’) if(season in shows): shows[season].append(name) else: shows[season] = [name] with open(‘output_keys.txt’, ‘w+’) as f: for key in …

Total answers: 2

How to add leading zeros in a month columns in Pandas?

How to add leading zeros in a month columns in Pandas? Question: I looked through a lot of other questions but didn’t see one that quite fit. Say I have the dataframe, df: Name Month Bob 5 Jim 7 Mary 12 I’m trying to write a for loop that would add a leading zero to …

Total answers: 4

How to remove leading and trailing zeros in a string? Python

How to remove leading and trailing zeros in a string? Python Question: I have several alphanumeric strings like these listOfNum = [‘000231512-n’,’1209123100000-n00000′,’alphanumeric0000′, ‘000alphanumeric’] The desired output for removing trailing zeros would be: listOfNum = [‘000231512-n’,’1209123100000-n’,’alphanumeric’, ‘000alphanumeric’] The desired output for leading trailing zeros would be: listOfNum = [‘231512-n’,’1209123100000-n00000′,’alphanumeric0000′, ‘alphanumeric’] The desire output for removing both …

Total answers: 7