Created
October 2, 2010 14:10
-
-
Save micktwomey/607669 to your computer and use it in GitHub Desktop.
python basics
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
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
scheme number | |
scheme1 5.23456 | |
scheme2 563.7655 | |
scheme3 34534.734534 | |
scheme4 34534.76546756 |
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
data = { | |
"answer": 10, | |
"answer2": 20, | |
} | |
data["answer3"] = data["answer"] * data["answer2"] | |
html = """<htm> | |
<head><title>...</title></head> | |
<body> | |
<p>The answer = %(answer)d and %(answer2)d</p> | |
<p>calculated %(answer3)d</p> | |
</body> | |
</htm>""" | |
print html % data |
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
import csv | |
fp = open("/Users/mick/Desktop/data.txt") | |
for row in csv.DictReader(fp, delimiter="\t"): | |
print row | |
# {'scheme': 'scheme1', 'number': '5.23456'} | |
# {'scheme': 'scheme2', 'number': '563.7655'} | |
# {'scheme': 'scheme3', 'number': '34534.734534'} | |
# {'scheme': 'scheme4', 'number': '34534.76546756'} |
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
from flask import Flask | |
app = Flask(__name__) | |
@app.route("/") | |
def hello(): | |
data = { | |
"answer": 10, | |
"answer2": 20, | |
} | |
data["answer3"] = data["answer"] * data["answer2"] | |
html = """<htm> | |
<head><title>...</title></head> | |
<body> | |
<p>The answer = %(answer)d and %(answer2)d</p> | |
<p>calculated %(answer3)d</p> | |
</body> | |
</htm>""" | |
return html % data | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment