Created
April 9, 2011 17:24
-
-
Save zstumgoren/911574 to your computer and use it in GitHub Desktop.
Read in some data. Skip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
A simple script showing how to read data from a specified | |
file, do some basic processing, and then print out processed | |
lines to the shell. The input file is hard-coded in the text. | |
USAGE from command line | |
python simple_file_processing.py | |
""" | |
# Use a list comprehension to read in all the lines from your file | |
data = [line for line in open('/path/to/source_data.txt', 'rb')] | |
# Skip the first 5 lines using slice notation, and start processing from line 6 | |
# Remember that Python lists are zero-indexed, so the first line is index 0, | |
# second line is index 1, etc. | |
for line in data[5:]: | |
# Strip white space from start and end of line | |
clean_line = line.strip() | |
# Do some processing if the line contains text (ie is not a blank line) | |
if clean_line: | |
# Remove double-quotes and right parens, replace left parens with dash | |
# Notice that you can chain your string methods. | |
processed_line = clean_line.replace('"', '').replace(')','').replace('(','-') | |
print processed_line | |
else: | |
# This will print a blank line to shell | |
# If you don't want blank lines, you can remove this else clause | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment