0

I am using python 3.7 and I wanted to convert .xlsx file into .txt file and below is my code:

import pandas as pd

dataframe1 = pd.read_excel(r'C:\path\exceldata1.xlsx', index=False)

print(dataframe1)

with open(r'C:\path\exceldata1.txt', 'w') as text_file:
    dataframe1.to_string(text_file)

I am able to convert .xlsx into .txt but I am also getting the index value printed in the text file. I want to remove that.

0     9100499S    1
1     9100099S   10
2     910000SW    1
3     91Y961SR    5
4     9120301S   20

above is the text file created but I do not need the index values in the first column.

and i also wanted to separate the two column values by tab to make it into a TVS text file so that they do not come in one cell..How do i go about it??

9100499S   1
9100099S   10
910000SW   1
91Y961SR   5
9120301S   20

1 Answer 1

2

Adding index = False to your to_string call will prevent the index from being included.

If you want tab separated values, instead of to_string you should use to_csv - this function is meant to generate comma separated values, but you can change what character is used as a separator by adding sep='\t' to get tab separated values instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you also answer the next part of my question which i updated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.