1

I have a string that looks like this:

"2018-05-23-13:54:56.594000"

When i try to convert it to int, it gives me an error:

ValueError: invalid literal for int() with base 10: '"2018-05-23-13:54:56.594000"'

Code:

output_file = open(r"C:\PATH\123.acc.bin", "wb")

with open(r"C:\PATH\00000007.csv", newline='') as csvfile:
    sensor = csv.reader(csvfile, delimiter=',', quotechar='|')

    with open(r"C:\PATH\3dm2.csv", newline='') as csvfile:
        sensor2 = csv.reader(csvfile, delimiter=',', quotechar='|')

        for row, row2 in zip(sensor, sensor2):

            internalTimestamp  = int(row2[16])
            msInfile = (int(float(row2[12])*1000)) + (1523138400000+604800000)

            accX    = float(row[0])
            accY    = float(row[1])
            accZ    = float(row[2])
            roll    = float(row[3])
            pitch   = float(row[4])
            yaw     = float(row[5])

            accData2 = pack('f', accX)
            accData3 = pack('f', accY)
            accData4 = pack('f', accZ)
            accData5 = pack('f', roll)
            accData6 = pack('f', pitch)
            accData7 = pack('f', yaw)
            accData8 = pack('I', internalTimestamp)
            accData9 = pack('I', ValidData)
            accData10 = pack('q', msInfile)

            output_file.write(accData2)
            output_file.write(accData3)
            output_file.write(accData4)
            output_file.write(accData5)
            output_file.write(accData6)
            output_file.write(accData7)
            output_file.write(accData8)
            output_file.write(accData9)
            output_file.write(accData10)
            count += 1

This is my code where I pack data from two different csv files into one binary file.

martineau
  • 119,623
  • 25
  • 170
  • 301

3 Answers3

5

If you want your datetime in unix format.

import datetime
import time
s = "2018-05-23-13:54:56.594000"

d = datetime.datetime.strptime(s, "%Y-%m-%d-%H:%M:%S.%f")
print(time.mktime(d.timetuple()))

Output:

1527063896.0
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

That string is not in base 10. It has dashes and other characters that are not in the base 10 system.

So, python cannot convert it to int

0

This is because you have non-numerical characters within the string. For int() to properly cast, you must pass it a string with only numerical characters.

Try to replace the non numerical characters in the string using the .replace() method before you pass the string in. Here is some info on that method!

Additionally, you might want to convert the datetime into a integer representation. To do that, check out this previous question!