Python How Can I Correct My Python Regular Expressions Code for Analyzing Swallow Data?

AI Thread Summary
The discussion focuses on a user's attempt to learn Python, specifically regarding reading a text file and using regular expressions (regex) to filter data about swallows. The user successfully implemented file I/O but struggled with the regex part. They provided an initial code snippet that included a regex pattern to match specific data formats. Feedback highlighted the need for adjustments, such as enclosing patterns in quotation marks and correcting variable names from "match" to "record." Additionally, it was noted that regex is case-sensitive, and testing with smaller data samples is advisable to ensure accuracy before processing the entire file. Overall, the conversation emphasizes the importance of precise syntax and testing in mastering regex in Python.
Schr0d1ng3r
Messages
59
Reaction score
0

Homework Statement



This isn't really a homewrok/coursework question, but I didn't know where to put it. I'm trying to teach myself python over the summer and I seem to have got stuck on regular expressions. I want to read a .txt file with data for various kinds of swallows, laden/unlden, their airspeed velocities, and the date they were observed. Then, print only the ones which conform to the correct format. I managed to get the File I/O part to work, but the regex part doesn't.

2. The attempt at a solution

import re

# Open text file contining data
reader = open('C:\file.txt', 'r')
# Read all lines of data
data = reader.readlines()
# Print all lines of file
for line in data:
print line

# Close reader
reader.close()

print "\n\n\nShow Matches\n\n"

# Regular expression to find matching patterns in file
record = re.search((African|European) (Unladen|Laden) (\d{1,2}m/s) (\d{1-4}-\d{1,2}-\d{1,2})', data)
if record:
print match.search(1) + "\t" + match.search(2) + "\t" + match.search(3) + "\t" + match.search(4)

# Require input to close
x = raw_input("\n\nPress Enter")
 
Technology news on Phys.org


Hello there! It looks like you're on the right track with your code. Regular expressions can definitely be tricky, but with some practice and patience, you'll get the hang of it.

One thing I noticed is that your regular expression may need a few adjustments. The first thing is that you need to enclose your patterns in quotation marks, so it should look something like this: re.search('(African|European) (Unladen|Laden) (\d{1,2}m/s) (\d{1-4}-\d{1,2}-\d{1,2})', data). Also, in your second line of the regular expression, you have "match" instead of "record". It should be record.search(2) and so on.

Another thing to keep in mind is that regular expressions are case-sensitive, so make sure your patterns match the exact case of the data in your file.

I also recommend testing your regular expression with a smaller sample of data to make sure it's capturing the correct patterns before applying it to the entire file.

I hope this helps and good luck with your Python learning journey!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top