Python 尝试将文件转换为excel…[宏]
txt文件正在使用Python转换为Excel xls文件。但是,当文本文件转换为Excel xls文件时,某些新行将不起作用Python 尝试将文件转换为excel…[宏],python,vba,Python,Vba,txt文件正在使用Python转换为Excel xls文件。但是,当文本文件转换为Excel xls文件时,某些新行将不起作用 eg: In the txt file, the data is as follow: 1.ABC 2.DEF 3.GHI When it is converted to excel xls, the data is showed as follow: 1.ABC
eg: In the txt file, the data is as follow:
1.ABC
2.DEF
3.GHI
When it is converted to excel xls, the data is showed as follow:
1.ABC 2.DEF 3.GHI
换行符不可用。我希望使用宏启动换行。一个简单的方法是首先读取文本文件,并将每行存储为列表元素。然后使用pandas to_excel,您可以轻松地将该列表写入excel文件。 Pandas.to_excel与Pandas-to-datframe一起工作,因此我们需要将列表转换为Pandas-dataframe
import pandas as pd
#Let's say you have text file as my_text.txt
text_file = open("my_text.txt", "r")
#store each line as list element of list lines
lines = text_file.readlines()
#Convert list to pandas dataframe and store it in df
df = pd.DataFrame(lines)
df.to_excel('output.xlsx', header=False, index=False)
您将获得带有所需结果的output.xlsx文件。如果您不向我们显示代码,我们如何指出代码的错误?很抱歉,我无法显示代码。我希望用宏来实现这一点,因为结果是在excel文件中。