我正在创建一个将数据框转换为 .txt 文件的函数。
import pandas as pd
def print_table(dataframe):
headers = dataframe.columns.to_list()
table = dataframe.values.tolist()
with open('file.txt','w') as file:
file.write(''.join(column.rjust(40) for column in headers))
for row in table:
with open('file.txt','w') as file1:
file1.write(''.join(str(column).ljust(20) for column in row))
df = pd.DataFrame({'Yoruba': ['Wèrè èèyàn ní ńwípé irú òun ò sí; irú ẹ̀ẹ́ pọ̀ ó ju ẹgbàágbèje lọ.','Wọ́n ńpe gbẹ́nàgbẹ́nà ẹyẹ àkókó ńyọjú.'],
'Translation': ['Only an imbecile asserts that there is none like him or her; his or her likes are numerous, numbering more than millions.',
'The call goes out for a carpenter and the woodpecker presents itself.'],
'Meaning': ['No one is incomparable.',
"One should not think too much of one's capabilities."]})
这就是我希望 .txt 文件的样子
Yoruba Translation Meaning
"Wèrè èèyàn ní ńwípé irú òun ò sí; irú ẹ̀ẹ́ pọ̀ ó ju ẹgbàágbèje lọ." "Only an imbecile asserts that there is none like him or her; his or her likes are numerous, numbering more than millions." "No one is incomparable."
"Wọ́n ńpe gbẹ́nàgbẹ́nà ẹyẹ àkókó ńyọjú." "The call goes out for a carpenter and the woodpecker presents itself." "One should not think too much of one's capabilities."
但这是我得到的错误
UnicodeEncodeError:“charmap”编解码器无法在位置 14 编码字符“\u0144”:字符映射到
婷婷同学_
慕勒3428872
相关分类