我有一個包含英文和中文的csv檔案,如何將它們分開,然后將包含中文的保存為“中文”,將不包含中文的保存為“英文”,我找到了一個代碼來區分但我沒有不知道如何拯救他們。
def is_chinese(string):
for ch in string:
if u'\u4e00' <= ch <= u'\u9fff':
return True
return False
ret1 = is_chinese("a中國aaa")
print(ret1)
ret2 = is_chinese("123")
print(ret2)
.csv 檔案
"sex","name","age"
"1","hali","18"
"2","張三","24"
"1","云lee","20"
我想這樣分類: Eeglish
"sex","name","age"
"1","hali","18"
中國人:
"sex","name","age"
"2","張三","24"
"1","云lee","20"
uj5u.com熱心網友回復:
此代碼輸出包含漢字的行并將其保存到名為“detected.txt”的檔案中
import re
characters=[]
i = 0
with open('01.csv','r',encoding='utf-8') as file: #Open CSV file
with open('detected.txt', 'r ') as f: #Open file to write
for line in file.readlines(): #Read each line of CSV file
if re.findall(r'[\u4e00-\u9fff] ', line) == []: #If there is no Chinese character in the line
pass
else:
characters.append(re.findall(r'[\u4e00-\u9fff] ', line)) #Append the Chinese character to the list
if str(characters[i][0]) in line: #If the Chinese character is in the line
f.write(line) #Append the line to the file
i =1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/495893.html
