跳过文本块添加到字典 vb.net [英] Skip block of text from adding to dictionary vb.net
问题描述
我想知道是否有一种方法可以读取具有如下内容的文本文件:
<前>商店 - 001P所有者 - 格雷格价格 - 45000员工 - 30商店 - 002业主-仁价格 - 34400现在假设我只想处理商店编号包含 P 的块中的商店信息.有没有办法读取文本文件以检查分隔符后的 P,然后跳到下一个商店"的实例在文本文件中?我试图找到哪个功能最好,因为在不同语言中您可以使用 GoTo 功能,但我找不到任何关于此的信息.
这是我当前代码的片段:
Dim columnV As New Dictionary(Of String, Integer)Dim descriptionV As String将数量变暗为整数对于 MyData 中的每个 totalLine 作为字符串descriptionV = Split(totalLine, -")(0)数量V = Split(totalLine, -")(1)如果 columnV.ContainsKey(descriptionV) 然后columnV(descriptionV) = colSums(descriptionV) + 数量V别的columnV.Add(descriptionV,quantityV)万一'下一个
好吧,如果我没看错的话,您只需要担心和处理以P"结尾的存储.我做对了吗?
然后你的数据是这样的:
Store - 001P \n Owner - Greg \n Price - 45000 \n 员工 - 30 \n商店 - 002\n 所有者 - Jen \n 价格 - 34400 \n
所以在代码中我们可以去:
dim 存储为字符串将所有者作为字符串变暗以十进制表示的价格将员工调暗为整数' 读取数据的代码现在我们有了流程循环对于 MyData 中的每个 totalLine 作为字符串store = trim(split(split(totalLine,"\n)(0),"-")(1))Owner = trim(split(split(totalLine,"\n)(1),"-")(1))价格 = trim(split(split(totalLine,"\n")(3),"-")(1))员工 = trim(split(split(totalLine,"\n")(4),"-")(1))if strings.right(store,1) = "P";然后'我们的流程代码如果 columnV.ContainsKey(store) 然后万一下一个
所以你可以使用 Strings.Right(some text,1) 来获得最合适的字符.然后,您只需在该代码周围设置一个 if/then 块,从而跳过任何没有带有P"的存储区的内容.作为最后一个字母.以上是空码,但这里的简单概念是先:将线分割成正确的部分.
检查商店是否以P"结尾使用上面的 if/then,因此只有以 P 结尾的存储才会被处理.
但是,您的示例代码和使用的变量名称确实没有多大意义.
I want to know if there is a way to read a text file that lets say has content like so:
Store - 001P Owner - Greg Price - 45000 Employees - 30 Store - 002 Owner- Jen Price - 34400
Now lets say I only want to work with the store information in the block where the store number contains a P. Is there a way to read the text file to check for the P after the delimiter, and then skip to the next instance of "store" in the text file? I tried to find which function is best as in different languages you can GoTo function, but I cannot find anything on this.
This is a snippet of my current code:
Dim columnV As New Dictionary(Of String, Integer)
Dim descriptionV As String
Dim quantityV As Integer
For Each totalLine As String In MyData
descriptionV = Split(totalLine, "-")(0)
quantityV = Split(totalLine, "-")(1)
If columnV.ContainsKey(descriptionV) Then
columnV(descriptionV) = colSums(descriptionV) + quantityV
Else
columnV.Add(descriptionV, quantityV)
End If
'Next
Ok, if I read this correct, you only want to worry about and process stores that end in a "P". Did I get that right?
and your data is like this then:
Store - 001P \n Owner - Greg \n Price - 45000 \n Employees - 30 \n
Store - 002\n Owner- Jen \n Price - 34400 \n
So in code we could go:
dim store as string
dim Owner as string
dim Price as decimal
dim employees as integer
' your code to read the data
now we have the process loop
For Each totalLine As String In MyData
store = trim(split(split(totalLine,"\n)(0),"-")(1))
Owner = trim(split(split(totalLine,"\n)(1),"-")(1))
Price = trim(split(split(totalLine,"\n")(3),"-")(1))
Employees = trim(split(split(totalLine,"\n")(4),"-")(1))
if strings.right(store,1) = "P" then
' our process code
If columnV.ContainsKey(store) Then
end if
Next
So you can use Strings.Right(some text,1) to get the right most character. You simply then have a if/then block around that code, and thus you skip anything that does not have a store with "P" as the last letter. The above is air code, but the simple concept here is to first: Split out the line into the correct parts.
Check if store ends in "P" with the above if/then, and thus only stores ending in P will be processed.
However, your sample code and the variable names used really does not make a lot of sense.
这篇关于跳过文本块添加到字典 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!