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
LordLazor
  • 39
  • 1
  • 7

1 Answers1

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

Corralien
  • 109,409
  • 8
  • 28
  • 52