I am taking a text file and trying to do calculations with it where the line in the file looks like this:
'Corn For Grain', 'Irrigated', '1970', 'Colorado', 'Chaffee', '8', '10', '15', '11199199', '1', '', '100 acres', '75 bushel', '7500 bushel', '', ''

It has commas because it came from another text file. I want to do calculations to the 100 acres, 75 bushel and 7500 bushel
So in my program I am splitting it but then it splits every word, which causes problems in the end when I am writing it to another file and then trying to open it to excel.
I would like to split it by the commas so I can keep 'Corn for Grain' together rather than 'Corn','For','Grain'
here is my program:

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:
    skipline = line
    for line in Cornfile:
        out = line
        #out = str(line)
        #out = out.replace("'","")
        #out = out.replace(",","")
        out = out.split()
        print out
        acres = out[-6]
        bushel1 = out[-4]
        bushel2 = out[-2]
        print acres
        print bushel1
        print bushel2
        acres = float(acres.replace("'", ""))
        bushel1 = float(bushel1.replace("'", ""))
        bushel2 = float(bushel2.replace("'", ""))
        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)
        length = len(out)
        if length == 20:
            del out[0:3]
            out.insert(0, 'Corn for Grain')
            word = out[4:6]
            del out[4:6]
            word = str(word)
            word = word.replace("'", "")
            out.insert(4,word)
        out = str(out)
        out = out.replace("'","")
        out = out[1:-1]
        Corncaloutput.writelines('\n' +out)

Where it says out = out.split()...I tried putting out.split(line, ' ,') to get it to split at the commas but it told me that an integer is required.

Thanks for the help!

Now I got it to work but when writing it into the file I want to keep the commas but take away the "" around each character in the string....how can I do this?

My code now looks like this:

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:
    skipline = line
    for line in Cornfile:
        out = line
        out = string.split(out,',')
        print out
        acres = out[-5]
        acres = string.split(acres,' ')
        acres = acres[-2]
        bushel1 = out[-4]
        bushel1 = string.split(bushel1, ' ')
        bushel1 = bushel1[-2]
        bushel2 = out[-3]
        bushel2 = string.split(bushel2, ' ')
        bushel2 = bushel2[-2]
        print acres
        print bushel1
        print bushel2
        acres = float(acres.replace("'", ""))
        bushel1 = float(bushel1.replace("'", ""))
        bushel2 = float(bushel2.replace("'", ""))
        acres = acres * 0.405
        bushel1 = bushel1 / 39.37
        bushel2 = bushel2 / 39.37
        del out[-5]
        out.insert(-4 ,acres)
        del out[-4]
        out.insert(-3, bushel1)
        del out[-3]
        out.insert(-2, bushel2)
        out = str(out)
        out = out[1:-5]
        out = out.replace("'", "")
        Corncaloutput.writelines('\n' +out)

To split up the line from your first post here's what I'd do (this will also strip out the quotes from the beginning and end of each entry):

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

data_split = [entry.strip(" '") for entry in data.split(',')]

EDIT:
Looking at your code, why do you have a nested for loop? It seems to have no purpose, and may actually cause you problems.

This article has been dead for over six months. Start a new discussion instead.