我正在编写一个程序,一次打开一个文件,解析它,并将解析后的数据输出到一个新的 .txt 文档中,该文档根据传入的文件命名。将读取 50 多个文件并解析。
因此,如果打开文件的命名类似于:STACK-OVERFLOW-125663-D2.txt
,则输出文件将类似于以下内容125663-D2.txt
。
每次读取文件时,都会解析其部件号。每个文件将包含类似于此的行(第 8 个分隔的逗号值(即 119082、119083、119040、119085、119084)是零件编号值。):
"00003",6,"D","C20",-70.10,42.06,90.00,"119082",0,1,2,0,0,"",0,"001"
"00004",6,"D","C21",-67.91,42.06,90.00,"119082",0,1,2,0,0,"",0,"001"
"00005",13,"D","C23",-66.91,59.07,180.00,"119083",0,1,2,0,0,"",0,"002"
"00006",13,"D","R10",-77.32,66.88,90.00,"119040",0,1,2,0,0,"",0,"003"
"00007",13,"D","L3",-77.64,77.48,90.00,"119085",0,1,2,0,0,"",0,"004"
"00008",20,"D","D1",-62.91,103.77,0.00,"119084",0,1,2,0,0,"",0,"005"
"00009",21,"D","D1",-25.83,103.77,0.00,"119084",0,1,2,0,0,"",0,"005"
"00010",14,"D","L3",-40.56,77.48,90.00,"119085",0,1,2,0,0,"",0,"004"
"00011",14,"D","R10",-40.24,66.88,90.00,"119040",0,1,2,0,0,"",0,"003"
现在我需要做的是检查另一个 .txt 文件.. 假设它被称为“DATABASE.txt”,以查看这些零件编号是否已存在。该数据库文件将如下所示:
119082: 125663-D2, 123456-A1,
119083: 125663-D2,
119085: 125663-D2, 123456-A1, 987654321-Z11234, 1111111-B50
因此,在 DATABASE.txt 文件和上面打开的文件中,我想检查打开的文件中的所有零件号,看看它们是否存在于数据库中。
我不确定如何做到这一点,有人可以帮忙吗?
这是到目前为止我的一些代码:
List<string> partNumberLines = new List<string>();
string file = openFile.FileName;
string splitFile = file.Split('\\');
string[] savedName = splitFile[splitFile.Length - 1].Split('.');
string[] lineNumber = savedNamed[2].Split('-');
string fileName = savedNamed[1] + "-" + lineNumber[0] + ".txt";
foreach (string line in fileList)
{
string[] splitLine = line.Split('\n');
for (int i = 0; i < splitLine.Length; i++)
{
string tempSplit = splitLine[i].Split(','); // splits each line by commas
if (tempSplit.Length.Equals(16))
{
tempSplit[7] = tempSplit[7].TrimStart('"'); //trims the quotes from the part numbers
tempSplit[7] = tempSplit[7].TrimEnd('"');
}
}
}
partNumberLines = partNumberLines.Distinct().ToList(); //gets rid of duplicate partnumbers in one file.
所以我的代码正在获取所有零件号和文件名。我只是不知道如何打开现有文件(如果不存在,则创建它)并搜索该文件并在列表: partNumberLines
中查找匹配项。如果匹配,则将文件名连接到当前行。如果不匹配,请创建一个新行并添加部件号和文件名,然后按部件号对文件进行数字排序。
谁能帮我解决这个问题吗?
I am writing a program that opens up one file at a time, parses it, and outputs the parsed data into a new .txt document that is named based off of the file coming in. There are over 50 files that are going to be read and parsed.
So if the opened file was named something like this: STACK-OVERFLOW-125663-D2.txt
, the output file would be something like this 125663-D2.txt
.
Each time a file is read, it is parsed for their part numbers. Each file will contain lines similar to this (The 8th seperated comma values (ie, 119082, 119083, 119040, 119085, 119084) are the part number values.):
"00003",6,"D","C20",-70.10,42.06,90.00,"119082",0,1,2,0,0,"",0,"001"
"00004",6,"D","C21",-67.91,42.06,90.00,"119082",0,1,2,0,0,"",0,"001"
"00005",13,"D","C23",-66.91,59.07,180.00,"119083",0,1,2,0,0,"",0,"002"
"00006",13,"D","R10",-77.32,66.88,90.00,"119040",0,1,2,0,0,"",0,"003"
"00007",13,"D","L3",-77.64,77.48,90.00,"119085",0,1,2,0,0,"",0,"004"
"00008",20,"D","D1",-62.91,103.77,0.00,"119084",0,1,2,0,0,"",0,"005"
"00009",21,"D","D1",-25.83,103.77,0.00,"119084",0,1,2,0,0,"",0,"005"
"00010",14,"D","L3",-40.56,77.48,90.00,"119085",0,1,2,0,0,"",0,"004"
"00011",14,"D","R10",-40.24,66.88,90.00,"119040",0,1,2,0,0,"",0,"003"
Now what I need to do is check another .txt file.. let's say it is called "DATABASE.txt" to see if these part number exist in there already. This data base file will look something like this:
119082: 125663-D2, 123456-A1,
119083: 125663-D2,
119085: 125663-D2, 123456-A1, 987654321-Z11234, 1111111-B50
So, in the DATABASE.txt file and the file opened above, I want to check all of the part numbers from the opened file and see if they exist in the data base.
-
If the part does exist, I want to concat the filename (the output file) to the end of the line that the part number was found on.
-
If the part does not exist, I want to add the part to the file and sort the file using list.Sort()
.
I am unsure on how to do this, can anyone help?
Here is some of my code so far:
List<string> partNumberLines = new List<string>();
string file = openFile.FileName;
string splitFile = file.Split('\\');
string[] savedName = splitFile[splitFile.Length - 1].Split('.');
string[] lineNumber = savedNamed[2].Split('-');
string fileName = savedNamed[1] + "-" + lineNumber[0] + ".txt";
foreach (string line in fileList)
{
string[] splitLine = line.Split('\n');
for (int i = 0; i < splitLine.Length; i++)
{
string tempSplit = splitLine[i].Split(','); // splits each line by commas
if (tempSplit.Length.Equals(16))
{
tempSplit[7] = tempSplit[7].TrimStart('"'); //trims the quotes from the part numbers
tempSplit[7] = tempSplit[7].TrimEnd('"');
}
}
}
partNumberLines = partNumberLines.Distinct().ToList(); //gets rid of duplicate partnumbers in one file.
So my code is getting all of the part numbers and the name of the file.. I just do not know how to open up an existing file (and if it does not exist, create it) and search through the file and look for matches in the List: partNumberLines
. And if it matches, concat the file name to the current line. If it does not match, create a new line and add the part number and file name and then sort the file numerically by part number.
Can anyone help me figure this out?
发布评论
评论(3)
希望你还没有放弃。这是一个示例类。我已经从上一篇文章完成了它。将上面的数据库数据保存到
database.txt
并将零件数据保存到parts.txt
并修改路径以查看其工作原理。希望对您有帮助。如果您还有其他疑问,请随时提问。编辑
如果您想要一个静态数据库并且希望允许用户选择零件文件,那么您可以在按钮单击事件中执行此操作:
Hope you haven't given up. Here's a sample class. I've completed it from my last post. Save your database data above to
database.txt
and your parts data toparts.txt
and modify the paths to see how it works. Hope it helps you. If you have any more questions, feel free to ask.Edit
If you want a static database and you want to allow the user to choose a parts file, then you could do this in a button click event:
不要自己解析 CSV。为此,请使用 FileHelpers 库。可能会有很多异常,FileHelpers 可以很好地处理这些异常。
其次,您正在执行足够的数据操作,简单的数据库可能会有所帮助。也许是 SQL Express,或者单个文件嵌入式数据库(SQL Server Compact、SQLite)。
最后,要手动执行此操作,您只需在内存中构建表即可。您真正拥有的是零件编号和文件之间的多对多关系。所以你有两个表和一个连接表。
现在,由于“filename”在您的示例中只有一个属性 ( filename ),因此可以将其作为附加列附加到连接表中。所以你有 2 张桌子。第二个看起来像
您已有的第一个表。
因此,如果您使用
List
在内存中复制这两个表,您应该能够使用 LINQ 轻松完成此操作。尽管我个人会构建新的类或至少结构来表示两个表元组。>
希望这有帮助。
Do not parse CSV yourself. Use FileHelpers Library for that. There can be a lot of exceptions, and FileHelpers handles those really well.
Secondly, you're doing enough data operations were a simple database might help. Maybe SQL Express, or a single file embedded database ( SQL Server Compact, SQLite ).
Finally to do this manually, you simply have to build out your tables in memory. What you really have is a Many-to-Many relationship between part numbers and files. So you have two tables and a join table inbetween.
Now since "filename" has only a single attribute ( filename ) in your example, it can be appended in the join table as an additional column. So you have 2 tables. The second looks like
The first table you already have.
So if you replicate these two tables in memory using
List<List<string>>
you should be able to finish this with LINQ with no trouble at all. Though personally I would build out new classes or at least structs to represent the two table tuples.Hope this helps.
这是一个快速运行..它肯定需要改进,但它是一个开始:)
Here's a quick run at it .. it could definitely use improvement but its a start :)