我如何重新安排数据 [英] how I can Re Arrange Data

查看:75
本文介绍了我如何重新安排数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含格式数据的文本文件



100; 10:24

100; 11:34
200; 9:40

200; 12:45



如何在阅读后将数据转换为格式



100 10:24 11:34

200 9:40 12:45





因此我希望将所有行开头与单行100中的100组合成一个键并以200行开头,因此第一列是键

I have a text file contains data in the format

100;10:24
100;11:34
200;9:40
200;12:45

how I can convert the data after reading to the format

100 10:24 11:34
200 9:40 12:45


as result I want to combine all line starts with 100 in single line 100 is a key and starts with 200 in line so the first column is a key

推荐答案

创建一个字典< int,string>()对象

打开文件

每行使用Split分割上 ;给出一个字符串数组

将数组中的第一项(100,200等)转换为int

检查int是否作为字典中的键存在(使用ContainsKey)

如果密钥不存在,添加值为数组中的第二项

如果密钥确实存在,则获取关联的字符串并附加一个空格,然后在数组中添加第二项



创建一个新文件

现在循环遍历字典中的每个关键项写出钥匙加上一个空格加上字符串值





为了抢先你的下一个问题,不,我不会给你的代码,它应该很简单,将上面的伪代码转换成实际代码。
Create a Dictionary<int, string >() object
Open the file
for each line use "Split" to split on ";" which gives an array of strings
Convert the first item in the array (the 100, 200 etc) to an int
Check if that int exists as a key in your dictionary (use ContainsKey)
if the key doesn't exist, add it with the value being the second item in the array
if the key does exist, get the associated string and append a space and then the second item in the array

Create a new file
Now loop through each key item in the dictionary and write out the key plus a space plus the string value


To pre-empt your next question, no I won't "give you the code", it should be simple enough to convert the pseudo code above into actual code.


你可以使用LINQ



you can use LINQ

Dim lines = File.ReadAllLines(filepath).Select(Function(record) record.Split(";"C)).Where(Function(cell) cell.Length >= 2).GroupBy(Function(x) x(0)).Select(Function(x) x.Key + " " + String.Join(" ", x.Select(Function(y) y(1))))

File.WriteAllLines(filepath, lines)





参考阅读:

https://msdn.microsoft.com/en-us/library /system.io.file.readalllines(v=vs.110).aspx [ ^ ]

https://msdn.microsoft .com / zh-cn / library / system.io.file.writealllines(v = vs.110).aspx [ ^ ]

https://msdn.microsoft.com/en-us/library/b873y76a(v = vs.110 ).aspx [ ^ ]

https ://msdn.microsoft.com/en-us/library/tabh47cf(v = vs.110).aspx [ ^ ]

https://msdn.microsoft.com/en-us/library/ 57a79xd0(v = vs.110).aspx [ ^ ]

https ://msdn.microsoft.com/en-us/library/bb531412.aspx [ ^ ]



reference to read:
https://msdn.microsoft.com/en-us/library/system.io.file.readalllines(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/system.io.file.writealllines(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/tabh47cf(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/bb531412.aspx[^]


学会自己动手:

1. 逐行阅读文本文件 [ ^ ]

2. VB .NET中的Split()和Join() [ ^ ]
Learn to do it yourself:
1. Reading a Text File Line by Line[^]
2. Split() and Join() in VB .NET[^]


这篇关于我如何重新安排数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