python - 如何使用 python 和 pandas 编写带有阿拉伯字符的 csv 文件?
问题描述
我有一个读取 csv 或 excel 文件并尝试复制和粘贴或使用 pandas 包读取和写入文件的功能。
当我尝试将带有阿拉伯字符的文件写入 csv 文件时,它会显示符号和不可理解的字符。
如何指定utf-8文件的编码并能够对其进行解码?
功能 :
import pandas as pd
import numpy as np
import base64
def download_file(df, types, new_types, extension):
for i, col in enumerate(df.columns):
new_type = types[new_types[i]]
if new_type:
try:
df[col] = df[col].astype(new_type)
except:
st.write('Could not convert', col, 'to', new_types[i])
# csv
if extension == 'csv':
csv = df.to_csv(r"F:\AIenv\update2.csv",index=False,header=True,encoding="utf8")
b64 = base64.b64encode(csv.encode()).decode("utf-8")
else:
excel = df.to_excel(r"F:\AIenv\update2.xlsx", index = False, header=True,encoding="utf-8")
b64 = base64.b64encode(excel.encode()).decode("utf-8")
我在这里调用函数:
if btn1:
download_file(df, types, new_types, "csv")
if btn2:
download_file(df, types, new_types, "xls")
我的代码中的错误在哪里?
解决方案
推荐阅读
- unix - 从 HDFS FS 获取文件名,其中文件包含特定字符串
- r - 如何在数据框的列中嵌套表?
- react-native - 如何将参数传递给使用 useRoute() 的屏幕
- java - 如果满足某些条件,如何停止线程执行
- r - 如何选择两个事件之间的所有行
- regex - 正则表达式命令在第二个空格后获取字符串
- amazon-web-services - 使用 AWS Amplify 和 Pinpoint 进行分析
- r - 选择最频繁的组合,同时删除该组合的部分 R
- javascript - Chrome 和 Safari Mobile 上不同的自动调整大小
- linux - 树莓派 3:在 arm64 系统上运行 arm32 程序