I'm trying to make the for loop append on all the data set which is from the range of (0,8675) but it keeps getting error just working with 1 row
def convert_emoticons(text):
for emot in EMOTICONS:
text = re.sub(u'('+emot+')', "_".join(EMOTICONS[emot].replace(",","").split()), text)
return text
text = (dataset['posts'][0])
convert_emoticons(text)
Try using just emot:
def convert_emoticons(text):
for emot in EMOTICONS:
text = re.sub(u'('+emot+')', "_".join(emot.replace(",","").split()), text)
return text
text = (dataset['posts'][0])
convert_emoticons(text)
Related
How do I make it so that I get all the items inside a listbox into a variable? I only manage to get the last item only. I want to print the items of a listbox into a new window but I only print the last item only.
get_content = listbox2.get(0, END)
bb = Label(receipt_window, text = "Pizzeria")
line1 = Label(receipt_window, text = "----------------------------------------------------------------")
for con_item in get_content:
con = (con_item.split('PHP'))
con1 = con[0]
con2 = con[1]
rec_content = f'{con1:<40}costs PHP{con2:<8}'
receipt = Label(receipt_window, text = rec_content)
bb.pack()
line1.pack()
receipt.pack()
The listbox contains:
The result:
It is because you use same variable receipt for the labels created inside the for loop and call receipt.pack() outside the for loop, so the last label created inside for loop will be packed only.
You need to call receipt.pack() inside the for loop:
get_content = listbox2.get(0, END)
bb = Label(receipt_window, text='Pizzeria')
line1 = Label(receipt_window, text='----------------------------------------------------------------')
bb.pack()
line1.pack()
for con_item in get_content:
con1, con2 = con_item.split('PHP')
rec_content = f'{con1:<40}costs PHP{con2:<8}'
receipt = Label(receipt_window, text=rec_content)
receipt.pack()
Note that it is better to use fixed width (monospaced) font for the labels.
I'm trying to apply a preprocessor to remove html tags from the fileds of a Tensorflow dataset. I use this in the implementation of t5 which uses segio.
in my_preprocessor if I use text = row[field_index], it works okay and in training, it prints several rows and continues, but if I use text = normalize_text(row[field_index]) it hangs on an infinite or long loop as if it wants to apply it on the whole dataset rows!
def normalize_text(text):
"""Lowercase and remove quotes and tags from a TensorFlow string."""
text = tf.strings.lower(text)
text = tf.strings.regex_replace(text,"'(.*)'", r"\1")
text = tf.strings.regex_replace(text,"<[^>]+>", " ")
return text
def make_add_field_names_preprocessor(
field_names: Sequence[str], field_indices: Optional[Sequence[int]] = None,
) -> Callable:
def my_preprocessor(ds):
def to_inputs_and_targets(*row):
ret = {}
count=0
tf.print("=======================")
for field_name, field_index in zip(field_names, field_indices):
# if I use this, it works okay
text = row[field_index]
# if I use the following it falls to a long or endless loop
text = normalize_text(row[field_index])
tf.print(count, "Row:",text)
ret[field_name] = text
count+=1
return ret
return ds.map(to_inputs_and_targets,
num_parallel_calls=tf.data.experimental.AUTOTUNE)
return my_preprocessor
Update: The preprocessor gets two field names, "inputs" for row[1], and "targets" for row[2]. I noticed that the problem exists many when I call normalize_text on row[2]
I add a add_hyperlink function in my code and I can add hyperlinks in to my document with this function but when I want to change their color, I can't do that. Here is the function:
def add_hyperlink(paragraph, url, text):
part = paragraph.part
r_id = part.relate_to(url, docx.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_external=True)
hyperlink = docx.oxml.shared.OxmlElement('w:hyperlink')
hyperlink.set(docx.oxml.shared.qn('r:id'), r_id, )
new_run = docx.oxml.shared.OxmlElement('w:r')
rPr = docx.oxml.shared.OxmlElement('w:rPr')
new_run.append(rPr)
new_run.text = text
hyperlink.append(new_run)
paragraph._p.append(hyperlink)
return hyperlink
I tried to change it with using
hyperl = add_hyperlink(paragraph, 'https://stackoverflow.com', 'stackoverflow')
hyperl.font.color.rgb = RGBColor(0x99, 0x99, 0x99)
But it didn't work. I'm trying to do this for days. Is it possible to change the color or the style of the hyperlink from code?
I solved the problem. I added these lines top of the return hyperlink line in the function
r = paragraph.add_run()
r._r.append (hyperlink)
r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK
I'm new to Python3 and coding. I'am get stuck with a simple function.
import datetime
date = str(datetime.date.today())
mail = ""
text = (f"""
some text {mail}
some text {date}
some text
""")
print(text)
def get_deadline_date():
mail = "a#a.com"
print(text)
And I have
some text
some text 2019-03-21
some text
some text
some text 2019-03-21
some text
I can't change the text variable.
It should print:
some text a#a.com
some text 2019-03-21
some text
As I understand, I made a simple mistake, but I can't figure it out.
I tried,
import datetime
date = str(datetime.date.today())
mail = ""
text = (f"""
some text {mail}
some text {date}
some text
""")
print(text)
def get_deadline_date():
global mail
mail = "a#a.com"
get_deadlin
It gave the same output.
Your function is defined but not executed. And, if it was executed, it would not change the value of mail because it has it's own namespace of variables, so it could access mail but not set it. What you are doing there is declaring a new variable, also called mail in that function. On top of that, your text is already set and will not update when you chnge the value of mail. I suggest this solution:
text = "some text {mail}\n" \
"some text {date}\n" \
"some text"
def make_text(text, date):
mail = "a#a.com"
return text.format(mail=mail, date=date)
text = make_text(text=text, date=date.today())
You may also want to make separate functions for setting mail and making the text, but remember to return the new mail you make to the outer scope.
Mistake 1: You are not calling the function
Mistake 2: mail is local variable (to function)
Mistake 3: String is already evaluated/initialised
Bad, but your solution should be
import datetime
date = str(datetime.date.today())
mail = ""
text = """
some text {0}
some text {1}
some text
""".format
print(text(mail, date))
def get_deadline_date():
global mail
mail = "a#a.com"
get_deadline_date()
print(text(mail, date))
Avoid global variable
Dedesign your code to avoid global variable, use function parameters and return values instead.
I'm creating a simple text editor in Python 3.4 and Tkinter. At the moment I'm stuck on the find feature.
I can find characters successfully but I'm not sure how to highlight them. I've tried the tag method without success, error:
str object has no attribute 'tag_add'.
Here's my code for the find function:
def find(): # is called when the user clicks a menu item
findin = tksd.askstring('Find', 'String to find:')
contentsfind = editArea.get(1.0, 'end-1c') # editArea is a scrolledtext area
findcount = 0
for x in contentsfind:
if x == findin:
findcount += 1
print('find - found ' + str(findcount) + ' of ' + findin)
if findcount == 0:
nonefound = ('No matches for ' + findin)
tkmb.showinfo('No matches found', nonefound)
print('find - found 0 of ' + findin)
The user inputs text into a scrolledtext field, and I want to highlight the matching strings on that scrolledtext area.
How would I go about doing this?
Use tag_add to add a tag to a region. Also, instead of getting all the text and searching the text, you can use the search method of the widget. I will return the start of the match, and can also return how many characters matched. You can then use that information to add the tag.
It would look something like this:
...
editArea.tag_configure("find", background="yellow")
...
def find():
findin = tksd.askstring('Find', 'String to find:')
countVar = tk.IntVar()
index = "1.0"
matches = 0
while True:
index = editArea.search(findin, index, "end", count=countVar)
if index == "": break
matches += 1
start = index
end = editArea.index("%s + %s c" % (index, countVar.get()))
editArea.tag_add("find", start, end)
index = end