Python "SyntaxError: invalid token" on numbers starting with 0 (zeroes) - python

I know someone might think this question has been answered here but it doesn't have answer to what I want to achieve.
I have list of phone numbers, a very large one, and a whole lot of them starts with 08 and there is a lot of duplication, which is what I am trying to remove. Now I need to put them in a list or set so that I can use them in my program but it returns Invalid token as shown in the picture below:
Python assumes anything that starts with 0 as octal. How do I device a mean to bypass this and have these numbers in a list and then in a set?

read your phone input file, save each phone as string to a set, then the duplicates will be removed due to set only hold unique elements, and you can do further work on them.
def get_unique_phones_set():
phones_set = set()
with open("/path/to/your/duplicated_phone_file", "r") as inputs:
for phone in inputs:
# phone is read as a string
phones_set.add(phone.strip())
return phones_set

If you need to have them prepended by 08, use strings instead of ints.
a = ["08123","08234","08123"]
a = list(set(a)) # will now be ["08123","08234"]
Since (as you say) you don't have an easy way of surrounding the numerous numbers with quotes, go to http://www.regexr.com/ and enter the following:
Expression: ([0-9]+)
Text: Your numbers
Substitution (expandable pane at the bottom of the screen: "$&"

Related

How to separate user's input with two separators? And controlling the users input

I want to separate the users input using two different separators which are ":" and ";"
Like the user should input 4 subject and it's amounts. The format should be:
(Subject:amount;Subject:amount;Subject:amount;Subject:amount)
If the input is wrong it should print "Invalid Input "
Here's my code but I can only used one separator and how can I control the users input?
B = input("Enter 4 subjects and amount separated by (;) like Math:90;Science:80:").split(";")
Please help. I can't figure it out.
If you are fine with using regular expressions in python you could use the following code:
import re
output_list = re.split("[;:]", input_string)
Where inside the square brackets you include all the characters (also known as delimiters) that you want to split by, just make sure to keep the quotes around the square brackets as that makes a regex string (what we are using to tell the computer what to split)
Further reading on regex can be found here if you feel like it: https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285
However, if you want to do it without importing anything you could do this, which is another possible solution (and I would recommend against, but it gets the job done well):
input_string = input_string.replace(";", ":")
output_list = input_string.split(":")
Which works by first replacing all of the semicolons in the input string with colons (it could also work the other way around) and then splitting by the remaining character (in this case the colons)
Hope this helped, as it is my first answer on Stack overflow.

In python, is there a way to remove all text following the last instance of a delimiter?

I'm trying to create a random text generator in python. I'm using Markovify to produce the required text, a filter to not let it start generating text unless the first word is capitalized and, to prevent it from ending "mid sentence", want the program to search from the back of the output to the front and remove all text after the last (for instance) period. I want it to ignore all other instances of the selected delimiter(s). I have no idea how many instances of the delimiter will occur in the generated text, nor have anyway to know in advance.
While looking into this I found rsplit(), and tried using that, but ran into a problem.
'''tweet = buff.rsplit('.')[-1] '''
The above is what I tried first, and I thought it was working until I noticed that all of the lines printed with that had only a single sentence in them. Never more than that. The problem seems to be that the text is being dumped into an array of strings, and the [-1] bit is calling just one entry from that array.
'''tweet = buff.rsplit('.') - buff.rsplit('.')[-1] '''
Next I tried the above. The thinking, was that it would remove the last entry in the array, and then I could just print what remained. It... didn't go to plan. I get an "unsupported operand type" error, specifically tied to the attempt to subtract. Not sure what I'm missing at this point.
.rsplit has second optional argument - maxsplit i.e. maximum number of split to do. You could use it following way:
txt = 'some.text.with.dots'
all_but_last = txt.rsplit('.', 1)[0]
print(all_but_last)
Output:
some.text.with

A few questions about string formating in python. Alignment

FIRST QUESTION
For example, if i want to print a lot of lines with the same width, i could use
print(f'{"INFO":=^50}')
print(f'{"some info":<50}')
print(f'{"another info":>50}')
And will get
=======================INFO=======================
some info
another info
But, what if I want to get something like this?
=======================INFO=======================
some info.............................another info
Ok.
I can do it
print(f'{"INFO":=^50}')
print('some info' + f'{"another info":.>{50-len("some info")}}')
Maybe python has another, the easiest way to do it?
SECOND QUESTION
For align we can use >, <, ^, and =
And = works only with numbers. And it works the same as >
For example
print(f'{13:.=5}')
print(f'{13:.>5}')
...13
...13
So Why do we need =, if it works the same? To be sure that the value is a number? What are the pluses it gives more?
For your second question, the answer is in Format Specification Mini-Language:
'='
Forces the padding to be placed after the sign (if any) but before the
digits. This is used for printing fields in the form ‘+000000120’.
This alignment option is only valid for numeric types. It becomes the
default when ‘0’ immediately precedes the field width.
This becomes clear when you have a signed number:
print(f'{-13:0=5}')
# -0013
print(f'{-13:0>5}')
# 00-13
What you are trying to do is an alignment inbetween two variables. That's quite specific. What then about alignment between three variables, four etc... ?
You can however approach it as an alignment problem for each of the two variables: split the 50 in two parts.
print(f'{"INFO":=^50}')
print(f'{"some info":.<25}{"another info":.>25}')
=======================INFO=======================
some info.............................another info

Python variables stored in text files not working, but should be

I've settled on a text-file based save system for my game, storing the values of required variables with keywords - for example, the password that tells the game which chapter to play. However, it appears to be malfunctioning, and I can't see why.
Before starting the game, we have:
if not os.file.isfile('TSGsave{0}.txt'.format(name)):
TSGsave=open('TSGsave{0}.txt'.format(name),'wt')
TSGsave.write('\nw5CT$n<jfW=-#J%4Ya5##')
TSGsave.close()
(the keyword used is a bunch of jibberish so that the user can't change it knowing what's going to happen). This adds w5CT$n<jfW=-#J%4Ya5## to the text file. We then have:
for i in range (len(lines)):
if 'w5CT$n<jfW' in lines[i]:
findpass=lines[i]
for i in range (len(findpass)):
if findpass[i]=='=':
cutfrom=i+1
password=findpass[cutfrom:len(findpass)]
to retrieve the variable (which can change, so it can't be written in as definite value). I know it works, because I added print (password) to the code and it returned -#J%4Ya5##. Then to start the corresponding chapter, the code is:
if password=='-#J%4Ya5##':
but it isn't starting the indented block. In the shell, the program ends and goes back to the >>> line.
If there is a way to fix this code, great - but another code to do the same thing would work just as well.
Your lines contain newlines, and these are being included. Strip these from the line:
findpass = lines[i].rstrip('\n')
Printing a value with a newline in it will simply add an extra black line after the print. Always use the repr() function to produce a Python representation of strings to see such characters:
>>> print '-#J%4Ya5##\n'
-#J%4Ya5##
>>> print repr('-#J%4Ya5##\n')
'-#J%4Ya5##\n'
Your parsing code is overly complicated; you can use str.split() or str.partition() to split your password from the line instead. You should just loop over the lines list directly rather than produce indices with range():
for line in lines:
if 'w5CT$n<jfW' in line:
password = line.partition('=')[2].rstrip('\n')

Python: Separating string into individual letters/words to be converted into ascii-hex or ascii-dec

As the title suggests, I want to get a string, split it into individual bits to input into something like ord('') and get a value for each individual character in that string. Still learning python so things like this get super confusing :P. Furthermore, the process for encryption for each of the codes will just be to shift the alphabet's dec number by a specified value and decrypt into the shifted value, plus state that value for each character. How would i go about doing this? any and all help would be greatly appreciated!
message=input("Enter message here: ", )
shift=int(input("Enter Shift....explained shift: ", )
for c in list(message):
a=ord(c)
print c
This is the very basic idea of what i was doing (was more code but similar), but obviously it didn't work :C, the indented--> just means that it was indented, just don't know how to do that in stack overflow.
UPDATE: IT WORKS (kinda) using the loop and tweaking it according to the comments i got a list of every single ascii dec value for each character in the string!, ill try and use #Hugh Bothwell's suggestion within the loop and hopefully get some work done.
mystring = "this is a test"
shift = 3
encoded = ''.join(chr(ord(ch) + shift) for ch in mystring)
You'll have to do a little more if you want your alphabet to wrap around, ie encode('y') == 'b', but this should give you the gist of it.

Categories