Huge newbie to python and this is probably really easy, but I can't get my head around it at all.
I have a text file with a number of rows following this format
nothing doing nothing[0] doing[0]
hello world hello[0] world[2]
There are only spaces between the strings, no markers.
I'd like to extract these strings into excel file in the following format - so that each 'set' of strings are in a separate column.
| 1 | 2 | 3
------------------------------------------------------
1 | nothing doing | nothing[0] | doing[0]
------------------------------------------------------
2 | hello world | hello[0] | world[2]
I've been looking at answers on here but they don't quite full fill this question.
Alright, here's how you'd write to an actual Excel file. Note that my method of splitting isn't as complicated as others because this is mostly about writing to Excel. You'll need the python-excel package to do this.
>>> data = []
>>> with open("data.txt") as f:
... for line in f:
... data.append([word for word in line.split(" ") if word])
...
>>> print data
[['nothing doing', 'nothing[0]', 'doing[0]\n'], ['hello world', 'hello[0]', 'world[2]']]
>>>
>>> import xlwt
>>> wb = xlwt.Workbook()
>>> sheet = wb.add_sheet("New Sheet")
>>> for row_index in range(len(data)):
... for col_index in range(len(data[row_index])):
... sheet.write(row_index, col_index, data[row_index][col_index])
>>>
>>> wb.save("newSheet.xls")
>>>
This produces a workbook with one sheet called "New Sheet" that looks like this
Hopefully this helps