python 讀取中文文件路徑、文件內容

-- coding: utf-8 --

“”"
Created on Mon Oct 21 11:22:13 2019
功能:測試讀取中文txt文件名,含中文字符串內容
@author: Acer201209
“”"
import pandas as pd

# 文件路徑爲中文處理
path1 ='D:\\XXXXXXXXXX_加速度.txt' 
path2 = 'XXXXXXXXXX_加速度.txt'
path3 = '1.txt'
f = open(path1)
df = pd.read_csv(f)
f.close()
print(df.shape)

#-----txt中存在’非數字’處理,類型爲str----------------

#---------path3:文件名爲英文處理---------------------------
test1 = pd.read_csv(path3,header = None,sep=' ')
boola = np.where(test1 == '非數字')
boolaa = boola[1]   #非數字所在的位置
test11 = test1.copy()
test11[boolaa] = 0

print(test11[5][0])
print(test11[6][0])
print(test11[7][0])
print(test11[8][0])
test111 = test11.values.ravel()
#f.close()

#----------文件名爲中文,內容含‘非數字’-----------------------

#---------path2:文件名爲中文,內含'非數字'---------------------------
test_china = open(path2,"rt", encoding="utf-8")
test1 = pd.read_csv(test_china,header = None,sep=' ')#

boola = np.where(test1 == '非數字')
boolaa = boola[1]   #非數字所在的位置
test11 = test1.copy()
test11[boolaa] = 0

print(test11[5][0])
print(test11[6][0])
print(test11[7][0])
print(test11[8][0])
wave = test11.values.ravel()
test_china.close()

‘’’
f = open(path1)
df = pd.read_csv(f)
f.close()
print(df.shape)
‘’’

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章