Question

This is a Python related question. My returned output from a function used finditer with a...

This is a Python related question.
My returned output from a function used finditer with a regex pattern to present data from text file. For cities that were blank, there were two dashes like this: “- -“ Those two dashes are being returned for City (?P<city>) since city is blank and that’s the value showing for it in text file. That makes sense; however I need it to pull back only 1 dash.
Example:
When city present in text file, this is returned from my function which is good:
“{‘city’: ‘Boston‘}”
And it is returning “- -“ when city is blank because that is what shows in text file for blank city.
“{‘city’: ‘- -‘}”
However, when city is “- -“ in text file I need “-“ returned instead so it looks like this below:
“{‘city’: ‘- ‘}”
How can I get it to do that?
Does it have to be done from the regex pattern because it is very difficult to get it done there?
Can it be done with additional code to do replacement output of the one dash instead of the two?
Here is example code:
import re
def function( ):
with open(“textfile”,”r”) as file:
test = file.read
s = “”
pattern = r”(?P<city>.whatever pattern etc)”
for item in re.finditer(pattern,test,re.MULTILINE):
s=s+ str(item.groupdict())
return (s)
function()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def function( ):

with open(“textfile”,”r”) as file:

test = file.read

s = “”

pattern = r”(?P<city>.whatever pattern etc)”

for item in re.finditer(pattern,test,re.MULTILINE):

s=s+ str(item.groupdict())

if(s=="{‘city’: ‘- -‘}")

return str("{‘city’: ‘-‘}")

else:

return (s)

function()

Add a comment
Know the answer?
Add Answer to:
This is a Python related question. My returned output from a function used finditer with a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Sample program run - Needs to be coded in python -Enter the text that you want to search for, Or DONE when finished: val...

    Sample program run - Needs to be coded in python -Enter the text that you want to search for, Or DONE when finished: valiant paris The valiant Paris seeks for his love. -Enter the text that you want to search for, or DONE when finished: Romeo and Juliet Enter Romeo and Juliet aloft, at the WIndow -- Enter the text that you want to search for, DONE when finished: DONE   # copy the following two lines into any # program...

  • Your mission in this programming assignment is to create a Python program that will take an...

    Your mission in this programming assignment is to create a Python program that will take an input file, determine if the contents of the file contain email addresses and/or phone numbers, create an output file with any found email addresses and phone numbers, and then create an archive with the output file as its contents.   Tasks Your program is to accomplish the following: ‐ Welcome the user to the program ‐ Prompt for and get an input filename, a .txt...

  • Hi, i am working with MATLAB. I have a text file from which I have to...

    Hi, i am working with MATLAB. I have a text file from which I have to read in my data. So far i have this in my code. fid = fopen('ecgnormaloff.txt', 'r'); data = textscan(fid,'%f') According to all the websites, this should be enough to read in my points, however, I get this as my output data = 1×1 cell array {0×1 double} >> And when I try to access my data, there is nothing. I do not want to...

  • Hello, In Python, I am trying to create a function that will return the domain name...

    Hello, In Python, I am trying to create a function that will return the domain name from any inputted email address. For example, my input of john@domainname.com would return just the domainname. It needs to be done using regular expressions and the map function. I'm still stuck on trying to get the correct parameters for regular expression and also what is throwing it off. Thank you for your help! I am currently working with a list of different email addresses...

  • Write a program using python that reads from values from a text file and plots them...

    Write a program using python that reads from values from a text file and plots them using matplotlib. The input data for each graph is on three lines of the input textfile. The first line contains the x-coordiates of the points of the graph, and the second line contains the y-coordinates of the points of the graph that correspond, in the same order, to the x-coordinates on the first line. The third line in each group of three lines may...

  • This is my assignment prompt This is an example of the input and output This is...

    This is my assignment prompt This is an example of the input and output This is what I have so far What is the 3rd ToDo in the main.cpp for open the files and read the encrypted message? Does the rest of the code look accurate? Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a...

  • Question: I am unable to get a output of my function on my label1 when I...

    Question: I am unable to get a output of my function on my label1 when I click on button 1, please help? - x Lè DRAFT P1.py - C:\Users\Darren Louw\Desktop\DRAFT P1.py (3.8.2)* File Edit Format Run Options Window Help from tkinter import* HEIGHT=300 WIDTH=400 root=Tk) lowerframe=Frame (root, bg='green', bd=10) lowerframe.place (relx=0.5, rely=0.6, relwidth=0.75, relheight=0.2, anchor='n') labell-Label (lowerframe, bg='white', font=30) labell.place (relwidth=0.25, relheight=0.8, relx=0) def countl(): countl=0 for i in range (1,1001): num= i*i if num>1000: countl=countlul print('numbers in range 1...

  • Part 1 Encapsulate the following Python code from Section 7.5 in a function named my_sqrt that...

    Part 1 Encapsulate the following Python code from Section 7.5 in a function named my_sqrt that takes a as a parameter, chooses a starting value for x, and returns an estimate of the square root of a. while True: y = (x + a/x) / 2.0 if y == x: break x = y Part 2 Write a function named test_sqrt that prints a table like the following using a while loop, where "diff" is the absolute value of the...

  • The payMe function Display "What's the most you are willing to pay me for my advice?"...

    The payMe function Display "What's the most you are willing to pay me for my advice?" Then prompt the user with... "The more you pay, the better the answer! (minimum price: $975.46)" If the user enters a payment of less then that then return False from the function If the user enters a figure at the minimum or greater, format the number so that it displays appropriate decimals and commas (e.g. 12,456.45) and display the following... "I guess $12,456.45 seems...

  • in Python Objective: Practice Displaying Output with print Function, Comments, Variable, Reading Input from the Keyboard,...

    in Python Objective: Practice Displaying Output with print Function, Comments, Variable, Reading Input from the Keyboard, format output, if statement, if-else statement, loop structure. import, functions, file handling. and string methods. Lab Description: You are given a file containing protein sequences with the task of finding motifs. Motifs are certain patterns of amino acids that appear many times in protein sequences that act as indictors or markers for special regions, genes, mutations, etc. An example of what this file will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT