FlineDev / CSVImporter

Import CSV files line by line with ease

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Output full of "0\"

Patrick3131 opened this issue · comments

Thanks for your frameworks.

I have been trying to use your framework.

But I keep getting output I can not use, do you know why I get an output like this:

["O\0r\0d\0e\0r\0U\0u\0i\0d\0", "\0E\0x\0c\0h\0a\0n\0g\0e\0", "\0T\0y\0p\0e\0", "\0Q\0u\0a\0n\0t\0i\0t\0y\0", "\0L\0i\0m\0i\0t\0", "\0C\0o\0m\0m\0i\0s\0s\0i\0o\0n\0P\0a\0i\0d\0", "\0P\0r\0i\0c\0e\0", "\0O\0p\0e\0n\0e\0d\0", "\0C\0l\0o\0s\0e\0d\0\r\0"]

If you take the "0\" away it would be the output I need, why does this occur?

Okay I figured it out, but I have an other problem. I think the framework creates an invalid array, if I want to loop through the array afterwards I get an out of range error. I handled this by removing the last element of the array. Is this a known problem or am I doing something wrong?

Hi Patrick. Thanks for your question. Please could you provide more details for both: The issue you fixed (so others might find this and see the solution) and the new problem with the array you have. I'm not sure what exactly you mean, maybe you could provide example code of how you use CSVImporter?

Hi Dschee! Thanks for getting back.
I am using CSVImporter synchron.
The output of CSVImporter was still full of "\0" and the last character of a line returns "\r" probably because of the line break. I now check the output afterwards and replace the "\0" and "\r". I tried to use the asynchron version as well, same result.

The last element of the csvoutput equals ["\0"] therefore if I loop through the array to build my object it can not access it, so I delete it after the import. Did this problems occur before or am I using it wrong?

My code looks like this:

struct Object {
    let orderUuid: String //0 Position in Csv
    let exchange: String //1
    let typeOrder: String //2
    let quantity: Double //3
    let limit: Double //4
    let commissionPaid: Double  //5
    let pricePaid: Double  //6
    let opened: Date?   //7
    let closed: Date?  //8
    
    init(orderUuid: String, exchange: String, typeOrder: String, quantity: Double, limit: Double, commissionPaid: Double, pricePaid: Double, opened: Date?, closed: Date?){
        self.orderUuid = orderUuid
        self.exchange = exchange
        self.typeOrder = typeOrder
        self.quantity = quantity
        self.limit = limit
        self.commissionPaid = commissionPaid
        self.pricePaid = pricePaid
        self.opened = opened
        self.closed = closed
    }
    
 
    func csvImport() -> [Object]{
        var accountHistoryOfCsv = [Object]()
        var importedCsv: [[String]]  = []
        var firstRowCsv: [String] = []
        
        let path = "/Users/path/csv.csv"
        let defaultImporter = CSVImporter<[String]>(path: path)
        
        let result = defaultImporter.importRecords{ $0 }
        importedCsv = result
        //print(importedCsv[1])
        
        var lastElementImportedCsv = importedCsv.last
        
        while lastElementImportedCsv! == ["\0"] && importedCsv.count > 0 {
            importedCsv.removeLast()
            lastElementImportedCsv = importedCsv.last
            print("deleted")
            // Hier wird gecheckt ob ["\0"] dem letzten Element entspricht, output durch csvimporter, das wird solange entfernt 
        }
        if importedCsv.count > 0 {
            
        // Erstes Element wird entfernt und geschaut ob es dem entsprechenden Csv entspricht
        firstRowCsv = importedCsv.removeFirst()
            

        let bereinigenFirstRowCsv = firstRowCsv.map{ $0.replacingOccurrences(of: "\0", with: "")}
            
            if bereinigenFirstRowCsv.count >= 8 {
            
        if bereinigenFirstRowCsv[0] == "OrderUuid"
            && bereinigenFirstRowCsv[1] == "Exchange"
            && bereinigenFirstRowCsv[2] == "Type"
            && bereinigenFirstRowCsv[3] == "Quantity"
            && bereinigenFirstRowCsv[4] == "Limit"
            && bereinigenFirstRowCsv[5] == "CommissionPaid"
            && bereinigenFirstRowCsv[6] == "Price"
            && bereinigenFirstRowCsv[7] == "Opened"
            && bereinigenFirstRowCsv[8].replacingOccurrences(of: "\r", with: "") == "Closed" // in test file it somehow is "Closedr", eventuell durch den Zeilenumbruch
            && importedCsv.count > 0
        {
            
            for x in importedCsv {
                
                var checkOrderUuid = x[0]
                checkOrderUuid = checkOrderUuid.replacingOccurrences(of: "\0", with: "")
                
                
                var checkExchange = x[1]
                var transformExchange = ""
                if checkExchange != nil {
                    checkExchange = checkExchange.replacingOccurrences(of: "\0", with: "")
                    checkExchange = checkExchange.replacingOccurrences(of: "-", with: "/")
                    transformExchange = checkExchange
                } else {
                    checkExchange = "unknown"
                }
                
                var checkTypeOrder = x[2]
                var checkTypeOrderToString = ""
                checkTypeOrder = checkTypeOrder.replacingOccurrences(of: "\0", with: "")
                if checkTypeOrder != nil  {
                    checkTypeOrderToString = checkTypeOrder
                } else {
                    checkTypeOrderToString = "unknown"
                }
                
                
                
                var checkQuantity = x[3]
                var checkQuantityToDouble = 0.0
                checkQuantity = checkQuantity.replacingOccurrences(of: "\0", with: "")
                if Double(checkQuantity) != nil  {
                    checkQuantityToDouble = Double(checkQuantity)!
                } else {
                    checkQuantityToDouble = 0.0
                }
                
                var limit = x[4]
                var limitToDouble = 0.0
                limit = limit.replacingOccurrences(of: "\0", with: "")
                if Double(limit) != nil {
                    limitToDouble = Double(limit)!
                } else {
                    limitToDouble = 0.0
                }
                
                
                var commissionPaid = x[5]
                var commissionPaidToDouble = 0.0
                commissionPaid = commissionPaid.replacingOccurrences(of: "\0", with: "")
                if Double(commissionPaid) != nil {
                    commissionPaidToDouble = Double(commissionPaid)!
                } else {
                    commissionPaidToDouble = 0.0
                }
                
                var pricePaid = x[6]
                var pricePaidToDouble = 0.0
                pricePaid = pricePaid.replacingOccurrences(of: "\0", with: "")
                if Double(pricePaid) != nil {
                    pricePaidToDouble = Double(pricePaid)!
                } else {
                    pricePaidToDouble = 0.0
                }
                
                
                var opened = x[7]
                var openedDate: Date?
                opened = opened.replacingOccurrences(of: "\0", with: "")
                //print(opened)
                let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "M/dd/yyyy HH:mm:ss a"
                if dateFormatter.date(from: opened) != nil {
                   openedDate = dateFormatter.date(from: opened)
                } else {
                    openedDate = nil
                }
                
                var closed = x[8]
                var closedDate: Date?
                closed = closed.replacingOccurrences(of: "\0", with: "")
                //print(opened)
                let closedDateFormatter = DateFormatter()
                closedDateFormatter.dateFormat = "M/dd/yyyy HH:mm:ss a"
                if closedDateFormatter.date(from: opened) != nil {
                    closedDate = closedDateFormatter.date(from: closed)
                } else {
                    closedDate = nil
                }
                
                accountHistoryOfCsv.append(Object(orderUuid: checkOrderUuid, exchange: transformExchange, typeOrder: checkTypeOrderToString, quantity: checkQuantityToDouble, limit: limitToDouble, commissionPaid: commissionPaidToDouble, pricePaid: pricePaidToDouble, opened: openedDate, closed: closedDate))
                
            }
        } else {
            print("Invalid Csv File, please upload a valid Csv File")
            }
        } else {
            print("Invalid Csv File, please upload a valid Csv File")
        }
        } else {
            print("Invalid Csv File, please upload a valid Csv File")
        }
        print(accountHistoryOfCsv)
        return accountHistoryOfCsv
     
        }
    }

This is an example of my csv file:
https://www.dropbox.com/s/11u6hd5n0w9yto4/csv.csv?dl=0
I uploaded it on dropbox because Github does not support csv.

I have not been coding that long, so if you see anything I could do better I am happy to here it.

Okay, I've had a look into your code and here are some tips:

  1. The fixed path let path = "/Users/path/csv.csv" will only work on your computer. If you want to include this file in your app, make sure to copy the file into your Xcode project linking it against your target and load it in code like so:
let path = Bundle.main.path(forResource: "csv", ofType: "csv")!
  1. You don't need to write default initializers that simply set a value with the same name as the parameter for Structs in Swift. Swift generates them for you automatically. Try to simply delete your initializer and you will see that everything still works. :)

  2. I put your example code into a project, included CSVImporter as a Carthage dependency and run the code. When I set a breakpoint at line 32 (importedCsv = result) then the contents of result look like everything is just fine. I can't see any \0 as you are saying. I have no idea why you get them, look how it is shown for me (see the output on the bottom):

bildschirmfoto 2018-01-16 um 15 49 09

I've also uploaded my project as a zip file. Please try out if it works for you in the project or if you still receive the \0 everywhere when running it.

https://drive.google.com/open?id=1pebyR-sl7d8IF-aYfV_rkg63uVww3ieX

I hope this helps.

I'm going to close this as it doesn't seem like to be an issue with CSVImporter. Will reopen if you prove otherwise.

Hey Dschee, thanks a lot for your response. Sorry for not replying earlier I have been busy working, I havent worked on my project since then. I will test your code on the weekend and get back to you. Thanks again!