Python - Read and split lines from text file into indexes.

Discussion in 'App Development' started by OTM-Options, Apr 28, 2015.

  1. What's the purpose of not using libraries? learning?
     
    #21     May 6, 2015

  2. I had a lot of problems with lists and dictionaries, I ended up with special characters I couldn't get rid of. That's why I also call the write function with each loop, instead of writing once to file at the end. 100 lines will call the write function 100 times.




    :)
     
    #22     May 6, 2015
  3. Special characters huh?
    Are you using the same encoding on the file as you use in the program?

    With 100 lines your approach should work fine, but those IO's to HDD will become expensive if you go into larger files.
     
    #23     May 6, 2015
  4. Well I'm glad that you got it to work. But you've ended up with some real ugly code there (your code is so ugly..... pick your own punchline http://www.jokes4us.com/yomamajokes/yomamasouglyjokes.html). Hope you, or someone else, never has to debug it.


     
    #24     May 7, 2015
    i960 and eusdaiki like this.
  5. This is hilarious, 3 pages of Python chaos to read a simple text based file with delimiter. Seriously, guys, I thought some of you are the die hard Python users...

    OP, here is some C# code in case you are getting tired of even considering to use a library (chuckle, chuckle) to read a simple csv based file:

    var contents =File.ReadAllText(filename).Split('\n');
    var csv =from line in contents
    Select line.Split(',').ToArray();
     
    #25     May 7, 2015
    Yawning Lion likes this.
  6. well... if you put it that way...
    the job could be done quite easily using the right tools...

    Code:
    import pandas as pd
    excel_file = pd.ExcelFile('MyBelovedMICROSOFTExcelFile.xlsx')
    excel_page = excel_file.parse(excel_file.sheet_names[0])
    
    
    
     
    #26     May 7, 2015
  7. via the import of a library? Seriously?

    Btw, I thought the task was to read in a text based file. Why are you now providing code to read in a spread sheet?

    Just to summarize, here is what the Python experts suggest to read in a simple text file:

    => Use CSV library
    => Use Pandas library
    => Use Anacondas distro

    Not re-inventing the wheel is a good thing, but that you even have to peruse a library to read in and properly parse a simple text file is absolutely beyond me. I thought that kind of job is what Python was designed to tackle.


     
    Last edited: May 7, 2015
    #27     May 7, 2015
  8. With all due respect, but the code you previously published on your blog (and which by your own admission was so horrible that you continued to only post pseudo code) did not look that much more appealing nor efficient...

     
    #28     May 7, 2015
  9. Libraries get the job done faster in coding time, and usually faster in run time (since they have optimized routines written in C under the hood), and in the case of pandas it also allows for much easier manipulation of the data after it has been imported through the dataframe structure.


    oh yeah, I forgot and used Excel :)
    here's the code for csv text files

    Code:
    import pandas as pd
    csv_file = pd.DataFrame.from_csv("myBelovedCSVfile.csv", sep=',')
    and here's the code to do the same in R... when it comes to doing a lot with a single line, it is hard to beat R.

    Code:
    csv_file = read.csv('myBelovedcsvFile.csv', sep=',')
     
    #29     May 7, 2015
  10. And here is the code in C# :

    var contents =File.ReadAllText(filename).Split('\n');
    var csv =from line in contents select line.Split(',').ToArray();

    (you can actually move that all into one line but it looks ugly)

    P.S.: This version is most likely a lot faster than Python's Pandas. I know OP asked about a Python solution but I could not help it but jump in after seeing 3 full pages of discussion how to read in a text file.



     
    #30     May 7, 2015