Question

Python Regular Expressions problem: How do you get string data from file of say rows of...

Python Regular Expressions problem:
How do you get string data from file of say rows of IP address and city location values obtained from re.finditer patterns to appear in the same dictionary?
for example:
text file has
100.200.10.255 New York City
10. 16. 25.254 Los Angeles

segment of code includes

for item in re.finditer(the_two_patterns, text_file, re.MULTILINE):
print (item.groupdict())

result is below that I am getting

{‘IP’: ‘100.200.10.255’, ‘city’: None}
{‘IP’: None, ‘city’: ‘New York City’}
{‘IP’: ‘10.16.25.254’, ‘city’: None}
{IP’: None, ‘city’: ‘Los Angeles’}

what I am wanting to see in results is IP and its associated city location appearing both in same dictionary output like this below

{‘IP’: ‘100.200.10.255’, ‘city’: ‘New York City’}
{‘IP’: ‘10.16.25.254’, ‘city’: ‘Los Angeles’}

any ideas on how to do that? with re.finditer and groupdict() ?

Homework Answers

Answer #1

Code. This pattern will give you the correct solution

Output.

import re
#creating a pattern
pattern=(r'(?P<IP>^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\W(?P<City>\w+(.)*\w+)')

with open('text.txt') as f: # reading the file
   data=f.read()
   for item in re.finditer(pattern,data,re.MULTILINE):
       print(item.groupdict())

Know the answer?
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
Similar Questions
Python A text file (WorldSeriesWinners.txt ) contains a chronological list of the World Series winning teams...
Python A text file (WorldSeriesWinners.txt ) contains a chronological list of the World Series winning teams from 1903 through 2009. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2009. Note the World Series was not played in 1904 and 1994. Write a program to open the file WorldSeriesWinners.txt, read it line by line and store the team names in...
What are 4 key things you learned about the topic from reading their paper? How does...
What are 4 key things you learned about the topic from reading their paper? How does the topic relate to you and your current or past job? Critique the paper in terms of the organization and quality. Team 3 answer questions above. Part I In today’s world we see fear among people when dealing with sexual harassment. This leads to people not reporting sexual harassment. A misconception about sexual harassment is that it’s only about touching and forcing other people...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT