Python - exclude data with double quotes and parenthesis - python-3.x

I have a list which contains set of words in single quotes and double quotes, now i want to grep all the data only in single quotes.
Input_
sentence = ['(telco_name_list.event_date','reference.date)',"'testwebsite.com'",'data_flow.code',"'/long/page/data.jsp'"]
Output:
telco_name_list.event_date,reference.date,data_flow.code
I want to exclude the parenthesis and string in double quotes.

Since you tagged the post as python-3.x, I'm assuming you want to write python code. Based on your example this would work:
#!/usr/bin/env python3
Input_sentence = ['(telco_name_list.event_date','reference.date)',"'testwebsite.com'",'data_flow.code',"'/long/page/data.jsp'"]
comma = False
result = []
for i in range(len(Input_sentence)):
e = Input_sentence[i]
if e.startswith("'"): continue
e = e.replace('(', '').replace(')', '')
if comma:
print(",", end="")
comma = True
print(e, end="")
print()
This code iterates through the list, ignoring elements which begin with a single quote and filtering out parentheses from elements before printing them on stdout. This code works for the given example, but may or may not be what you need as the exact semantics of what you want out are somewhat ambiguous (i.e. is it fine to filter out all parentheses or just the ones at the beginning and end of your elements?).

Related

How do i find/count number of variable in string using Python

Here is example of string
Hi {{1}},
The status of your leave application has changed,
Leaves: {{2}}
Status: {{3}}
See you soon back at office by Management.
Expected Result:
Variables Count = 3
i tried python count() using if/else, but i'm looking for sustainable solution.
You can use regular expressions:
import re
PATTERN = re.compile(r'\{\{\d+\}\}', re.DOTALL)
def count_vars(text: str) -> int:
return sum(1 for _ in PATTERN.finditer(text))
PATTERN defines the regular expression. The regular expression matches all strings that contain at least one digit (\d+) within a pair of curly brackets (\{\{\}\}). Curly brackets are special characters in regular expressions, so we must add \. re.DOTALL makes sure that we don't skip over new lines (\n). The finditer method iterates over all matches in the text and we simply count them.

How can i remove (') from a string result, in order to use in subproduct i need not to have (')

Im creating a name based on different inputs by the user, it constructs this way:
Result = str('Blue' + shoetype.result + '5')
Result = 'Bluesandal5'
now i need to use 'Bluesandal5' in another operation, but i need it with out the ('), just Bluesandal5
how can I achieve this?
The ' characters are there for the specific purpose of telling you that it's a string. They disappear when you actually use the string for something. Example, using my python console:
>>> Result = "Bluesandal5"
>>> Result
'Bluesandal5'
>>> print(Result)
Bluesandal5
As you can see, the quotes disappear when it's used in a print() statement. This also holds true for any other operation (e.g. string slicing) - they don't count as part of the string itself.

re.sub replacing string using original sub-string

I have a text file. I would like to remove all decimal points and their trailing numbers, unless text is preceding.
e.g 12.29,14.6,8967.334 should be replaced with 12,14,8967
e.g happypants2.3#email.com should not be modified.
My code is:
import re
txt1 = "9.9,8.8,22.2,88.7,morris1.43#email.com,chat22.3#email.com,123.6,6.54"
txt1 = re.sub(r',\d+[.]\d+', r'\d+',txt1)
print(txt1)
unless there is an easier way of completing this, how do I modify r'\d+' so it just returns the number without a decimal place?
You need to make use of groups in your regex. You put the digits before the '.' into parentheses, and then you can use '\1' to refer to them later:
txt1 = re.sub(r',(\d+)[.]\d+', r',\1',txt1)
Note that in your attempted replacement code you forgot to replace the comma, so your numbers would have been glommed together. This still isn't perfect though; the first number, since it doesn't begin with a comma, isn't processed.
Instead of checking for a comma, the better way is to check word boundaries, which can be done using \b. So the solution is:
import re
txt1 = "9.9,8.8,22.2,88.7,morris1.43#email.com,chat22.3#email.com,123.6,6.54"
txt1 = re.sub(r'\b(\d+)[.]\d+\b', r'\1',txt1)
print(txt1)
Considering these are the only two types of string that is present in your file, you can explicitly check for these conditions.
This may not be an efficient way, but what I have done is split the str and check if the string contains #email.com. If thats true, I am just appending to a new list. For your 1st condition to satisfy, we can convert the str to int which will eliminate the decimal points.
If you want everything back to a str variable, you can use .join().
Code:
txt1 = "9.9,8.8,22.2,88.7,morris1.43#email.com,chat22.3#email.com,123.6,6.54"
txt_list = []
for i in (txt1.split(',')):
if '#email.com' in i:
txt_list.append(i)
else:
txt_list.append(str(int(float(i))))
txt_new = ",".join(txt_list)
txt_new
Output:
'9,8,22,88,morris1.43#email.com,chat22.3#email.com,123,6'

switching between different quotation marks for output

how can i get my code to change the quotation marks of my output. i saw some references that mentioned json, but i think i need to write it myself.
so i'll post the question and then my code:
Program: quote_me() Function
quote_me takes a string argument and returns a string that will display surrounded with added double quotes if printed
check if passed string starts with a double quote ("\""), then surround string with single quotations
if the passed string starts with single quote, or if doesn't start with a quotation mark, then surround with double quotations
Test the function code passing string input as the argument to quote_me()
[ ] create and test quote_me()
def quote_me (word):
if word == ("\'"):
str(word).replace ("\'", '\"')
else:
return word
print (quote_me(input ("what is the sentence: ")))
maybe i've misunderstood what is required as well, if that's the case, please do tell.
def quote_me(word):
if word.startswith("\"") and word.endswith("\""):
word = word.replace("\"","\'")
elif word.startswith("\'") and word.endswith("\'"):
word = word.replace("\'","\"")
else:
word = "\""+word+"\""
return word

Replace two backslashes with a single backslash

I want to replace a string with two backslashes with single backslashes. However replace doesn't seem to accept '\\' as the replacement string. Here's the interpreter output:
>>> import tempfile
>>> temp_folder = tempfile.gettempdir()
>>> temp_folder
'C:\\Users\\User\\AppData\\Local\\Temp'
>>> temp_folder.replace('\\\\', '\\')
'C:\\Users\\User\\AppData\\Local\\Temp'
BTW, I know that Windows paths need to contain either double backslashes or a single forward slashes. I want to replace them anyway for display purposes.
Your output doesn't have double backslashes. What you are looking at is the repr() value of the string and that displays with escaped backslashes. Assuming your temp_folder would have double backslashes, you should instead use:
print(temp_folder.replace('\\\\', '\\'))
and that will show you:
C:\Users\User\AppData\Local\Temp
which also drops the quotes.
But your temp_folder is unlikely to have double backslashes and this difference in display probably got you thinking that there are double backslashes in the return value from tempfile.gettempdir(). As #Jean-Francois indicated, there should not be (at least not on Windows). So you don't need to use the .replace(), just print:
print(temp_folder)
This works for me
text = input('insert text')
list = text.split('\\')
print(list)
text2 = ''
for items in list:
if items != '':
text += items + '\\'
print(text2)

Resources