Stripping '../' from the front of a string [duplicate] - python

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
I have a text file that looks like this:
../../../../foo/bar
../../this/that
../barfoo
and I want:
foo/bar
this/that
barfoo
with open('file_list.txt', 'r') as file_list:
for file_list_lines in file_list:
file_list_lines.lstrip('../')
print(file_list_lines)
I tried .lstrip('../') but nothing was stripped from the beginning of the line.

The string.lstrip() does not do the string manipulation in place. In other words, you would need to store it into a variable like so:
stripped_line = file_list_lines.lstrip('../')
print( stripped_line )
In your version, you did the lstrip, but did not store the result of that operation anywhere.

Related

How to extract the filename from a string using regular expression [duplicate]

This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Extract file name with a regular expression
(3 answers)
Get Filename Without Extension in Python
(6 answers)
Extracting extension from filename in Python
(33 answers)
Regex: Get Filename Without Extension in One Shot?
(10 answers)
Closed 3 years ago.
I am new in the regular expression and trying to extract the file name from a string which is basically a file path.
string = "input_new/survey/argentina-attributes.csv"
string_required = argentina-attributes
i know i can do this by below code.
string.split('/')[2].split('.')[0]
but I am looking to do this by using regular expression so if in future the formate of the path changes(input_new/survey/path/path/argentina-attributes.csv) should not affect the output.
i know kind of similar question asked before but I am looking for a pattern which will work for my use case.
Try this,
>>> import re
>>> string = "input_new/survey/argentina-attributes.csv"
Output:
>>> re.findall(r'[^\/]+(?=\.)',string) # or re.findall(r'([^\/]+)\.',string)
['argentina-attributes']
Referred from here
Try this:
string = "input_new/survey/argentina-attributes.csv"
new_string = string.split('/')[-1].split('.')[0]
print(new_string)

"\n" not working in python when reading files [duplicate]

This question already has answers here:
Passing meta-characters to Python as arguments from command line
(4 answers)
Closed 4 years ago.
Let's say I have an example file named 'greetings.txt' with this in it
Hello\nThere
and this code
f = open("greetings.txt", "r")
readit = f.read()
print(readit)
But the output is
Hello\nThere
What do I do to make the output detect the "\n" and put Word "There" to the 2nd line?
Thanks for your answers!
Try this:
print(readit.replace(r'\n','\n'))
(When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. See here)

Split string when ";" appears [duplicate]

This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 5 years ago.
I need to split the string everytime ; shows up.
words = "LightOn;LightOff;LightStatus;LightClientHello;"
Output should be something like this:
LightOn
LightOff
LightStatus
LightClientHello
Simply, everytime it finds ; in a string, it has to split it.
Thank you for help
res = words.split(";")
Refer to this link for more information on split.

Replacing newline doesn't work [duplicate]

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 7 months ago.
I have a .txt file form which I read multiple lines and append an array with every line.
Unfortunately I also have the line breaks in the array.
When I try to replace them with line.replace("\n", ""), nothing will happen.
Are you just doing line.replace("\n", "")? If so, that's the problem. You are doing the replacement, then throwing away the result. You need:
line = line.replace("\n", "")
I had the same problem, but even saving the result in another variable. I started breaking it in code units to find the problem and I found that my input had carriage returns, which is '\r'. They made visually the same result as the '\n' in the output file. So I fixed it by doing the following:
result = input.replace("\n", "").replace("\r", "");

cut the right side of a '=' delimited string and storing into a list in python [duplicate]

This question already has answers here:
How to get part of string and pass it to other function in python?
(4 answers)
Closed 8 years ago.
A beginner question
My file looks like -->
10.5.5.81=apache,php,solr
10.5.5.100=oracle,coherence
How can I cut the IP part and store it into a list for further processing?
Please help.
answer = []
with open('path/to/file') as infile:
for line in infile:
answer.append(line.partition('=')[0].strip())

Categories