how to remove space in string list python - python

So i have list contains with 1 string.
biner_pass = ['00001111000000000000111111111111
00001111111111111111000011111111 11111111111100000000000000000000
11111111111111110000111111111111']
all i want is to remove the space and join all the binary.
i was trying using
binerpass = ''.join(biner_pass.split(' ')) or biner_pass.replace(" ", "") but it cannot work. so how to remove space?

The string is the 0-th element of a list with 1 element. Therefore you need
biner_pass[0].replace(" ", "")

IIUC, You can use replace.
biner_pass = ['0000111100000000000011111111111100001111111111111111000011111111 1111111111110000000000000000000011111111111111110000111111111111']
biner_pass[0] = biner_pass[0].replace(" ", "")
print(biner_pass)
Output:
['00001111000000000000111111111111000011111111111111110000111111111111111111110000000000000000000011111111111111110000111111111111']

You need to do it for each element of the list
biner_pass = [b.replace(' ','') for b in biner_pass]

binary will be a string that conatains 1 and 0 without spaces
binary = "".join(biner_pass).replace(" ", "")

Related

Python replace particular substring in string

I need my program to replace a certain - with a variable. I've tried this but it replaces the whole thing with the varibale.
jelenleg = 8 * "-"
x = 0
guess = c
jelenleg = jelenleg[x].replace("-", guess)
print(jelenleg)
So I need this to happen:
before: --------
after: c-------
But instead what I get is this: c
You can specify the count of to be replaced items:
jelenleg.replace("-", guess, 1)
will only replace one -
To replace at a particular location, I cant think of anything easier than transforming the string into a list, then replacing, then back to a string, like this:
jelenleg_list = list(jelenleg) # str --> list
jelenleg_list[x] = guess # replace at pos x
jelenleg = "".join(jelenleg_list) # list --> str

Format string output

With this python's code I may read all tickers in the tickers.txt file:
fh = open("tickers.txt")
tickers_list = fh.read()
print(tickers_list)
The output that I obtain is this:
A2A.MI, AMP.MI, ATL.MI, AZM.MI, BGN.MI, BMED.MI, BAMI.MI,
Neverthless, I'd like to obtain as ouput a ticker string exactly formatted in this manner:
["A2A.MI", "AMP.MI", "ATL.MI", "AZM.MI", ...]
Any idea?
Thanks in advance.
If you want the output to look in that format you want, you would need to do the following:
tickers_list= "A2A.MI, AMP.MI, ATL.MI, AZM.MI, BGN.MI, BMED.MI, BAMI.MI"
print("["+"".join(['"' + s + '",' for s in tickers_list.split(",")])[:-1]+"]")
With the output:
["A2A.MI"," AMP.MI"," ATL.MI"," AZM.MI"," BGN.MI"," BMED.MI"," BAMI.MI"]
Code explanation:
['"' + s + '",' for s in tickers_list.split(",")]
Creates a list of strings that contain each individual value, with the brackets as well as the comma.
"".join(...)[:-1]
Joins the list of strings into one string, removing the last character which is the extra comma
"["+..+"]"
adds the closing brackets
Another alternative is to simple use:
print(tickers_list.split(","))
However, the output will be slightly different as in:
['A2A.MI', ' AMP.MI', ' ATL.MI', ' AZM.MI', ' BGN.MI', ' BMED.MI', ' BAMI.MI']
Having ' instead of "
A solution for that however is this:
z = str(tickers_list.split(","))
z = z.replace("'",'"')
print(z)
Having the correct output, by replacing that character
you can to use Split function:
tickers_list = fh.read().split(',')

Removing strings character with given input

I need to remove a certain string with the given input. For example,
user_input = input("Input File name: ")
and the user key in "patient_zero[20150203].txt" and it would like to clean up the file name by removing all instances of square brackets and the contents contained within it.
Output should be "patient_zero.txt". Is there anyway to do it?
If you just want to remove the square bracket portion of the filename, you could use:
inp = "patient_zero[20150203].txt"
output = re.sub(r'^(\S+)\[.*?\]\.(\S+)$', r'\1.\2', inp)
print(output) # patient_zero.txt
using spplit
var = "patient_zero[20150203].txt"
print(var.split("[")[0] + var.split("]")[1]) # PatientName + FileFormat
import re
s = "patient_zero[20150203].txt"
print (re.sub(r'\[[\w\s]+\]','',s))
Output:
patient_zero.txt

Removing Single Quotes from a String Stored in an Array

I wrote code to append a json response into a list for some API work I am doing, but it stores the single quotes around the alphanumerical value I desire. I would like to get rid of the single quotes. Here is what I have so far:
i = 0
deviceID = []
while i < deviceCount:
deviceID.append(devicesRanOn['resources'][i])
deviceID[i] = re.sub('[\W_]', '', deviceID[i])
i += 1
if i >= deviceCount:
break
if (deviceCount == 1):
print ('Device ID: ', deviceID)
elif (deviceCount > 1):
print ('Device IDs: ', deviceID)
the desired input should look like this:
input Device IDs:
['14*************************00b29', '58*************************c3df4']
Output:
['14*************************00b29', '58*************************c3df4']
Desired Output:
[14*************************00b29, 58*************************c3df4]
As you can see, I am trying to use RegEx to filter non Alphanumeric and replace those with nothing. It is not giving me an error nor is it preforming the actions I am looking for. Does anyone have a recommendation on how to fix this?
Thank you,
xOm3ga
You won't be able to use the default print. You'll need to use your own means of making a representation for the list. But this is easy with string formatting.
'[' + ', '.join(f'{id!s}' for id in ids) + ']'
The f'{id:!s} is an f-string which formats the variable id using it's __str__ method. If you're on a version pre-3.6 which doesn't use f-strings, you can also use
'%s' % id
'{!s}'.format(id)
PS:
You can simplify you're code significantly by using a list comprehension and custom formatting instead of regexes.
ids = [device for device in devicesRanOn['resources'][:deviceCount]]
if deviceCount == 1:
label = 'Device ID:'
elif deviceCount > 1:
label = 'Device IDs:'
print(label, '[' + ', '.join(f'{id!s}' for id in ids) + ']')

Python - Print last string after line.split

Is there a easy way to print the last string after you've split?
Input File:
Name:DOB:Age
Name:Name2:DOB:Age
I want the name and age...
So...
string = line.split(':')
print(string[0] + string[xxxx])
Is there anything that goes where xxxx is to always print the last string?
Thanks
string = line.split(':')
print(string[0] + string[-1])

Categories