<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=I719_Fundamentals_of_Python%2Flecture3</id>
	<title>I719 Fundamentals of Python/lecture3 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=I719_Fundamentals_of_Python%2Flecture3"/>
	<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture3&amp;action=history"/>
	<updated>2026-05-07T04:34:51Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture3&amp;diff=117865&amp;oldid=prev</id>
		<title>Eroman: add lecture notes</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture3&amp;diff=117865&amp;oldid=prev"/>
		<updated>2017-02-17T17:21:46Z</updated>

		<summary type="html">&lt;p&gt;add lecture notes&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Lecture 3 =&lt;br /&gt;
&lt;br /&gt;
Lets review! - lists - dictionaries - strings - comparison operators&lt;br /&gt;
&lt;br /&gt;
== Command line arguments ==&lt;br /&gt;
&lt;br /&gt;
The standard library has &amp;lt;code&amp;gt;sys&amp;lt;/code&amp;gt; which give us access to the command line arguments executed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import sys&lt;br /&gt;
&lt;br /&gt;
if &amp;#039;-h&amp;#039; in sys.argv or &amp;#039;--help&amp;#039; in sys.argv:&lt;br /&gt;
    print(&amp;#039;Sorry I can\&amp;#039;t help you :(&amp;#039;)&amp;lt;/source&amp;gt;&lt;br /&gt;
But &amp;lt;code&amp;gt;argparse&amp;lt;/code&amp;gt; from the standard library is much easier if we want to use the commandline arguments.&lt;br /&gt;
&lt;br /&gt;
== File opening ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;f = open(filename)&lt;br /&gt;
# interact with the file&lt;br /&gt;
f.close()&lt;br /&gt;
&lt;br /&gt;
# is the same as&lt;br /&gt;
with open(filename) as f:&lt;br /&gt;
    # interact with the file&amp;lt;/source&amp;gt;&lt;br /&gt;
here is an example of a script that opens a file, and print all lines in the file. The file name is specified as the first command line argument&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
Print all lines in file&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
import argparse&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    parser = argparse.ArgumentParser()&lt;br /&gt;
    parser.add_argument(&amp;#039;file_name&amp;#039;)&lt;br /&gt;
    args = parser.parse_args()&lt;br /&gt;
    text_file = open(args.file_name)&lt;br /&gt;
    for line in text_file:&lt;br /&gt;
        cleaned_line = line.strip() # Remove newline, i.e. &amp;#039;\n&amp;#039; or &amp;#039;\r\n&amp;#039;&lt;br /&gt;
        print(cleaned_line)&lt;br /&gt;
    text_file.close()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
    main()&amp;lt;/source&amp;gt;&lt;br /&gt;
== TASK 1 ==&lt;br /&gt;
&lt;br /&gt;
Print out a dictionary with words and the number of occurences of the word.&lt;br /&gt;
&lt;br /&gt;
Example output:&lt;br /&gt;
&lt;br /&gt;
{&amp;#039;Apple&amp;#039;: 10, &amp;#039;Bananan&amp;#039;: 1}&lt;br /&gt;
&lt;br /&gt;
=== How to count words in a list of words ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; words = [&amp;#039;hello&amp;#039;, &amp;#039;hi&amp;#039;, &amp;#039;hello&amp;#039;]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; wc = {}&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; for word in words:&lt;br /&gt;
...     if word in wc:&lt;br /&gt;
...         wc[word] = wc[word] + 1&lt;br /&gt;
...     else:&lt;br /&gt;
...         wc[word] = 1&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; wc&lt;br /&gt;
{&amp;#039;hello&amp;#039;: 2, &amp;#039;hi&amp;#039;: 1}&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
== Task 2 ==&lt;br /&gt;
&lt;br /&gt;
Make a new csv file and save it, where the age become year born.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
convert input csv to output csv&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
import argparse&lt;br /&gt;
import csv&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def convert_age_in_csv(data_in, data_out):&lt;br /&gt;
    reader = csv.reader(data_in)&lt;br /&gt;
    writer = csv.writer(data_out)&lt;br /&gt;
    for row in reader:&lt;br /&gt;
        name = row[0].strip()&lt;br /&gt;
        age = int(row[1])&lt;br /&gt;
        year_born = 2017 - age&lt;br /&gt;
        writer.writerow([name, year_born])&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    parser = argparse.ArgumentParser()&lt;br /&gt;
    parser.add_argument(&amp;#039;data_in&amp;#039;)&lt;br /&gt;
    parser.add_argument(&amp;#039;data_out&amp;#039;)&lt;br /&gt;
    args = parser.parse_args()&lt;br /&gt;
&lt;br /&gt;
    with open(args.data_in, &amp;#039;r&amp;#039;) as data_in:&lt;br /&gt;
        with open(args.data_out, &amp;#039;w&amp;#039;) as data_out:&lt;br /&gt;
            convert_age_in_csv(data_in, data_out)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
How to execute&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;shell&amp;quot;&amp;gt;python3 csvconverter.py input.csv output.csv&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Eroman</name></author>
	</entry>
</feed>