invalid syntax: None, line 4, pos 72 - python

I can't make a space between year and hour, and I am not allowed to use ",". I have use concatenation.
from datetime import datetime
now = datetime.now()
print str(now.month) + "/" + str(now.day) + "/" + str(now.year), "+ "str(now.hour) + ":" + str(now.minute) + ":" + str(now.second)

Try avoiding such long statements and try format them in a readable way. You have wrongly put the + operator within "" and I believe the , should be printed as a separator between month/day/year and hour:minute:second
print str(now.month) + "/" +
str(now.day) + "/" +
str(now.year) + " " +
str(now.hour) + ":" +
str(now.minute) + ":" +
str(now.second)

+ str(now.year), "+ "str(now.hour) + ":"
Your + is in quotes and you have a comma after str(now.year)

now.strftime('%m/%d/%Y + %H:%M:%S')
type this command after 'now = datetime.now()' and you will get the output you want.

Related

Python Requests gives Invalid Return Character or Leading Space in Header On Line Breaks

Python 3.8.5 Requests 2.22.0
The API I'm working with requires a unique signature header format.
Please note that this is a header which includes a list of the headers in it as well.
format:
The Header: X-Ca-Signature
stringToSign=HTTPMethod+"\n"+ Headers+"\n"+API_URI+"\n"+FormattedQFStr;
which I believe translates to something like
GET
X-Ca-Key:ACCESSKEY
X-Ca_Nonce:UUID
X-Ca-Timestamp:UNIX_TIMESTAMP
api/open/v1/device/checkMac
mac=MACADDR
I have tried the three following methods of formulating the header
stringToSign = str("GET" + "n\\" + "X-Ca-Key:" + ACCESSID + "n\\" + "X-Ca_Nonce:" + UUID + "n\\" + "X-Ca-Timestamp:" + MILLI_TIMESTAMP_STRIPPED + "n\\" + "api/open/v1/device/checkMac" + "n\\" + "mac=MACADDR")
stringToSign = str("GET" + os.linesep + "X-Ca-Key:" + ACCESSID + os.linesep + "X-Ca_Nonce:" + UUID + os.linesep + "X-Ca-Timestamp:" + MILLI_TIMESTAMP_STRIPPED + os.linesep + "api/open/v1/device/checkMac" + os.linesep + "mac=MACADDR")
stringToSign = str("GET" + "\n" + "X-Ca-Key:" + ACCESSID + "\n" + "X-Ca_Nonce:" + UUID + "\n" + "X-Ca-Timestamp:" + MILLI_TIMESTAMP_STRIPPED + "\n" + "api/open/v1/device/checkMac" + "\n" + "mac=MACADDR")
The last two appear to give me the correct format:
GET
X-Ca-Key:ACCESSKEY
X-Ca_Nonce:UUID
X-Ca-Timestamp:UNIX_TIMESTAMP
api/open/v1/device/checkMac
mac=MACADDR
but on my request
payload = {'mac': MACADDR }
HEADERS = {"X-Ca-Key": ACCESSID, "X-Ca-Timestamp" : MILLI_TIMESTAMP_STRIPPED, "X-Ca-Nonce": UUID, "X-Ca-Signature": stringToSign,}
response = requests.get(URL_ARRAY[3], headers=HEADERS, params=payload)
Requests does not like the linebreaks at all and gives me
requests.exceptions.InvalidHeader: Invalid return character or leading space in header: X-Ca-Signature.
Any advice to steer me in the right direction is very much appreciated.

TypeError: bad operand type for unary +: 'str' in string creation

