0
我有一個關於使用Python在for循環中從文本文件打印數據的問題。每當我嘗試運行這個程序,它說「無法將字符串轉換爲浮點數:KAAA」(KAAA是元素[2]中的字符串元素)。我正在嘗試使用製表符分隔符將數據從帶有逗號分隔符的文本文件傳輸到文本文件。由於某種原因,這個程序工作正常,如果我拿走了字符串的第三列,但不是當我把它重新加入。有沒有一種方法,我可以得到它,所以三列轉移到一個文本文件以逗號分隔他們?這是來自數據的示例行。 「40.15869904 -89.33499908 KAAA」。製表符分隔符到逗號分隔符傳輸問題
cj = CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor)
opener.addheaders = [('User-agent','mr_anderson')]
keywords = map(''.join, product(ascii_lowercase, repeat=3))
keywords = ["k"+a+b+c for a,b,c in product(ascii_lowercase, repeat=3)]
start_time = time.time()
print("--- %s seconds ---" % (time.time() - start_time))
try:
a = 1
b = 1
List=[""]
data = np.loadtxt("airportcodelatlonidentonlyk.txt")
text_file_latlonair = open("latlontext.txt","a")
for element in data:
lat = str(element[0])
lon = str(element[1])
ident = str(element[2])
text_file_latlonair.write(str(lat)+','+str(lon)+','+str(ident))
text_file = open("nws_contourcurrenttemp_datatwo.txt","a")
for i in range (1,50):
i=1
i+=1
a+=1
b+=1
keywargs = str(keywords[a]).upper()
argument = 'http://w1.weather.gov/xml/current_obs/'+keywargs+'.rss'
page = 'http://w1.weather.gov/xml/current_obs/KBED.rss'
sourceCode = opener.open(page).read()
#print(sourceCode)
req = Request(argument)
try:
page_open = urlopen(req)
except:
None
else:
c=1
c+=1
sourceCode = opener.open(argument).read()
KBEDforecastraw = re.findall(r'and\s\d{1,2}\s\w.*?',str(sourceCode))
windraw = re.findall(r'at\s\d{1,2}\.\d{0,1}.*?',str(sourceCode))
pressureraw = re.findall(r'The pressure is\s\d{1,4}\.\d{0,1}\s\w\w.*?',str(sourceCode))
humidraw = re.findall(r'the humidity is\s\d{1,2}\%.*?',str(sourceCode))
try:
KBEDtemp = KBEDforecastraw[0]
KBED = str(KBEDtemp).strip("[and F]")
#text_file.write(KBEDtemp)
except:
None
else:
text_file.write(KBED+'\n')
#print(KBED)
#print(str(KBEDforecastraw)+' '+keywargs+str(windraw)+str(pressureraw)+str(humidraw))
except Exception, e:
print(str(e))
print("--- %s seconds ---" % (time.time() - start_time))