I am trying to do calculations onto a file that I created in python.
But I can't seem to make a string with the data without loosing my quotations or going word by word...
i have attached my code...


This then produces...


s
e
'

But I want to work with the 7500, 75, and 100 and it is just giving me the letters?!
Also I tried I have it doing the out = out.translate(string...ect) but in some of the lines there is extra quotations between the 1 and the 100 which I need so they keep a blank cell when opening it in excel.

Thanks for any help!!

import string

Cornfile = open('E:\WORK_09\NASS_DATA\Corn\Output\Cornoutput.txt', 'r')
Corncaloutput = open('E:\WORK_09\NASS_DATA\Corn\Output\Corncal.txt', 'w')

Header = ''
Header = 'Commodity,Practice,Year,State,County,StFips,District,CoFips,CommCode,PracCode,Planted All Purposes,Harvested,Yield,Production'
Header = str(Header)
Corncaloutput.write(Header)

for line in Cornfile:
    skip1st = line
    for line in Cornfile:
        out = str(line)
        out = out.translate(string.maketrans("", ""),string.punctuation)
        out = out.split()
        out = str(out)
        acres = out[-6]
        bushel1 = out[-4]
        bushel2 = out[-2]

To convert from a string to int or float cast it using int() or float() like so:

>>> s = '7500'
>>> int(s)
7500
>>> int(s) / 10
750
>>> float(s) / 5
1500.0
>>>

Here is the whole code...

import string

Cornfile = open('E:\WORK_09\NASS_DATA\Corn\Output\Cornoutput.txt', 'r')
Corncaloutput = open('E:\WORK_09\NASS_DATA\Corn\Output\Corncal.txt', 'w')

Header = ''
Header = 'Commodity,Practice,Year,State,County,StFips,District,CoFips,CommCode,PracCode,Planted All Purposes,Harvested,Yield,Production'
Header = str(Header)
Corncaloutput.write(Header)

for line in Cornfile:
    skip1st = line
    for line in Cornfile:
        out = str(line)
        out = out.translate(string.maketrans("", ""),string.punctuation)
        out = out.split()
        out = str(out)
        acres = out[-6]
        bushel1 = out[-4]
        bushel2 = out[-2]
        print out
        print acres
        print bushel1
        print bushel2
        acres = float(acres)
        bushel1 = float(bushel1)
        bushel2 = float(bushel2)
        acres = acres * 0.405
        bushel1 = bushel1 / 39.37
        bushel2 = bushel2 / 39.37
        del out[-3]
        out.insert(-2 ,acres)
        del out[-2]
        out.insert(-1, bushel1)
        del out[-1]
        out.append(bushel2)
        del out[0:3]
        out.insert(0, 'Corn for Grain')
        if len(out[5]) > 2:
            word = out[4:6]
            del out[4:6]
            word = str(word)
            word = word.translate(string.maketrans("",""),string.punctuation)
            out.insert(4,word)
        out = str(out)
        out = out[1:-1]
        Corncaloutput.writelines('\n' +out)

So I do convert it...but it is not taking 7500 it is only taking each individual letter.

What is "not taking 7500" ? When you do your print checks... do they print 7500 or 7, then 5, then 0, then 0 ?

Also, wtf are the translate calls there for? What are you trying to do?

For what you have in mind you need to use a list, not a string ...

mylist = ['Corn', 'For', 'Grain', 'Irrigated', '1970', 'Colorado', 'Chaffee', 
'8', '10', '15', '11199199', '1', '100', 'acres', '75', 'bushel', '7500', 'bushel']

acres = int(mylist[-6])
bushel1 = int(mylist[-4])
bushel2 = int(mylist[-2])

print acres, bushel1, bushel2  # --> 100 75 7500

Also string.maketrans("", "") makes a translation table going from empty to empty and doesn't do anything with translate().

I was using the translate to try and get rid of the punctuation. But when I take the translate out and try to do calculates on the 7500 and 75...they come out as '75 and '7500.
It will not accept '75 as an integer and I already tried stripping it

So you were using translate like you would use replace...

Here look:

>>> w = "'7500"
>>> int(w)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: "'7500"
>>> w.replace("'", "")
'7500'
>>> int(w.replace("'", ""))
7500
>>> # OR
>>> int(w.strip("'"))
7500
>>>

You could really use either strip or replace here... but yeah translate is definitely not what you're looking for.

This question has already been answered. Start a new discussion instead.