I keep getting the above mentioned error when running:
def Decode(iList):
IssuerList = ["Dummy","enRoute","JCB","Diner's Club","Visa"
,"Master Card","Union Pay","Petroleum"]
TypeList = ["Debit account", "Credit account"]
for istr in iList:
ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
+ ". The card is linked to a " + TypeList[int(istr[8])]
+ " with the account number: " + istr[8:]
WriteFile(ostr)
File "", line 24, in Decode
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
TypeError: bad operand type for unary +: 'str'
Have tried str() on the bad line but no luck.
You have a valid, complete line of Python
ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
Then another line starting with a unary +, which isn't a valid statement applied to a string.
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
See How can I do a line continuation in Python for the options to continue an expression over more than one line.
The practice recommended in the style guide is to use parenthesis and break before the operator.
def Decode(iList):
IssuerList = ["Dummy","enRoute","JCB","Diner's Club","Visa"
,"Master Card","Union Pay","Petroleum"]
TypeList = ["Debit account", "Credit account"]
for istr in iList:
ostr = (istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
+ ". The card is linked to a " + TypeList[int(istr[8])]
+ " with the account number: " + istr[8:])
WriteFile(ostr)
You are assuming that python knows that you want to carry the expression to the next line. In this case it does not know that unless you tell it using a \ at the end of the line or enclose the expression in (). Basically some way to indicate that the seemingly completed expression is not complete and continues on the next line.
This:
ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
+ ". The card is linked to a " + TypeList[int(istr[8])]
+ " with the account number: " + istr[8:]
should be:
ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20" \
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8] \
+ ". The card is linked to a " + TypeList[int(istr[8])] \
+ " with the account number: " + istr[8:]
or:
ostr = (istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
+ ". The card is linked to a " + TypeList[int(istr[8])]
+ " with the account number: " + istr[8:])
Now let me explain the actual error you are getting 'TypeError: bad operand type for unary +: 'str' in string creation' is because it has finished parsing the seemingly complete first line or the part clipped out above and has now moved on to parse the second line. It sees the new line starts with the + so it must be the unary + since there is no operand on the left of the +. It is not applicable to the string operand that comes next, so it complains.
Adding backslashes when creating string on multiple lines, should solve the problem:
ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20" \
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8] \
+ ". The card is linked to a " + TypeList[int(istr[8])] \
+ " with the account number: " + istr[8:]

Python : Calculate values and send in email

UPDATE : I have corrected my code and below is working fine as expected
Basically i need an output like below in mail.
I achieved this. but need to know if any efficient code then below one.
name 5001 5010 9000 4 %
name 5002 5010 9000 4 %
name 5003 5010 9000 4 %
name 5004 5010 9000 4 %
Storing the values in list.
Below are dummy values
container = []
for server in range(1,5):
container.append('name')
container.append(server + 5000)
container.append(5000+10)
container.append(4000+5000)
container.append(2500 % 12)
print('\n' + str(container))
Assign list of values to msgBody in order to send it via email
I'm just putting piece of code here. Below also working fine
msgBody1 = ''
for count in range(4):
if count == 0:
tempValue = '\n' + '\n' + str(container[count]) + '\t' + str(container[count+1]) + '\t' + str(container[count+2]) + '\t'
+ str(container[count+3]) + '\t' + str(container[count+4])
msgBody1 = msgBody1 + str(tempValue) + ' %'
elif count == 1:
tempValue = '\n' + '\n' + str(container[count+4]) + '\t' + str(container[count+5]) + '\t' + str(container[count+6]) + '\t'
+ str(container[count+7]) + '\t' + str(container[count+8])
msgBody1 = msgBody1 + str(tempValue) + ' %'
elif count == 2:
tempValue = '\n' + '\n' + str(container[count+8]) + '\t' + str(container[count+9]) + '\t' + str(container[count+10]) + '\t'
+ str(container[count+11]) + '\t' + str(container[count+12])
msgBody1 = msgBody1 + str(tempValue) + ' %'
elif count == 3:
tempValue = '\n' + '\n' + str(container[count+12]) + '\t' + str(container[count+13]) + '\t' + str(container[count+14]) + '\t'
+ str(container[count+15]) + '\t' + str(container[count+16])
msgBody1 = msgBody1 + str(tempValue) + ' %'
Any other better and short code to replace msgBody1
Thanks in advance
Your question is not clear; the code example does not make any sense. But from the structure of it, it seems like you are trying to use dict, but you are defining or sourcing lists.
Not sure why for server in servers, I hope your servers list is collection of numerical value, which does not make any sense.
Please go through list Vs dict, and list.append() and how to add new key, value pairs to dictionary.

New line being printed as "\n" instead of actual new line

I'm trying to print a simple game board.
places = [" "] * 9
board = (places[8] + "|" + places[7] + "|" + places[6] + "\n",
places[5] + "|" + places[4] + "|" + places[3] + "\n",
places[2] + "|" + places[1] + "|" + places[0] + "\n")
print(board)
But instead of a board I get this:
(' | | \n', ' | | \n', ' | | \n')
I want to print it without the parentheses and with new lines instead of \n. How can I do this?
Don't print whole tuple, but it's elements, e.g.:
for i in board:
print(i)
You just have to use this line to print:
print(*board,sep="")
Explanation: the operator * is the unpacking operator: the tuple board is then unpacked, and it is like you've done:
print( places[8] + "|" + places[7] + "|" + places[6] + "\n",
places[5] + "|" + places[4] + "|" + places[3] + "\n",
places[2] + "|" + places[1] + "|" + places[0] + "\n",
sep="")
Because you've put commas on the end of each line in the brackets, you've made a tuple, which is an iterable, like a list. Just replace the commas with a + at the start of the next line
board = ('line 1' + '\n'
+ 'line 2' + '\n'
+ 'line 3' + '\n')
This creates a string instead of a tuple.
places = [" "] * 9
print(places[8] + "|" + places[7] + "|" + places[6] + "\n" + places[5] + "|" +
places[4] + "|" + places[3] + "\n" + places[2] + "|" + places[1] + "|" + places[0] +
"\n")
This is what you're looking for.
The problem was that "\n" only works in print statements.

Not able to wrap lines in Python

else:
fullName = curLineFin[1] + ' ' + curLineFin[2]
players[fullName] = curLineFin[0] + '\t' + curLineFin[1] + \
'\t' + curLineFin[2] + '\t' + curLineFin[3] + '\t' + \
curLineFin[4] + '\t' + curLineFin[5] + '\t' + curLineFin[6] + \
'\t' + curLineFin[7] + '\t' + curLineFin[8] + '\t' + \
curLineFin[9] + '\t' + curLineFin[10] + '\t'
Every time I run the script, I get the error:
players[fullName] = curLineFin[0] + '\t' + curLineFin[1] + \
^
IndentationError: unindent does not match any outer indentation level
Wrap your code with parentheses
players[fullName] = (curLineFin[0] + '\t' + curLineFin[1] +
'\t' + curLineFin[2] + '\t' + curLineFin[3] + '\t' +
curLineFin[4] + '\t' + curLineFin[5] + '\t' + curLineFin[6] +
'\t' + curLineFin[7] + '\t' + curLineFin[8] + '\t' +
curLineFin[9] + '\t' + curLineFin[10] + '\t' )
or
players[fullName] = '\t'.join(curLineFin[:11]) + '\t'
or if this trailing tab char is not needed and you have exactly eleven elements in curLineFin.
players[fullName] = '\t'.join(curLineFin)
Just use parenthesis:
fullName = (curLineFin[1] + ' ' + curLineFin[2] +
players[fullName] = curLineFin[0] + '\t' + curLineFin[1] +
'\t' + curLineFin[2] + '\t' + curLineFin[3] + '\t' +
curLineFin[4] + '\t' + curLineFin[5] + '\t' + curLineFin[6] +
'\t' + curLineFin[7] + '\t' + curLineFin[8] + '\t' +
curLineFin[9] + '\t' + curLineFin[10] + '\t')
The code you have posted does not generate that error, so it's impossible to diagnose exactly what's happening in the different code you're actually running.
The most likely cause is that it's completely unrelated to the backslashes, and you're doing something like mixing tabs and spaces. (The fact that you're using a weird 6-character indent for the block isn't a good sign…)
Another possibility is that you're putting extra spaces after one of the backslashes. This should usually give you a SyntaxError: unexpected character after line continuation character, but it's possible to confuse Python to the point where that passes and you get the following generic SyntaxError for a + with no right operand or IndentationError for the next line.

Categories

Resources