I have a syntax issue while using dictionary for the first time. I hope you guys can help me with it. I'm reading election data from a .csv file. I'm trying to put the names and votes for each candidate into a dictionary so the display would look like this:
Election Results
-------------------------
Total Votes: 369711
-------------------------
Charles Casper Stockham: 23.049% (85213)
Diana DeGette: 73.812% (272892)
Raymon Anthony Doane: 3.139% (11606)
Here are my codes below. I have already captured the votes for each candidate in each sum variable. I'm just trying to figure out what's the syntax for putting them in the dictionary so they look the display above.
# Import the os module
import os
# Module for reading CSV files
import csv
# Store the file path associated with the file
csvpath = os.path.join('Resources', 'election_data.csv')
# Open the file "read mode" and store contents in the variable
with open(csvpath) as csvfile:
csvreader = csv.reader(csvfile, delimiter =',')
print(csvreader)
csv_header = next(csvreader)
total_ballots = 0
sumCharles = sumDiana = sumRaymon = 0
candidates = dict()
candidates_list = ["Charles Casper Stockham","Diana DeGette","Raymon Anthony Doane"]
candidates["names"] = candidates_list
votes_list = [sumCharles , sumDiana , sumRaymon]
candidates["votes"] = votes_list
for row in csvreader:
total_ballots = total_ballots + 1
if (row[2] == "Charles Casper Stockham"):
sumCharles = sumCharles + 1
if (row[2] == "Diana DeGette"):
sumDiana = sumDiana + 1
if (row[2] == "Raymon Anthony Doane"):
sumRaymon = sumRaymon + 1
print(candidates)
print(total_ballots)
Assuming that the element in position 2 is the name of the candidate and that each line in the csv is a single vote:
# Import the os module
import os
# Module for reading CSV files
import csv
# Store the file path associated with the file
csvpath = os.path.join('Resources', 'election_data.csv')
# Open the file "read mode" and store contents in the variable
with open(csvpath) as csvfile:
csvreader = csv.reader(csvfile, delimiter =',')
candidates = dict()
total_ballots = 0
for row in csvreader:
total_ballots += 1
candidates[row[2]] += 1
print("Election Results")
print("-"*25)
print("Total Votes:", total_ballots)
print("-"*25)
for candidate, votes in candidates.items():
percentage = votes / total_ballots * 100
print("{}: {:.3f}% ({})".format(candidate, percentage, votes)
Related
I have a following text-file products.txt:
Product;Amount;Price
Apple;3;10.00
Banana;1;5.00
Lemon;2;3.00
Orange;4;20.00
Apple;4;8.00
I want read this file and make a new text-file newfile.txt, which contains value of each row (Amount X Price):
30.00
5.00
6.00
80.00
32.00
Finally, I want to find the total sum of newfile.txt (which is 30+5+6+80+32 = 153)
Note, the price of same product can vary and we are not interested total sum of each product.
I started with creating class.
class DATA:
product= ""
amount= 0
price= 0
def read (name):
list = []
file= open(name, 'r', encoding="UTF-8")
file.readline()
while (True):
row= file.readline()
if(rivi == ''):
break
columns= row[:-1].split(';')
info= DATA()
info.amount= int(columns[1])
info.price= int(columns[2])
info.total = info.amount * info.price
file.append(info)
tiedosto.close()
return list
This should work:
def read(name):
total = 0
ori = open(name, 'r', encoding="UTF-8")
row = ori.readline()
dest = open("newfile.txt", 'w', encoding="UTF-8")
row = ori.readline()
while (row != ""):
row = row[:-1].split(';')
res = int(row[1]) * float(row[2])
total += res
dest.write(str(res) + "\n")
row = ori.readline()
ori.close()
dest.close()
print(total)
read("products.txt")
A possibility would be to use csv from the standard library.
import csv
# fix files' paths
path1 = # file to read
path2 = # file to write
# read data and perform computations
rows_tot = []
with open(path1, 'r', newline='', encoding="utf-8") as fd:
reader = csv.DictReader(fd, delimiter=";")
for row in reader:
rows_tot.append(float(row['Amount']) * float(row['Price']))
# total sum
print("Total sum:", int(sum(rows_tot)))
# save to file the new data
with open(path2, 'w', newline='') as fd:
fieldnames = ("AmountXPrice",)
writer = csv.DictWriter(fd, fieldnames=fieldnames)
writer.writeheader()
for value in rows_tot:
writer.writerow({fieldnames[0]: f"{value:.2f}"})
Remark: it is not clear from the question the type of the various data, in case just change int with float or the other way around.
I am trying to read for a CSV file to print to another CSV. What I need is to get the specific line in a row.
csv source is:
hotdog, 3.99
burger, 4.99
ice cream, 2.50
soda, 1.50
What I need is to get the amount of each row only for specific row and the amount. Say I need to get the amount of hotdog to get the total value from the Entry box.
My code is below:
import tkinter
import tkinter.messagebox
import csv
item = []
def checkout(): # Checkout button function
"""write the receipt"""
with open("order.csv", "r") as file:
reader = file.read() # to get the total prices on checkout
#for line in file_contents:
# milk = line[1]
global root, hotdogbox, burgerbox, icecreambox, sodabox
with open("writereceipt.csv", "w") as file:
receipt = csv.writer(file, lineterminator = "\n")
for item in reader:
hotdog = item
for row in hotdog:
item.append(row)
total_hotdog = int(hotdogbox.get())
if total_hotdog >= 1:
hotdog_cost = total_hotdog * hotdog
hotdog_total = ["Milk: ", total_hotdog, hotdog_cost]
space = ["----------","--", "------------"]
total = hotdog_cost
total_output = ["Total: ", "", total]
receipt.writerow(hotdog_total)
receipt.writerow(total_output)
My code is not working...... It will print MM as total...
I have extracted the data using python Selenium from the site below.
https://portfoliomanager.energystar.gov/pm/targetFinder;jsessionid=F6FC40FBDE075BDA3834643F9BD65E37?execution=e1s2
Please have a look at the table "Metrics Comparison for Your Design and/or Target".
I have extracted the table as a text format.
Here is the sample output of the text below
Metric Design Project Design Target Median Property*
ENERGY STAR score (1-100) Not Available 75 50
Source EUI (kBtu/ft²) 3.1 Not Available 127.9
Site EUI (kBtu/ft²) 1.0 Not Available 40.7
Source Energy Use (kBtu) 314.0 Not Available 12,793.0
Site Energy Use (kBtu) 100.0 Not Available 4,074.2
Energy Cost ($) 2,000.00 Not Available 81,484.00
Total GHG Emissions (Metric Tons CO2e) 0.0 Not Available 0.5
I tried to convert the text into json,
import csv
import json
with open('file.txt', 'rb') as csvfile:
filereader = csv.reader(csvfile, delimiter=' ')
i = 0
header = []
out_data = []
for row in filereader:
row = [elem for elem in row if elem]
if i == 0:
i += 1
header = row
else:
row[0:4] = [row[0]+" "+row[1]+" "+row[2]+" "+row[3]]
_dict = {}
for elem, header_elem in zip(row, header):
_dict[header_elem] = elem
out_data.append(_dict)
print json.dumps(out_data)
The JSON format output which i got was like
[{"Project": "75", "Metric": "ENERGY STAR score (1-100)", "Design": "50"}]
The JSON format output should be in the form of
[{"Design Project": "Not Available", "Design Target": "75", "Metric": "ENERGY STAR score (1-100)", "Median Property*": "50"}]
You forgotten create data and header for other json keys (like Design
Project, Design Target etc)
This is correct version:
import csv
import json
with open('test.txt', 'r') as csvfile: # Opens file
filereader = csv.reader(csvfile, delimiter=' ')
i = 0
header = []
out_data = []
for row in filereader:
row = [elem for elem in row if elem]
if i == 0:
i += 2
row[1:3] = [row[1]+" "+row[2]] # Design Project key
row[2:4] = [row[2]+" "+row[3]] # Design Target key
row[3:5] = [row[3]+" "+row[4]] # Median Property*
header = row
else:
row[0:4] = [row[0]+" "+row[1]+" "+row[2]+" "+row[3]] # Metric value
if len(row) == 5: # check conditions for better parse
row[1:3] = [row[1]+" "+row[2]] # Design Project value
_dict = {}
for elem, header_elem in zip(row, header):
_dict[header_elem] = elem
out_data.append(_dict)
print json.dumps(out_data)
It work only if structure of your data is constant, and key/value consists of the same number of words.
You can add additional conditions (like me in line 21):
if len(row) == 5: # check conditions for better parse
row[1:3] = [row[1]+" "+row[2]] # Design Project value
I have a csv file with, say, 50 rows of data, and I would like to split it into separate csv files for each row, which includes first row (header) and the the relevant row.
E.g.
file 1 contains: row1, row2,
file 2 contains: row1, row3,
file 3 contains: row1, row4
And so on.
Currently working with:
import csv
counter = 1
with open('mock_data.csv', 'r', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
filename = "file_%s" % str(counter)
with open(filename, 'w') as csvfile_out:
writer = csv.writer(csvfile_out)
writer.writerow(row)
counter = counter + 1
I'm currently getting 'csvfile_out' not defined.
a) Am I even approaching this correctly
b) Any ideas why csvfile_out isn't being defined?
I have tried this and it works fine for your purpose. Unfortunately, I didn't get any csvfile_out error and your with statement works correctly in my Python 2.7.12 console.
import csv
counter = 1
with open('mock_data.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
row1 = next(reader) # here you save your first line of the .csv file
for row in reader:
if row: # if row is not empty, write a file with this row
filename = "file_%s" % str(counter)
with open(filename, 'w') as csvfile_out:
writer = csv.writer(csvfile_out)
writer.writerow(row1) #here you write your row1 as first row of csvfile_out
writer.writerow(row)
counter = counter + 1
Here is a solution with pandas. Assume the content of csv as follows:
Name, Age, Gender
John, 20, Male
Jack, 22, Male
Jill, 18, Female
And my code is as follows:
import pandas as pd
df = pd.read_csv("mock_data.csv")
for index, row in df.iterrows():
file_name = row['Name']+".csv" #Change the column name accordingly
pd.DataFrame(row).T.to_csv(file_name, index=None)
This will create filenames based on the values of the column "Name" (i.e. Jack, John and Jill) to produce three files John.csv, Jack.csv and Jill.csv. Content of John.csv is as follows:
Name | Age | Gender |
---------------------------
John | 20 | Male |
Content of Jack.csv is as follows:
Name | Age | Gender |
---------------------------
Jack | 22 | Male |
Content of Jill.csv is as follows:
Name | Age | Gender |
---------------------------
Jill | 20 | Female |
P.S: If you don't want the header, just add header = None when calling .to_csv() function. For example:
pd.DataFrame(row).T.to_csv(file_name, index=None, Header=None)
You can use DictReader too...
import csv
counter = 1
with open('mock_data.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
filename = "file_%s" % str(counter)
with open(filename, 'w') as csvfile_out:
writer = csv.DictWriter(csvfile_out, fieldnames=reader.fieldnames)
headers = dict((n, n) for n in reader.fieldnames)
writer.writerow(headers)
writer.writerow(row)
counter = counter + 1
I am using a DictWriter to write to a csv file to a single row, however; only a two columns are being populated. Here are some code snippets of what I am working on with the focus being on the function Stat2():
def scheduledPerformanceAvailability(FILENAME, CATEGORY):
# get category column for current entry
entry = retrieveEntries(FILENAME, CATEGORY)
# grab the most frequent entry from the collections.counter
mostFrequent = entry.most_common(1)[0][1]
print "\n", mostFrequent;
# calculate the total number of values in the file
totalNumber = sum(entry.values())
print "\n", totalNumber
# caculate the percentage
percentage = float( mostFrequent / totalNumber ) * 100
print "\n", percentage, "%\n";
return percentage
def Stat2(FILENAME2, itemValue, percentage):
# store the values into a list
entry = []
percent = str(percentage)
displayPercentage = percent + ' %'
entry.append({'DEPARTURES_SCHEDULED': displayPercentage, 'UNIQUE_CARRIER_NAME':itemValue})
fieldnames = ['DEPARTURES_SCHEDULED','DEPARTURES_PERFORMED','SEATS', 'UNIQUE_CARRIER_NAME']
# open a file for writing
outfile = open(FILENAME2, 'a')
# create the csv writer object
csvwriter = csv.DictWriter(outfile, delimiter=',', fieldnames=fieldnames)
# check the file before appending any unecessary headers
ckfile = open(FILENAME2, 'r').read()
if(ckfile == ''):
csvwriter.writerow(dict((fn,fn) for fn in fieldnames))
for row in entry:
csvwriter.writerow(row)
# close the file
outfile.close()
def most_commonCatgoryO_(FILENAME1, FILENAME2, CATEGORY1, CATEGORY2):
# create the field names for each category
fieldnames = ['DEPARTURES_SCHEDULED','DEPARTURES_PERFORMED','SEATS', CATEGORY1]
# open a file for writing
outfile = open(FILENAME1,'wb')
# create the csv writer object
csvwriter = csv.DictWriter(outfile, delimiter=',', fieldnames=fieldnames, extrasaction = 'ignore')
csvwriter.writerow(dict((fn,fn) for fn in fieldnames))
entry = retrieveEntries('input/NC_SC Proj Data_2012 {Intermediate-File}.csv', CATEGORY1)
# grab the item value associated with the most frequent number
itemValue = entry.most_common(1)[0][0]
# print "\n", itemValue;
# reopen the intermediate for reading
infile = open('input/NC_SC Proj Data_2012 {Intermediate-File}.csv', 'rb')
reader = csv.DictReader(infile)
# populate the outfile using the pre-defined condition
for row in reader:
if(row[CATEGORY1] == itemValue):
csvwriter.writerow(row)
outfile.close()
#open the outfile for reading
with open(FILENAME1, 'rb') as infile:
# calculate the percentage
percentage = scheduledPerformanceAvailability(FILENAME1, CATEGORY2)
_Stat2_(FILENAME2, itemValue, percentage)
infile.close()
For example, my csv file:
DEPARTURES_SCHEDULED,DEPARTURES_PERFORMED,SEATS,UNIQUE_CARRIER_NAME
3.90977443609 %,,,US Airways Inc.
4.21052631579 %,,,US Airways Inc.
1.8045112782 %,,,US Airways Inc.
My desired output should be:
DEPARTURES_SCHEDULED DEPARTURES_PERFORMED SEATS UNIQUE_CARRIER_NAME
3 % 4% 1% US Airways Inc.