Python Script Header Example For Essay

This will create a header for a Python script. It is a nice way keep a track of what your script does and when it was created, the author of the script, etc..

Update: Now, it will automatically open with one of the two most popular editors, Vim or Emacs! This script has been updated from the second version to replace spaces with underscores in the title, to convert uppercase to lowercase. It also checks to see if there is a script with the same name in the current working directory so it will not overwrite another file. Please leave comments and suggestions.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#!/usr/bin/env python#title :This will create a header for a python script.#author :bgw#date :20110930#version :0.4#usage :python :#python_version :2.6.6 #==============================================================================# Import the modules needed to run the script.fromos.pathimportexistsfromtimeimportstrftimeimportostitle=raw_input("Enter a title for your script: ")# Add .py to the end of the script.title=title+'.py'# Convert all letters to lower case.title=title.lower()# Remove spaces from the title.title=title.replace(' ','_')# Check to see if the file exists to not overwrite it.ifexists(title):print"\nA script with this name already exists."exit(1)descrpt=raw_input("Enter a description: ")name=raw_input("Enter your name: ")ver=raw_input("Enter the version number: ")div='======================================='# Create a file that can be written to.filename=open(title,'w')# Set the date"%Y%m%d")# Write the data to the file. filename.write('#!/usr/bin/python')filename.write('\n#title\t\t\t:'+title)filename.write('\n#description\t:'+descrpt)filename.write('\n#author\t\t\t:'+name)filename.write('\n#date\t\t\t:'+date)filename.write('\n#version\t\t:'+ver)filename.write('\n#usage\t\t\t:'+'python '+title)filename.write('\n#notes\t\t\t:')filename.write('\n#python_version\t:2.6.6')filename.write('\n#'+div*2+'\n')filename.write('\n')filename.write('\n')# Close the file after writing to it.filename.close()# Clear the screen. This line of code will not work on Windows.os.system("clear")defselect_editor():'''Open the file with either the Vim or Emacs editor.'''editor=raw_input("Select an number:\n\n1 for Vim.\n2 for Emacs.\n")ifeditor=="1"oreditor=="2":ifeditor=="1":os.system("vim +12 "+title)exit()elifeditor=="2":os.system("emacs +12 "+title)exit()else:os.system("clear")print"\nI do not understand your answer.\n"print"Press <Ctrl + C> to quit.\n"returnselect_editor()select_editor()

I use this script every day that I type some code. I thought that other people might find it useful.

Tags: 2, auto, create, emacs, header, linux, python, script, vim

How to read and write a CSV files

by Scott Davidson

Use Python to read and write comma-delimited files.

CSV (comma separated values ) files are commonly used to store and retrieve many different types of data. The CSV format is one of the most flexible and easiest format to read.

As an example, a CSV file might be used to store point locations in their X, Y, Z coordinate values:

Or a CSV might contain building data, for instance room and usage information in a database type format. Note that this CSV has a header titles on the first row:

The csv module in Python can be used to quickly parse CSV files into different data structures. In this sample the point coordinate file we will read into a list in which the X, Y and Z coordinates can be referenced by the index position in a list. The building data will be read in to a dictionary object so that the various values can be referenced by the header titles as named pieces of information.

Reading into a list

Here is an example script to read a CSV file into a list. Each row will become its own list and then can be referenced by the index position:

As usual the script starts with imports. This time the csv module is used.

Lines 2 thru 8 are standard lines from the Rhino.Python How to read and write a simple file guide.

At line 10 the file is opened for reading.

Then the key function in the script reads each row into a list of values into the variable :

The method does all the parsing of each line into a list. Then each value on each line into a list:

Manipulating each line of the list becomes easy:

In this case the values are split into 3 variables (x, y, z) and then printed to the console and additionally used to add a new point object to Rhino.

Reading a CSV file into a list is great for data that has the same number of values in each line and the position of the values are the same.
However sometimes it is easier to read csv values into a dictionary where the values have names.

Reading into a dictionary

The next sample script is very similiar to above.

This script uses the dictionary reader in the csv module on line 12:

The dictionary object contains key:value pairs where the name (key) of the values can be used to search through the data. The complete dictionary object looks like this:

Now since each value is preceeded by a key, values may be found by searching for the key names. For instance in this case the script prints the and from each line:

There are many ways to use dictionary data. Just to add to this example, here is code to add up all the square footage from the space in the building:

Here the statement loops through each row that contains ‘Retail’ and then will add the square footage for each into the variable. In the end it prints the total in the console.

By using the method, the simple CSV data can become a more sophisticated dictionary of information. This is a very powerful tool.

Writing CSV files

Writing a file is similar to reading, but the order in which things are done is different. This time we’ll look at the sample file.

Lines 1 thru 8 are standard lines from the Rhino.Python How to read and write a simple file guide.

There are a couple of things to notice compared to the previous example. We need some points to export, so we’ll let the user select some point objects. Then check that some points were picked:

Lines 14 thru 18 open the file for write. The script then runs through all the points in the list using the method. The write row method takes a list of values and adds the list as a line in the CSV file.

The method is great for writing out various values in the simplest CSV form.

Writing out a dictionary object

Like the read methods, there is also a way to write out a dictionary object to a CSV file. This is a great way to format any dictionary data into a spreadsheet or table format.

In lines 10-16 we set a list of dictionaries. The header names correspond with the key names in the dictionary. The class can write a header in the CSV using the same values in the keys to write the CSV.

The resulting CSV file will be formatted as follows:

For more information on CSV manipulation, see the csv module.

Related Topics

0 Thoughts to “Python Script Header Example For Essay

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *