Here is my code. It is the last eval line that is returning a syntax error
def format_date():
month_abrv = ['Jan','Feb','Mar','Apr','May','Jun','Jul',
'Aug','Sep','Oct','Nov','Dec']
print('''This program takes an input in the format MM/DD/YYYY(ex:09/23/2014)
and outputs date in format DD Mth, YYYY (ex: 23 Sep, 2014)''')
date_string = input('\nInput date in format MM/DD/YYYY ')
date_list = date_string.split('/')
date_list[0] = eval(date_list[0])
format_date()
Here is the error
Traceback (most recent call last):
File "C:/Python34/Python ICS 140/FormatDate.py", line 16, in <module>
format_date()
File "C:/Python34/Python ICS 140/FormatDate.py", line 14, in format_date
date_list[0] = eval(date_list[0])
File "<string>", line 1
09
^
SyntaxError: invalid token
After doing a little more digging I found that Python interprets numbers starting with 0s as octal numbers. using the int() function to change '09' to 9 worked in this case.
Related
When I scrape a webpage it returns this: 42,834.56
Apparently, it extracts it as a word (because when I try to sum it whit the other ones retrieve in excel it doesn't work). How can I convert it into a number?
I'm copying from the cmd:
Here is an error when I write it with int():
Traceback (most recent call last):
File "C:\Users\Windows\Desktop\py4e\callao.py", line 337, in <module>
print(int(peso))
ValueError: invalid literal for int() with base 10: '42,834.56\xa0'
Here is an error when I write it with float():
Traceback (most recent call last):
File "C:\Users\Windows\Desktop\py4e\callao.py", line 337, in <module>
print(float(peso))
ValueError: could not convert string to float: '42,834.56\xa0'
You might need to remove ',' from the number. Try this:
float("".join(peso.split(',')))
a = "42,834.56"
b = float(a.replace(",",""))
print(type(b))
# output: <class 'float'>
You can try this.
Strip off the \xa0 character.
Remove the , from the string
Convert it to float.
s = '42,834.56\xa0'
s = s.strip()
print(float(s.replace(',','')))
You can store the scraped value in a variable and convert to float in python. Here's the function:
def convert_to_float(peso):
return float(peso.replace(',',''))
And here's the call to the function:
peso = '42,834.56'
convert_to_float(peso)
Output:
42834.56
Now you can sum it with others.
Edit -
It seems you have scraped \xa0 also along with the string. So to handle that:
def convert_to_float(peso):
peso = peso.split("\\")[0]
return float(peso.replace(',',''))
peso = '42,834.56\xa0'
convert_to_float(peso)
Output will be same as above.
I need to change with python a lot of strings with a Spanish date format (DDMMMYYYY, MMM abbreviated month in Spanish) in a other datetime format but I'm having problems because my locale Spanish settings has a "." (a dot) at the end of the string when it change this format in a abbreviated month format.
By default, python take the English version of the language but I can change the language with the locale library.
When I select 'esp' or 'es_ES.utf8' the dot at the end of the abbreviated month appears.
Does it depend on the regional settings of my Windows 10? (I check it and all seems OK) Does it depend on the LOCALE library settings?
The same code in UBUNTU runs OK (without the point)
How can I solve this problem?
I don't want to transform all the strings like that..
str_date = str_date[:5] + "." + str_date[5:]
Thanks a lot!!
Example (previously I change the language with locale):
>>> datetime.strptime('2021-01-18', '%Y-%m-%d').strftime('%b')
'ene.'
>>> print(datetime.strptime('18ene2021', '%d%b%Y'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\galonsoi\AppData\Local\Programs\Python\Python36\lib\_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "C:\Users\galonsoi\AppData\Local\Programs\Python\Python36\lib\_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '18ene2021' does not match format '%d%b%Y'
>>> print(datetime.strptime('18ene.2021', '%d%b%Y'))
2021-01-18 00:00:00 ----> THIS IS OK BECAUSE I WRITE THE DOT AT THE END OF THE ABBREVIATED MONTH
Complete sequence of the Example
>>> import locale
>>> from datetime import datetime
>>>
>>> locale.getlocale()
(None, None)
>>> print (datetime.strptime('2021-01-18', '%Y-%m-%d').strftime('%b'))
Jan
>>> locale.setlocale(locale.LC_ALL, '')
`Spanish_Spain.1252`
>>> locale.getlocale()
(`es_ES`, `cp1252`)
#INCORRECT FORMAT, ADD A "." AT THE END
>>> print (datetime.strptime('2021-01-18', '%Y-%m-%d').strftime('%b'))
ene.
>>> locale.setlocale(locale.LC_ALL, 'es_ES.UTF-8')
`es_ES.UTF-8`
#FORMATO INCORRECTO, AÑADE UN "." a may
>>> print (datetime.strptime('2021-01-18', '%Y-%m-%d').strftime('%b'))
ene.
>>> print(datetime.strptime('18ene2021', '%d%b%Y'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\galonsoi\AppData\Local\Programs\Python\Python36\lib\_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "C:\Users\galonsoi\AppData\Local\Programs\Python\Python36\lib\_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '18ene2021' does not match format '%d%b%Y'
>>> print(datetime.strptime('18ene.2021', '%d%b%Y'))
2021-01-18 00:00:00 ----> THIS IS OK BECAUSE I WROTE THE DOT AT THE END OF THE ABBREVIATED MONTH
You could make use of dateutil's parser, where you can set custom month names via the parser.parserinfo class. Ex:
import locale
locale.setlocale(locale.LC_ALL, 'Spanish_Spain.1252') # set locale for reproducibility
import calendar
from dateutil import parser
# subclass parser.parserinfo and set custom month names with dots stripped:
class LocaleParserInfo(parser.parserinfo):
MONTHS = [(ma.strip('.'), ml) for ma, ml in zip(calendar.month_abbr, calendar.month_name)][1:]
s = '18ene2021'
print(parser.parse(s, parserinfo=LocaleParserInfo()))
# 2021-01-18 00:00:00
I want to convert the received hex data into binary form. I get ValueError.
For example, I want the first value in the output to be printed as 0000.
received_data = " ".join("{:02x}".format(byte) for byte in (data))
print(received_data)
P_data = "{0:b}".format(received_data[0:1])
Output:
01 04 04
Error:
Traceback (most recent call last):
File "C:\Users\User\eclipse-workspace\Try\test1\test2.py", line 22, in
<module>
P_data="{0:b}".format(received_data[0:1])
ValueError: Unknown format code 'b' for object of type 'str'
You should first convert your string into an integer
P_data = '{0:b}'.format(int(received_data[0:1], 16)).zfill(4)
I am trying to convert these strings to timestamps:
python test.py
2015-02-15T14:25:54+00:00
2015-02-15T16:59:01+00:00
2015-02-15T18:44:13+00:00
2015-02-15T18:45:24+00:00
2015-02-15T18:52:11+00:00
2015-02-15T18:52:33+00:00
2015-02-15T18:59:00+00:00
2015-02-15T19:06:16+00:00
2015-02-15T19:07:02+00:00
I get this output on executing below code:
for member in members_dict['members']:
s = member['timestamp_signup']
print s
But when I try to get the timestamp:
for member in members_dict['members']:
s = member['timestamp_signup']
print s
print time.mktime(datetime.datetime.strptime(s, "%Y-%m-%dT%H:%M:%S+00:00").timetuple())
I get the error as:
Traceback (most recent call last):
File "test.py", line 20, in <module>
print datetime.strptime(s, '"%Y-%m-%dT%H:%M:%S+00:00"').date()
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '' does not match format '"%Y-%m-%dT%H:%M:%S+00:00"'
What am I doing wrong here?
Your code to convert string to datetime is fine. For example:
>>> from datetime import datetime
>>> my_str = '2015-02-15T14:25:54+00:00'
>>> datetime.strptime(my_str, "%Y-%m-%dT%H:%M:%S+00:00")
datetime.datetime(2015, 2, 15, 14, 25, 54)
Error you are getting is due to empty string present in your file. I got to know about it based on your error message:
ValueError: time data '' does not match format
# empty string ^
Possibly there is empty line at the end of your file (or, somewhere else)
My code for the sorting of the file.
g = open('Lapse File.txt', 'r')
column = []
i = 1
next(g)
for line in g:
column.append(int(line.split('\t')[2]))
column.sort()
This is the error I get.
Traceback (most recent call last):
File "E:/Owles/new lapse .py", line 51, in <module>
column.append(int(line.split('\t')[2]))
ValueError: invalid literal for int() with base 10: '-8.3\n
My main question is why is there a \n. Earlier in the code I had written to another text file and wrote it by column from a previously read in file.
This is my code for writing the file
for columns in (raw.strip().split() for raw in Sounding):
if (i >2 and i <=33):
G.write(columns [3]+'\t'+columns[2]+'\t'+columns[4]+'\n')
i = i + 1
elif (i >= 34):
G.write(columns [0]+'\t'+columns[1]+'\t'+columns[2]+'\n')
i = i + 1
else:
i = i + 1
I am unsure if writing the lines like that is the issue because I have inserted the new line function.
The traceback is telling you exactly what happened:
ValueError: invalid literal for int() with base 10: '-8.3\n'
The problem here is that, while int() can handle the negative sign and the trailing newline character, it can't handle the decimal point, '.'. As you know, -8.3 may be a real, rational number, but it's not an integer. If you want to preserve the fractional value to end up with -8.3, use float() instead of int(). If you want to discard the fractional value to end up with -8, use float() to parse the string and then use int() on the result.
-8.3:
column.append(float(line.split('\t')[2]))
-8:
column.append(int(float(line.split('\t')[2])))
Because only numeric strings can be cast to integers; look at this:
numeric_string = "109"
not_numeric_string = "f9"
This is okay:
>>> int(numeric_string)
109
And it cannot be cast:
>>> int(not_numeric_string)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'f9'
So somewhere in your script it is getting a non-numeric string.
It seems as though the "-8.3\n" string sequence has raised the error, so you must strip escape chars as well.