0

Given a .xlsx file i want to convert it to .csv with the following format using Python

Given hex addresses .xlsx :

0xbfb22b18
0xbfb22b14
0xbfb22b10
0xbfb22b0c
0xbfb22b18
0xbfb22b14
0xbfb22b10
0xbfb22b0c

I need to convert to int .scv and add at the first line row the word "train_data"

train_data
3216124696
3216124692
3216124688
3216124684
3216124696
3216124692
3216124688
3216124684

The only relative I found is using panda , but I don't know how I can add the "train_data" .

2 Answers 2

0

How about

data = open('tmp.xlsx', 'r').read()

numbers = map(lambda x: str(int(x, 16)), data.split())

content = 'train_data\n' + '\n'.join(numbers) + '\n'

with open('tmp.csv', 'w') as f:
    f.write(content)
Sign up to request clarification or add additional context in comments.

Comments

0

Here You go:

import pandas as pd

df = pd.read_excel('data.xlsx', index_col=None, names=['train_data'])
df['train_data'] = df['train_data'].apply(lambda x: int(x, 16))
df.to_csv('data.csv', index=False)

Comments

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.