-3

I basically have a text file with data as:

Neighbor        V    State/Pf
10.230.2.91     4        1178
10.229.5.239    4        1177
10.229.6.239    4        1173

I want to read the values under neighbor column and put in excel. How can I achieve this? I am new to python, so please suggest me ways of doing this.

Anjali
  • 11
  • 3

2 Answers2

0

Pandas has methods to transform data frames into Excel files (pd.DataFrame.to_excel), but I agree with bernie: It is not very clear how your data look like

UPDATE

So it would look like

l = list(["Neighbor","10.230.2.91","10.229.5.239","10.229.6.239"])
df = pd.DataFrame(l[1:],columns=[l[0]])
df.to_excel("data.xls","data") # 2nd argument is the sheet name

You need to install the xlwt module which is not installed per default with Pandas

ChE
  • 88
  • 7
0

You'll want something simple that can output to a excel file.

You'll want to split your line into separate lines and only extract the parts that you want. In your case the .split() method would be very helpful. .split() takes your string and splits it based on the delimiter given (default is space). For example string.split('\n') will split the string by lines.

Something like this should be what you want:

import xlwt

book = xlwt.Workbook(encoding="utf-8")

sheet1 = book.add_sheet("Sheet 1")

temp[0].split('\n')

i = 0
for dataLine in temp[0]:
    sheet1.write(i, 0, dataLine.split()[0])
    i += 1

book.save("excel.xls")

Note: If you do not want the Neighbors heading you can skip that line and start i at 1.

Sean Wang
  • 778
  • 5
  • 14
  • @Anjali Oh so it is a space delimited file. And you only want the IP address looking 'Neighbor' column? You'll want to do some sort of loop for every third space delimited element or something to that effect. Also you may want to edit your question with that information. – Sean Wang Aug 12 '16 at 19:14
  • @Anjali Updated my answer to reflect your changes. You need to process your long string to get the parts that you want. – Sean Wang Aug 12 '16 at 19:25
  • Its still showing the same result. Is there any way I can read Neighbor column in array and then put in excel? – Anjali Aug 12 '16 at 19:43