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.

5
  • You have a date-time string what do you want it to be converted to? Commented May 23, 2018 at 12:19
  • when you say you want to convert it into an int do you mean you want to convert it to unix time? Commented May 23, 2018 at 12:20
  • I want to pack it into a binary file with a size of 4 bytes Commented May 23, 2018 at 12:20
  • a MCVE will be internalTimestamp = int("2018-05-23-13:54:56.594000") Commented May 23, 2018 at 12:21
  • strptime() Commented May 23, 2018 at 12:22

3 Answers 3

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 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.