Excel table:
Column A | Column B |
---|---|
Cell 1 | Mar-21 |
Cell 3 | Apr-21 |
Cell 3 | Dec-21 |
Expected CSV table:
Column A | Column B |
---|---|
Cell 1 | Mar-21 |
Cell 3 | Apr-21 |
Cell 3 | Dec-21 |
however, due to excel data format issue, the Column B is automatically accepting the column values as date (e.g. 01/03/2021, 01/04/2021 and 01/12/2021).
from pathlib import Path
for path in Path(excel_dir).rglob('*.xlsx'):
print(path.name)
df= pd.read_excel(path)
if 'Wave' in df.columns:
df = pd.read_excel((path), dtype = {'Wave': str})
else:
df = pd.read_excel(path)
file_name = os.path.basename(path).split('.')[0]
csv_file_name = f'{file_name}.csv'
df.to_csv(os.path.join(csv_dir,csv_file_name), index = False)
Using dtype
as str
I am not able to convert Column B as expected column values. Rather it is giving me result like 01/11/2021 12:00:00 am
.
How can I convert excel into csv without changing Column B values?
CodePudding user response:
Pandas Module support XLSX file format, check link XLSX File format Read in Pandas
import pandas as pd
data = pd.read_excel('path/of/xlsx')
data['Column B'] = data['Column B'].astype(str)
try the above code. It will convert the Column data to string
CodePudding user response:
Maybe this would work as you want:
df['Date'] = df['Date'].apply(lambda x: str(x))