Advertisement
Guest User

csv_

a guest
May 29th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.29 KB | None | 0 0
  1. 
  2. Public Class Form1
  3.  
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.         If InputFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
  6.             inputFile.Text = InputFileDialog.FileName
  7.             otputFileName.Text = IO.Path.GetFileNameWithoutExtension(inputFile.Text) & "_no_dublicates"
  8.             outputSaveDir.Text = IO.Path.GetDirectoryName(inputFile.Text)
  9.         End If
  10.     End Sub
  11.  
  12.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  13.         If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
  14.             outputSaveDir.Text = FolderBrowserDialog1.SelectedPath
  15.         End If
  16.     End Sub
  17.  
  18.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  19.         Using csvReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(inputFile.Text, System.Text.Encoding.GetEncoding(1251))
  20.             csvReader.TextFieldType = FileIO.FieldType.Delimited
  21.             csvReader.SetDelimiters(";")
  22.             csvReader.TrimWhiteSpace = True
  23.  
  24.             Dim currentRow As String()
  25.             Dim brands As Dictionary(Of String, List(Of Integer)) = New Dictionary(Of String, List(Of Integer))
  26.             Dim articles As Dictionary(Of String, List(Of Integer)) = New Dictionary(Of String, List(Of Integer))
  27.         ' Словник з елмементами які мають однаковий артикул і виробника
  28.         ' Структура:
  29.         ' Ідентифікатор який складається з виробника і артикула, номер рядка в файлі, ціна
  30.         ' id -> {
  31.         '       25687 -> 1325.2,
  32.         '       35978 -> 1525.0
  33.         '}
  34.             Dim duplicates As Dictionary(Of String, Dictionary(Of Integer, Double)) = New Dictionary(Of String, Dictionary(Of Integer, Double))
  35.  
  36.             Dim brand As String = ""
  37.             Dim article As String
  38.             While Not csvReader.EndOfData
  39.                 Try
  40.                     currentRow = csvReader.ReadFields()
  41.            
  42.                     brand = currentRow(2)
  43.                     article = currentRow(1)
  44.  
  45.                     ' For some reason real data starts from 3rd row. 2nd line is header
  46.             ' Рядок №2 - заголовок
  47.                     If csvReader.LineNumber < 3 Then
  48.                         Continue While
  49.                     End If
  50.  
  51.  
  52.                     Dim brands_line_numbers As New List(Of Integer)
  53.                     If brands.TryGetValue(brand, brands_line_numbers) Then
  54.                         brands_line_numbers.Add(csvReader.LineNumber)
  55.                     Else
  56.                         Dim nac As New List(Of Integer)
  57.                         nac.Add(csvReader.LineNumber)
  58.                         brands.Add(brand, nac)
  59.                     End If
  60.  
  61.                     Dim articles_line_numbers As New List(Of Integer)
  62.                     If articles.TryGetValue(article, articles_line_numbers) Then
  63.                         articles_line_numbers.Add(csvReader.LineNumber)
  64.                     Else
  65.                         Dim aac As New List(Of Integer)
  66.                         aac.Add(csvReader.LineNumber)
  67.                         articles.Add(article, aac)
  68.                     End If
  69.                 Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
  70.                     MsgBox("Line " & ex.Message & " is invalid.  Skipping")
  71.                 End Try
  72.             End While
  73.        
  74.             Dim pair As KeyValuePair(Of String, List(Of Integer))
  75.             Dim pair2 As KeyValuePair(Of String, List(Of Integer))
  76.  
  77.             ' Виробники
  78.         For Each pair In brands
  79.         ' Артикули
  80.                 For Each pair2 In articles
  81.                     'Dim iintersection = pair.Value.Intersect(pair2.Value)
  82.             ' Перетин множин артикули і виробники
  83.                     Dim intersection = Enumerable.ToList(pair.Value.Intersect(pair2.Value))
  84.             ' Якщо перетин містить лише один елемент, значить це номер рядка в якому розташована поточна зв'язка виробник + артикул
  85.                     If intersection.Count > 1 Then
  86.                         Dim output As New System.Text.StringBuilder
  87.            
  88.                         For Each number As Integer In intersection
  89.                             Console.WriteLine("bebe {0}", number)
  90.                             output.AppendLine(number)
  91.                             Dim tmpDict As Dictionary(Of Integer, Double) = New Dictionary(Of Integer, Double)
  92.                 ' ціни
  93.                             tmpDict.Item(number) = 0.0
  94.                 ' ідентифікатор який складається з артикула і назви виробника
  95.                             duplicates.Item(pair.Key & pair2.Key) = tmpDict
  96.                         Next
  97.            
  98.                         Console.WriteLine("******")
  99.                         Console.WriteLine("Intersaction is")
  100.                         Console.Write(output)
  101.                         Console.WriteLine("******")
  102.                     End If
  103.  
  104.                 Next
  105.             Next
  106.         End Using
  107.     End Sub
  108. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement