How to remove [' '] from string [duplicate] - python

This question already has answers here:
In the Python interpreter, how do you return a value without single quotes around it?
(2 answers)
Closed 2 years ago.
New guy here, I am currently writing a web scraper for an exercise and I have encountered a problem with extracting the url to re-use. Basically I managed to get the URL but when I print it, it is still showing the [' '] (for example: ['http://123.com'] so it cannot be used as an input.
I am extracting the string using re.findall but then I tried to use .strip and .replace but it's I'm either getting a traceback or the input remains the same. Any suggestions please?
Extract:
z = re.findall(r'(?=htt).*?(?<=htm)', y)
h = str(z)
h = h.strip('\['"')
print(h)

re.findall returns a list. Lists don't have strip or replace methods. Access the element of the list by using z[0]. You could also use re.search if you're only looking for one string.

Just like the answers in the comments, you can simply iterate over the list to access the elements inside like so:
for i in z:
print(i)
You can substitute other methods instead of the print statement.

Related

Is there a way to write single backslash within lists? [duplicate]

This question already has an answer here:
Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
(1 answer)
Closed 1 year ago.
enter image description here
is there a way to print single backslash within list?
Regarding the first version of your question, I wrote this:
First, this expression x='\' isn't right in Python in python. you should rather puth it this way: x='\\', since back slash is a special character in python.
Second, try this:
l=['\\'] print(l)
This will print: ['\\']
But when you execute this: print(l[0]), it renders this '\'. So basically, this ['\\'] is the way to print a backslash within a list.

Python list manipulation challenges [duplicate]

This question already has answers here:
Split a string only by first space in python [duplicate]
(4 answers)
Closed 1 year ago.
I have a list in python:
name = ['A.A.BCD', 'B.B.AAD', 'B.A.A.D']
I wish to discard everything before the second '.' and keep the rest. Below is what I have come up with.
[n.split('.')[2] for n in name]
Above is working for all except the last entry. Any way to do this:
Expected output: ['BCD', 'AAD', 'A.D']
Read the documentation for split() and you’ll find it has an optional parameter for the maximum number of splits - use this to get the last one to work:
[n.split('.',maxsplit=2)[2] for n in name]
See https://docs.python.org/3/library/stdtypes.html?highlight=split#str.split
Big disadvantage of doing this as a one-liner is it will fail if there ever aren’t two . in a string, so using a for loop can be more robust.
name = ['A.A.BCD', 'B.B.AAD', 'B.A.A.D']
['.'.join(n.split('.')[2:]) for n in name]
result
['BCD', 'AAD', 'A.D']

How to split string before a character then have python create a list from it [duplicate]

This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 1 year ago.
I am trying to make python take a string, separate the characters before another character eg: "10001001010Q1002000293Q100292Q". I want to separate the string before each Q and have python create either a list or another string. I cannot figure this out for the life of me.
You can do this using the split function, give "Q" as a parameter to the split function then you can slice the list to only get the numbers before Q.
num = "10001001010Q1002000293Q100292Q"
print(num.split("Q")[:-1])
Split() function: https://www.w3schools.com/python/ref_string_split.asp
Slicing: https://www.w3schools.com/python/python_strings_slicing.asp
The syntax is str.split("separator").
str = str.split("Q")
Then output will be ['10001001010', '1002000293', '100292', ''].
If you don't need the last empty element then you can write as:
str = str.split("Q")[:-1]

Why does .split() not appear to be working in this context? (Python) [duplicate]

This question already has answers here:
why is python string split() not splitting
(3 answers)
Closed 6 years ago.
I've got a simple block of code which is meant to iterate over a list of strings and split each item in the list into a new list, then call another function on each item:
list_input = take_input()
for item in list_input:
item.split()
system_output(item)
The problem is that 'item.split()' doesn't seem to be doing anything. With a print(item) statement in the penultimate line, all that is printed to the console is the contents of item, not the contents split into a new list. I feel like I'm missing something obvious, can anyone help? Thanks!
EDIT: So I've been informed that strings are immutable in Python, and in light of this replaced the 'item.split()' line with 'item = item.split()'. However, I am still running into the same error, even with item redefined as a new variable.
split() does not split the string inplace, it only returns a splitted string that you have to put in an other variable.

Jython: How to remove '\n' from the end of a string being read in [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python: How to remove /n from a list element?
I'm trying to read in data from text file but when the data is read in and added to an array, so is the '\n' because each set of data is on a new line.
for j in range(numStations):
allStations.insert(j, my_file2.readline())
Which results in an output of:
['H1\n', 'H2\n', 'H3\n', 'H4\n', 'H5\n', 'Harriston\n']
Did you try the normal way of getting rid of whitespace?
my_file2.readline().strip()
If you want to get crazy with Python syntax:
map(lambda x: x.rstrip('\n'), input_list)
This is equivalent to:
[x.rstrip('\n') for x in input_list]
I'm not sure which one is faster, though. I just wanted to use a lambda.
for i in range(numStations):
allStations.insert(j,my_file2.readline().rstrip())
for line in file:
data = line[:-1].split("\t")
I use this all the time for tab delimited data.

Categories

Resources