-1

So I'm writing a program that automatically timestamps Youtube videos, what I'm doing basically is getting a bunch of videos and putting them in one long video, so I have some code writted:

description = open("Description.txt", "r+")
description.truncate(0)
description.write("Timestamps:\n")
timestamps = [0.01, 0.08, 0.16, 0.30, 0.50, 1.05, 1.25] #These are just examples, usually this would be a list of 50 or so values
timeadd = 0
total = 0

for time in timestamps:
    timeadd += 1

for add in range(timeadd):
    total += timestamps[add]
    print(total)
    description.write(str(total) + "\n")

input("Done... ")

This works pretty successfully writes timestamps but any help to make this simpler would be appreciated, the problem is though it writes Timestamps like this: "2.1", what it means is "2:10", is there any way I could specify to add a 0 every time the Timestamp is in the tens place? I'm sorry if the answer is simple, I'm new to python.

rabbibillclinton
  • 410
  • 4
  • 17
  • 2
    One thing to consider with your script, you are adding numbers written in base 60, but doing math in base 10. For example, ```2:10 + 1:50 == 4```, but the way your script is running, you have ```2.1 + 1.50 == 3.6```. – goalie1998 Jan 28 '21 at 03:46
  • Does this answer your question? [Display number with leading zeros](https://stackoverflow.com/questions/134934/display-number-with-leading-zeros) – Chayim Friedman Jan 28 '21 at 03:49
  • @ChayimFriedman Wrong way. – user202729 Jan 28 '21 at 03:53
  • Quick suggestion, you can replace `for time in timestamps: timeadd += 1` (lines 8-9) with this `timeadd = len(timestamps)` – sushant_padha Jan 28 '21 at 03:53
  • What's wrong @user202729? – Chayim Friedman Jan 28 '21 at 03:53
  • That question asks for adding zeros to the left, this one is to the right. – user202729 Jan 28 '21 at 03:54
  • 1
    Well there's [string - Display a float with two decimal places in Python - Stack Overflow](https://stackoverflow.com/questions/6149006/display-a-float-with-two-decimal-places-in-python) ;; however the underlying problem with op here is that float is not the suitable format in the first place. – user202729 Jan 28 '21 at 03:54
  • Well he also wants to replace the dot with a colon, just use two integers instead – Chayim Friedman Jan 28 '21 at 03:55

2 Answers2

2

I think you are going to have to store the timestamps as strings, if you want them to have this way. Ootherwise, you can store them as doubles and abuse a bit good ol' time module. Third way would be to write your own data object to store them the way you want.

1

To add zeroes to the number while displaying them, use this

number = 1.3  # example

print(number)  # Prints `1.3`

zero_padded_number = str(number).split('.')[0] + ':' + str(number).split('.')[1].ljust(2, '0')

print(zero_padded_number)  # Prints `1:30`

Note that zero_padded_number is a string, so it can only be used for printing and representation purposes.

sushant_padha
  • 159
  • 1
  • 8