简体   繁体   中英

Python - How to convert txt to excel file using pandas

I tried to follow How to convert txt to excel file however the problem seems to still be where it seems to write a start ID at the first column which I don't really want to. For now it looks like:

在此处输入图片说明

and this is what I am looking for:

在此处输入图片说明

What I have done is that I have written a code that read a txt file that looks like:

id,quanitity,bu,link
80379173,1000.0,045,https://helloworld.com/-80379173/
10290396,1000.0,045,https://helloworld.com/-10290396/
40379170,1000.0,045,https://helloworld.com/-40379170/
20379171,1000.0,045,https://helloworld.com/-20379171/

by a code:

import pandas as pd

    excel = 'Available-Online-ART.txt'

    df = pd.read_csv(excel, sep=',')

    column_indexes = list(df.columns)

    df.reset_index(inplace=True)
    df.drop(columns=df.columns[-1], inplace=True)

    column_indexes = dict(zip(list(df.columns), column_indexes))

    df.rename(columns=column_indexes, inplace=True)
    df.to_excel('output.xlsx', 'Sheet1', index=False)

but it seems like it still adds the first column but also removing the link at the end. I wonder how I can do it to make it works as the second picture?

You have overthought the problem:

df = pd.read_csv(excel, sep=',')
df.to_excel('output.xlsx', index=False)

is enough...

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