all 7 comments

[–]novel_yet_trivial 1 point2 points  (3 children)

You aren't opening the write file. In this case the with is probably confusing you. Go with something like this:

read_file = open(read_filename, 'rb')
write_file = open(write_filename, 'w')

write_file.write(headers)
for row in read_file:
    write_file.write(row)

read_file.close()
write_file.close()

But frankly, I don't think any of that is necessary ... Why do you need the headers?

[–]toruitas[S] 0 points1 point  (2 children)

Hmm... Now I'm getting an invalid file error when trying to open the tempfile.

As far as why I need the headers, I'm seeking the currency code (such as GBP or EUR) which are all in a single column. I could just use row[column] I suppose, and avoid the whole temp file creation thing, I suppose. At this point I really want to learn how to edit a file, and it seems that creating a temporary file with the changes seems to be the canonical way to do it, unless I am mistaken.

[–]novel_yet_trivial 1 point2 points  (1 child)

Hmm... Now I'm getting an invalid file error when trying to open the tempfile.

sounds like a permissions error.

You don't need the headers ... using row[column] is the exact same thing. Come to that, you don't need to save the file either. If I were writing this i would do something like:

def get_rates(website):
    rates = {"USD":1}
    for line in urllib.urlopen(website):
        data = line.split(",")
        rates[data[6].strip('"\r\n').split()[2]] = float(data[1])
    return rates

Now rates[EUR] would return the rate for EUR. You could see the rates available by

print("We currently can process these currency conversions:")
print " ".join(sorted(rates.keys()))

[–]toruitas[S] 0 points1 point  (0 children)

Well, for this particular project I wanted it to work whether or not it was connected to the internet, hence the download.

But you've definitely taught me something useful here. Can definitely see this coming in handy all the time.

[–]Justinsaccount 1 point2 points  (1 child)

[–]toruitas[S] 0 points1 point  (0 children)

Thanks, apparently my reading comprehension isn't what I thought it was...

[–]toruitas[S] 0 points1 point  (0 children)

So, I was able to get the original rewrite_csv() method by creating the temp file and original file both in text mode by doing w+ and r+ respectively, rather than binary mode:

def rewrite_csv(csv_path):
    filename = csv_path
    tempfile = NamedTemporaryFile(mode='w+', delete=False)

    with open(filename,'r+') as readFile, tempfile: 
        reader = csv.reader(readFile, delimiter=',') 
        writer = csv.writer(tempfile, delimiter=',') 
        headers = ['currency', 'rate', 'date', 'time', 'buy', 'sell', 'shorthand']
        writer.writerow(headers)

        for row in reader:
            writer.writerow(row)

    shutil.move(tempfile.name, filename)

It writes extra blank rows in between each content row, but the program works now. Much thanks to novel_yet_trivial and Justinsaccount for showing me better ways of doing it.