简体   繁体   中英

How can i correct my error and make the dataframe a text file using functions in python

I am creating a function to convert a dataframe to a .txt file.

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."]})

This is how i want the .txt file to look like

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."


**and not this**
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."

But this is the error i am getting

UnicodeEncodeError: 'charmap' codec can't encode character '\ń' in position 14: character maps to

This largely works for me:

fout = open("file.txt", 'w', encoding='utf-8')

df = df[['Yoruba', 'Translation', 'Meaning']]


lengths = [len(max(val, key=len)) for val in df.values.T]
for i in range(len(df.columns)-1):
    fout.write("{heading:<{length}} ".format(heading=df.columns[i], length=lengths[i]))
fout.write("{}\n".format(df.columns[-1]))

rows, columns = df.values.shape
for i in range(rows):
    for j in range(columns-1):
        fout.write("{val:<{number}} ".format(val=df.values[i,j], number=lengths[j]))
    fout.write("{}\n".format(df.values[i, j+1]))

fout.close()

I think the bit you're missing is the "encoding='utf-8'". Hope this helps!

In this case, it would seem that there is an issue between your input text encoding and the panda output encoding on which you are trying to perform.

Please take a look at this answer: here on how to handle encoding and decoding characters when writing and reading characters (you may need different to UTF-8!)

As for styling the output, an easier mechanism may to use csv as shown here and use spaces for delimiting seeing as your data is already in a tabular form from pandas.

This worked in my case as an alternative to using joins. If you have any other problems, please try use encode/decode.

import pandas as pd
import csv

def print_table(dataframe):  
    with open('file.csv', 'w', newline="") as csvfile:
        writer = csv.writer(csvfile, delimiter="\t", quotechar='', quoting=csv.QUOTE_NONE)
        writer.writerow(heading) for heading in dataframe.columns)

        for row in dataframe.values:
            writer.writerow(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."
    ]
})

print_table(df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM