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
Do it in python please Boston Americans 1903 No Winner 1904 New York Giants 1905 Chicago...
Do it in python please Boston Americans 1903 No Winner 1904 New York Giants 1905 Chicago White Sox 1906 Chicago Cubs 1907 Chicago Cubs 1908 Pittsburgh Pirates 1909 Philadelphia Athletics 1910 Philadelphia Athletics 1911 Boston Red Sox 1912 Philadelphia Athletics 1913 Boston Braves 1914 Boston Red Sox 1915 Boston Red Sox 1916 Chicago White Sox 1917 Boston Red Sox 1918 Cincinnati Reds 1919 Cleveland Indians 1920 New York Giants 1921 New York Giants 1922 New York Yankees 1923 Washington Senators...
Please create a python module named homework.py and implement the functions outlined below. Below you will...
Please create a python module named homework.py and implement the functions outlined below. Below you will find an explanation for each function you need to implement. When you are done please upload the file homework.py to Grader Than. Please get started as soon as possible on this assignment. This assignment has many problems, it may take you longer than normal to complete this assignment. This assignment is supposed to test you on your understanding of reading and writing to a...
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