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!