比较文件无法正常工作 [英] Comparing files not working as intended

查看:77
本文介绍了比较文件无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么这行不通.

hi guys could someone explain to me why this does not work.

我基本上必须要输入名为Books和NewBooks的文本文件...

I basically have to text files called Books and NewBooks...

从Web请求中填充文本文件,然后将信息解析为文本文件...当我启动程序时,书和新书是相同的,并且几乎是彼此的副本.

The text files are populated from a web request and the info is then parsed into the text files...when I start the program Books and new books are identical and pretty much a copy of each other.

完成了更多的Web请求以更新NewBooks文本文件,当我比较它们时,如果NewBooks中有一行不在Books中,则它将把该行添加到名为myNewBooks的第三个文本文件中.现在,我将在此处显示的初始代码可以按预期运行

more web requests are done to update the NewBooks text file and when I compare them if there is a line in NewBooks that is not in Books it adds that line to a third text file called myNewBooks. Now my initial code that I will show here works as I expected

Dim InitialBooks = File.ReadAllLines("Books.json")
    Dim TW As System.IO.TextWriter
    'Create a Text file and load it into the TextWriter 
    TW = System.IO.File.CreateText("myNewBooks.JSON")

    Dim NewBooks = String.Empty
    Using reader = New StreamReader("NewBooks.json")
        Do Until reader.EndOfStream
            Dim current = reader.ReadLine
            If Not InitialBooks.Contains(current) Then 
                NewBooks = current & Environment.NewLine

                TW.WriteLine(NewBooks)
                TW.Flush()
                'Close the File 
            End If
        Loop
    End Using
    TW.Close() : TW.Dispose()

但是由于我的文本文件行中的字符串的一部分包含一个url,有时我会发现同一本书的URL有所不同...我得到了重复的书籍条目,因为URL是唯一的区别.所以我以为我会在url之前分割字符串,以便只比较标题,描述和区域...在文本文件中的一行看起来像这样:

but because part of the string in my text file lines contain a url which sometimes I find the same book with a different url... I was getting duplicate entries of books becuase the url was the only difference. So I thought that I would split the string before the url so that I just compare the title and description and region ...fyi a line in my text files look similar to this:

{标题":此处为我的标题",描述":此处为我的描述",区域":此处为我的区域",网址":此处为我的网址",图片":我的图片在这里};

{ "Title": "My Title Here", "Description": "My Description Here", "Region": "My Region Here", "Url": "My Url Here", "Image": "My Image Here" };

因此,今天有一个人帮助我弄清楚了如何分割线,使其看起来更像这样:

So a fellow today helped me figure out how to split my line so it looks more like this:

{标题":此处为我的标题",描述":此处为我的描述",区域":此处为我的区域",网址"

{ "Title": "My Title Here", "Description": "My Description Here", "Region": "My Region Here", "Url"

这很棒,但是现在我比较时看不到第一行包含分隔线,我也不明白为什么...这是修改后的代码.

which is great but now when I compare it does not see that the first line contains the split line and I don't understand why... here is the code after it was modified.

Dim InitialBooks = File.ReadAllLines("Books.json")
    Dim TW As System.IO.TextWriter
    'Create a Text file and load it into the TextWriter 
    TW = System.IO.File.CreateText("myNewBooks.JSON")

    Dim NewBooks = String.Empty
    Using reader = New StreamReader("NewBooks.json")
        Do Until reader.EndOfStream
            Dim current = reader.ReadLine
            Dim splitAt As String = """Url"""
            Dim index As Integer = current.IndexOf(splitAt)
            Dim output As String = current.Substring(0, index + splitAt.Length)
            If Not InitialBooks.Contains(output) Then 
                NewBooks = current & Environment.NewLine

                TW.WriteLine(NewBooks)
                TW.Flush()
                'Close the File 
            End If
        Loop
    End Using
    TW.Close() : TW.Dispose()

您的智慧将不胜感激!

推荐答案

您的OP令人困惑.

如果我正确理解:

您有3个文件图书,NewBooks和MyBooks. 您可以从网上下载数据,如果该数据不在书籍"中,则可以将其添加到NewBooks,否则将其添加到MyBooks(重复项).

You have 3 files Books, NewBooks and MyBooks. You download data from web, if that data is not located in Books, you add it to NewBooks, otherwise to MyBooks(duplicates).

看到您正在使用JSON,我将通过以下方式进行操作.

Seeing that you are working with JSON i would do it the following way.

加载书籍,在下载数据时将其检查并与书籍进行比较.然后写入适当的文件.

Load the Books, when downloading data check it and compare it with Books. Then write to proper file.

Imports System.Web.Script.Serialization ' for reading of JSON (+add the reference to System.Web.Extensions library)
Dim JSONBooks = New JavaScriptSerializer().DeserializeObject(Books_string)

检查具有断点的JSONBooks.您将看到它的外观. 删除数据时,您只需按标题,URL或您想要的任何内容检查其中是否存在书籍.

Inspect JSONBooks with breakpoint. You will see how it looks. When downlaoding data you can simply check if book exist in it, by title, url or whatever you want.

由于您只展示了一本书

Debug.Print(JSONBooks("Title")) 'returns >>>My Title Here

如果您有更多

JSONBooks(x)("Title") 'where x is book number. 

因此,您可以遍历所有书籍并检查所需内容.

So you can loop over all books and check what you need.

JSON数组看起来像这样(如果您需要构造它)

JSON array looks like this (if you need to construct it)

[{book1},{book2},...]

这篇关于比较文件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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