# Python 3
import os
import sys
import fileinput
import shutil
newcontent = []
with open('test.txt',"r") as f1:
with open('intermediate.txt',"w") as f2:
content = f1.readlines()
for i in range(1, len(content)+1):
if i % 2 == 0: f2.write(content[i-2].rstrip() + ";" + content[i-1].rstrip())
# opening the file in read mode
my_file = open("intermediate.txt", "r")
# reading the file
data = my_file.read()
# replacing end of line('/n') with ' ' and
# splitting the text it further when ';' is seen.
data_into_list = data.split(";")
my_file.close()
key = data_into_list[::2]
value = data_into_list[1::2]
callsummary = dict(zip(key,value))
callsummary.pop('', None)
# Print dictionary to file:
f = open("callsum_dict.txt", "w")
f.write("{\n")
for k in callsummary.keys():
f.write("'{}':'{}'\n".format(k, callsummary[k]))
f.write("}")
f.close()
### WC3 Clean Up
# Read in the file
with open('callsum_dict.txt', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace("{", "")
filedata = filedata.replace("}", "")
filedata = filedata.replace("'", "")
# Write the file out again
with open('callsum_output.txt', 'w') as file:
file.write(filedata)
LPB Example: Arizona Lost Person Behavior Distance Traveled Data
mission = input('What is the mission? (i.e. Aircraft, ELT, PLB, Search, Vehicle, or Water)\n')
lpb_cat = input('What is the search category? Refer to list\n')
from csv import reader
from csv import DictReader
with open('az_lpb.csv', 'r') as read_obj:
csv_dict_reader = DictReader(read_obj)
for row in csv_dict_reader:
if (row['Mission']) == mission:
if (row['Category']) == lpb_cat:
print(row['Mission'] + ' / ' + row['Category'] + ' >> ' + row['0.25'] + ',' + row['0.5'] + ',' + row['0.75'])
print('')
print('Distance Traveled')
print('____________________________________________')
print(row['0.25'] + ' miles - 25% ' + row['Category'])
print(row['0.5'] + ' miles - 50% ' + row['Category'])
print(row['0.75'] + ' miles - 75% ' + row['Category'])
else:
print('')
Sample Routines for Further Development:
from csv import reader
from csv import DictReader
def main():
print('*** Read csv file line by line using csv module reader object ***')
print('*** Iterate over each row of a csv file as list using reader object ***')
# open file in read mode
with open('az_lpb.csv', 'r') as read_obj:
# pass the file object to reader() to get the reader object
csv_reader = reader(read_obj)
# Iterate over each row in the csv using reader object
for row in csv_reader:
# row variable is a list that represents a row in csv
print(row)
print('*** Read csv line by line without header ***')
# skip first line i.e. read header first and then iterate over each row od csv as a list
with open('az_lpb.csv', 'r') as read_obj:
csv_reader = reader(read_obj)
header = next(csv_reader)
# Check file as empty
if header != None:
# Iterate over each row after the header in the csv
for row in csv_reader:
# row variable is a list that represents a row in csv
print(row)
print('Header was: ')
print(header)
print('*** Read csv file line by line using csv module DictReader object ***')
# open file in read mode
with open('az_lpb.csv', 'r') as read_obj:
# pass the file object to DictReader() to get the DictReader object
csv_dict_reader = DictReader(read_obj)
# iterate over each line as a ordered dictionary
for row in csv_dict_reader:
# row variable is a dictionary that represents a row in csv
print(row)
print('*** select elements by column name while reading csv file line by line ***')
# open file in read mode
with open('az_lpb.csv', 'r') as read_obj:
# pass the file object to DictReader() to get the DictReader object
csv_dict_reader = DictReader(read_obj)
# iterate over each line as a ordered dictionary
for row in csv_dict_reader:
# row variable is a dictionary that represents a row in csv
print(row['Name'], ' is from ' , row['City'] , ' and he is studying ', row['Course'])
print('*** Get column names from header in csv file ***')
# open file in read mode
with open('az_lpb.csv', 'r') as read_obj:
# pass the file object to DictReader() to get the DictReader object
csv_dict_reader = DictReader(read_obj)
# get column names from a csv file
column_names = csv_dict_reader.fieldnames
print(column_names)
print('*** Read specific columns from a csv file while iterating line by line ***')
print('*** Read specific columns (by column name) in a csv file while iterating row by row ***')
# iterate over each line as a ordered dictionary and print only few column by column name
with open('students.csv', 'r') as read_obj:
csv_dict_reader = DictReader(read_obj)
for row in csv_dict_reader:
print(row['Id'], row['Name'])
print('*** Read specific columns (by column Number) in a csv file while iterating row by row ***')
# iterate over each line as a ordered dictionary and print only few column by column Number
with open('students.csv', 'r') as read_obj:
csv_reader = reader(read_obj)
for row in csv_reader:
print(row[1], row[2])
if __name__ == '__main__':
main()