简体   繁体   中英

python tab delimited file to excel

Python newbie, could you point me in the right direction.

I have a text file that is tab delimited as follows:

Hello
      world
            !

What I require is for this to be transposed into excel as follows:

Hello
Hello world
Hello world !

Obviously the file that I am dealing with is considerably larger than this. I need it this way so that I can create a directory structure:

Hello ---> expands to world ---> expands to !

I have been researching pandas, csv modules etc etc, but don't understand enough about these modules or their docs and the fact that I'm a newbie.

Have anyone managed to accomplish this before I waste hours more trying to figure this out. Any code you could offer up would be appreciated, if not just a simple yes this is achievable. Currently pulling my hair out on where to start. Have managed simple for loops that iterates over the file line by line and displays in the IDE window, but failing miserably to get it to excel in the required format.

We don't really have enough information to understand how to convert that (rather strange looking) format into something flat. That's where you want to end up though; something like:

data = [('Hello', '', ''), ('Hello', 'world', ''), ('Hello', 'world', '!')]

In terms of working with excel files - xlsxwriter is the best python library I've found to date. It has good documentation for everything you'll want to do. This is all you need to get started https://xlsxwriter.readthedocs.org/tutorial01.html

Very do-able. If you commit to learning python you'll be amazed at what you can achieve.

Since this question is high level, here's an equally high level idea of where I'd start:

-Create a function that reads the text file ( https://docs.python.org/2/tutorial/inputoutput.html )

-Scan for the delimiting value (ASCII Decimal value of 9 in this case)

-Store the data in a list of lists (each list entry being a row of data for your future excel sheet)

-Create and excel sheet and transfer the list of lists into rows/cols. For this I'd suggest xlwt, it's fairly straight forward and very well documented. (examples here https://github.com/python-excel/xlwt/tree/master/xlwt/examples )

Cheers and good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM