2016-11-17 44 views
-1

我與它帶給我的X,Y座標和情節號碼map.Ex代碼工作:地塊編號20,21,22等,但如果地圖有字母數字值像20A,20A或20 A,我被卡住了,因爲當我輸入像20-A這樣的值時,我得到這個錯誤「ValueError:無效文字爲int()與基10」。所以請幫助我如何處理字母數字值。ValueError:以10爲基數的int()無效文字。如何修復字母數字值?

這是我的代碼。

import matplotlib.pyplot as plt 
from PIL import Image 
import numpy as np 
import Tkinter as tk 
import tkSimpleDialog 

#Add path of map file here. The output will be saved in the same path with name map_file_name.extension.csv 
path = "maps/21.jpg" 


#Set this to true for verbose output in the console 
debug = False 


#Window for pop ups 
root = tk.Tk() 
root.withdraw() 
root.lift() 
root.attributes('-topmost',True) 
root.after_idle(root.attributes,'-topmost',False) 


#Global and state variables 
global_state = 1 
xcord_1, ycord_1, xcord_2, ycord_2 = -1,-1,-1,-1 
edge1, edge2 = -1,-1 

#Defining Plot 
img = Image.open(path) 
img = img.convert('RGB') 
img = np.array(img) 

if(debug): 
    print "Image Loaded; dimensions = " 
    print img.shape 

#This let us bind the mouse function the plot 
ax = plt.gca() 
ax.axes.get_xaxis().set_visible(False) 
ax.axes.get_yaxis().set_visible(False) 
fig = plt.gcf() 
#Selecting tight layout 
fig.tight_layout() 
#Plotting Image 
imgplot = ax.imshow(img) 

#Event listener + State changer 
def onclick(event): 
    global xcord_1,xcord_2,ycord_1,ycord_2,edge1,edge2, imgplot,fig, ax, path 
    if(debug): 
     print "Single Click Detected" 
     print "State = " + str(global_state) 
    if event.dblclick: 
     if(debug): 
      print "Double Click Detection" 
     global global_state 
     if(global_state==0): 


      xcord_1 = event.xdata 
      ycord_1 = event.ydata 

      edge1 = (tkSimpleDialog.askstring("2nd", "No of 2nd Selected Plot")) 
      #Draw here 
      if edge1 is None: #Incase user cancels the pop up. Go to initial state 
       global_state = 1 
       pass 
      else: 
       edge1 = int(edge1) 
       global_state = 1 
       difference = edge2-edge1 
       dif_state = 1; 
       #So difference is always positive. Dif_state keeps track of plot at which side has the larger number 
       if difference <0: 
        dif_state = -1; 
        difference *= -1 
       #Corner Case; labelling a single plot 
       if(difference == 0): 
        import csv 
        fields = [int(xcord_1),int(ycord_1),edge1] 
        plt.scatter(int(xcord_1),int(ycord_1),marker='$' + str(edge1) + '$', s=150) 
        with open(path+'.csv', 'a') as f: 
         writer = csv.writer(f) 
         writer.writerow(fields) 
       else: 
        if(debug): 
         print "P1 : (" + str(xcord_1) + ", " + str(ycord_1) + ")" 
         print "P2 : (" + str(xcord_2) + ", " + str(ycord_2) + ")" 
        for a in range(0,difference+1): 
         #Plotting the labels 
         plt.scatter(int(xcord_1+(a*(float(xcord_2-xcord_1)/difference))),int(ycord_1+a*((float(ycord_2-ycord_1)/difference))),marker='$'+str(edge1+dif_state*a)+'$',s=150) 
         #Saving in CSV 
         import csv 
         fields = [int(xcord_1+(a*(float(xcord_2-xcord_1)/difference))),int(ycord_1+a*((float(ycord_2-ycord_1)/difference))),str(edge1+dif_state*a)] 
         with open(path+'.csv', 'a') as f: 
          writer = csv.writer(f) 
          writer.writerow(fields) 

         if debug: 
          print (int(xcord_1+(a*(float(xcord_2-xcord_1)/difference))),int(ycord_1+a*((float(ycord_2-ycord_1)/difference)))) 
       plt.show() 



     elif(global_state == 1): 
      xcord_2 = event.xdata 
      ycord_2 = event.ydata 
      print "Recorded" 
      edge2 = (tkSimpleDialog.askstring("1st", "No of Selected Plot")) 
      print type(edge2) 
      if edge2 is None: 
       root.withdraw() 
       pass 
      else: 
       edge2 = int(edge2) 
       global_state = 0 



cid = fig.canvas.mpl_connect('button_press_event', onclick) 
plt.show() 
+0

你期望發生,如果值是「20-A」?你想將其轉換爲整數「20」嗎? –

+0

沒有我想輸入的精確值,即「20-A」,但是當i型20-A然後我得到錯誤。現在,我可以只輸入整數,我想輸入整數和字母數字值 – Zeeshan

+0

如果你輸入「20-A2」?你期望得到什麼號碼? 20? 202?一個20,2的列表? –

回答

0

可以使用split功能20-A分成20和A.

例如,"20-A".split('-')將返回['20','A']。然後,您可以調用這個數組的第一個元素上int方法

+0

thanku先生u能告訴我在哪裏,我是新來這個或進行更改,讓我做的工作代碼更改代碼才能使用此功能。 – Zeeshan

0

通用的方法將使用regex從文本中提取數字。

例如:

import re 

def get_number_from_string(my_str): 
    return re.findall('\d+', my_str) 

這將提取所有字符串中的數字並返回作爲list

如果你只需要一個值,在指數0提取數量。樣品運行:

>>> get_number_from_string('20-A') 
['20'] 
>>> get_number_from_string('20 A') 
['20'] 
>>> get_number_from_string('20A') 
['20'] 

因此,你的代碼數量字符串轉換爲int應該是這樣的:

number_string = get_number_from_string('20A')[0] # For getting only 1st number from list 
number = int(number_string) # Type-cast it to int  
+0

非常感謝你的回答,你可以修改代碼併發布。 – Zeeshan

+0

我還沒有檢查完整的代碼。我回答你的問題:*「但是如果地圖有字母數字值像20A,20A或20A,我堅持,因爲當我輸入像20A值」 *這是你的代碼,你知道在哪裏把這個:) –

相關問題