I'm new to python and programming, so I'm sure my code is the opposite of 'pythonic'.
My goal is to grab an excel file from a server, open it, write the data in the file to a new file. Next I open that file and use dictReader to print a few columns from the file. First I wrote a script to just open an excel file and print the columns. I was able to achieve this. Now I'm adding the next step of grabbing the file, writing it to a local file and then opening it.
I'm receiving the following error which I've researched a fair amount prior to posting:
File "request.py", line 20, in <module>
if line['Change'] == ticket_ID:
KeyError: 'Change'
Below is my code:
import csv
import sys
import urllib2
data = urllib2.urlopen("URL-HERE")
new_file = open("c:\\file-here", "w")
for fields in data:
new_file.write(fields)
new_file.close()
test_file = 'getrequest6.csv'
csv_file = csv.DictReader(open(test_file, 'r'))
# ticket_ID = sys.argv[1]
ticket_ID = "RMSDB00010243"
for line in csv_file:
if line['Change'] == ticket_ID:
change_col = line['Change']
review_col = line['Review']
phase_col = line['Phase']
print change_col, review_col, phase_col
What I've found is that the file I manually download from the server is of ANSI codec. This file works when I open it. The new file I create and write (getrequest6) is a UTF-8 codec. I'm assuming that is the reason for the KeyError because when I copy and paste the data from the getrequest6 file, to the ANSI file, my code above works.
Any help would be appreciated. I searched on the codecs class but I do not understand it.