1

with python I'm reading an excel file via pandas.

My problem is that I'm missing the leading zeros and I can't just fill them to a specific length because it always varies.

Example: 001,0001,0020

This is my code for reading the data:

def readDataFromFile(self, file):
        try:
            df = pd.read_excel(file)
            list = df.values.tolist()
            print(f'{file} >> Read')
            return list
        except:
            print('No input or wrong input given')
            return
0

1 Answer 1

1

Use dtype as parameter of read_excel function to prevent pandas from converting string to number:

df = pd.read_excel(file, dtype={'your_column': str})

Without dtype:

>>> pd.read_excel(file)
   your_column
0            1
1            1
2           20

With dtype:

>>> pd.read_excel(file, dtype={'your_column': str})
  your_column
0         001
1        0001
2        0020

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.