2014-11-06 44 views
0

我已經寫了一個程序來從電子表格中提取一些數據,但是我想讓它在單元格中存在時忽略括號和單詞。電子表格包含城市名稱,縣名和稅率。如何解析不包含單詞和/或括號的數據?

對於大多數情況下的城市來說,單元格只包含城市名稱,但有時也會在裏面帶有「城市」一詞的括號。

Benton (city) 

我正在尋找一種解決方案,使我的程序只檢索沒有括號的城市。

Benton 

對於縣裏的每一條記錄都包含縣名和縣名。

Crawford County 

我想只檢索縣的名稱。

Crawford 

對於縣我知道我可以使用拆分方法,但我想建議爲處理這兩種情況的最佳方式。腳本的其餘部分工作正常。我只需要改進數據。

對於任何想看我如何處理數據的人。下面是代碼:

public static List<CityTax> getCityTaxRates(string fileName) 
     { 
      Excel.Application xlApp = new Excel.Application(); 
      Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@fileName); 
      Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1]; 
      Excel.Range xlRange = xlWorksheet.UsedRange; 
      List<CityTax> cityTaxList = new List<CityTax>(); 
      Console.WriteLine("City Tax Rates----------------------------------------------------------------"); 
      for (int i = 10; i <= 312; i++) 
      { 
       CityTax cityTaxRate = new CityTax(); 
       cityTaxRate.city = xlRange.Cells[i, 2].Value2.ToString(); 
       cityTaxRate.cityRate = Convert.ToDecimal(xlRange.Cells[i, 5].Value2); 
       cityTaxList.Add(cityTaxRate); 
      } 
      return cityTaxList; 
     } 

    public static List<CountyTax> getCountyTaxRates(string fileName) 
    { 
     Excel.Application xlApp = new Excel.Application(); 
     Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@fileName); 
     Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1]; 
     Excel.Range xlRange = xlWorksheet.UsedRange; 
     List<CountyTax> countyTaxList = new List<CountyTax>(); 
     Console.WriteLine("County Tax Rates----------------------------------------------------------------"); 
     for (int i = 316; i <= 390; i++) 
     { 
      CountyTax countyTaxRate = new CountyTax(); 
      countyTaxRate.county = xlRange.Cells[i, 2].Value2.ToString(); 
      countyTaxRate.countyRate = Convert.ToDecimal(xlRange.Cells[i, 5].Value2); 
      countyTaxList.Add(countyTaxRate); 
     } 
     return countyTaxList; 
    } 

    public static void testTaxRates(string fileName) 
    { 
     List<CityTax> cityTaxList = new List<CityTax>(); 
     cityTaxList = ParseTaxRates.getCityTaxRates(fileName); 
     foreach (CityTax cityTax in cityTaxList) 
     { 
      Console.WriteLine("City: " + cityTax.city); 
      Console.WriteLine("Rate: " + cityTax.cityRate); 
     } 

     List<CountyTax> countyTaxList = new List<CountyTax>(); 
     countyTaxList = ParseTaxRates.getCountyTaxRates(fileName); 
     foreach (CountyTax countyTax in countyTaxList) 
     { 
      Console.WriteLine("City: " + countyTax.county); 
      Console.WriteLine("Rate: " + countyTax.countyRate); 
     } 
    } 

如果你想看到我檢索數據從電子表格可以從以下網站下載: http://www.arkansas.gov/dfa/excise_tax_v2/st_zip.html

沒有理由在我的環路的靜態指標是由於電子表格的佈局。我願意接受如何處理這個問題的建議。現在我知道我必須在運行之前始終檢查電子表格,以確保行號仍然相同。不必擔心這一點真是太好了,但我不知道如何以其他方式處理它。

+0

你有一個像'等等等等city'什麼? – DavidG 2014-11-06 16:43:47

+0

如果你的意思是一個名字中有一個空格的城市名稱,那麼是「Ash Flat」。然後其中一些像「布拉德利(城市)」和「富蘭克林(城市)」。儘管沒有引號。 – billabrian6 2014-11-06 16:47:24

+0

爲什麼不從字符串的末尾刪除「(城市)」和「縣」? – Dialecticus 2014-11-06 17:09:11

回答

2

我不是一個正則表達式的專家以任何手段,但:

這將匹配文本前括號任何東西:

(^.*) \(.*\) 

,這將匹配文本排除county後綴

(^.*) county 

或合併:

(^.*) (\(.*\)|county) 

例如,如果您不確定文本將是County還是county,則應該對此不區分大小寫。

一些示例C#代碼:

var input = "blah blah County"; 
var regex = @"(^.*) (\(.*\)|county)"; 

var matches = Regex.Matches(input, regex, RegexOptions.IgnoreCase); 

if(matches.Count > 0) 
{ 
    var place = matches[0].Groups[1].Value; 
} 
+0

這可能是由於無知,但它不適合我。你能建議我如何實現這個?我創建了一個名爲pattern的字符串變量,並將該模式​​變量添加到由逗號分隔的WriteLine中。我認爲我做錯了。 – billabrian6 2014-11-06 16:59:12

+0

添加了一些(非常糟糕的)代碼 – DavidG 2014-11-06 17:03:07

+0

到目前爲止,我只測試過城市,它適用於大多數人,但我注意到一些奇怪的東西。原始數據有一個洞穴城市,只改變洞穴。我覺得這很奇怪,因爲正則表達式不檢查「城市」。它不可能是空間,因爲還有其他城市的空間運轉良好。 Cave City唯一不同的地方是字體粗體顯示... – billabrian6 2014-11-06 17:15:31

相關問題