導航:首頁 > 凈水問答 > python過濾特殊t字元串

python過濾特殊t字元串

發布時間:2021-01-29 05:52:26

㈠ 如何用python正則表達式去除\r和\n字元

importre

a='abcdeef '
b=re.sub(' ','',a)#直接用空字元串替代
##b為'abcdeef'

㈡ python 字典的值為列表,想把一個列表的值拼在一起用『\t』隔開,變成一個字元串怎麼寫

比∨
廠)∴
4947644786634886

㈢ 一道python的字元串索引題目

你這個缺失了兩個知識點。
1、下標是有兩種表達方式,從左至右1,2,3...N,從右至左,-1,-2,-3...-N。內
對於容'china520'就是0到7或-1到-8,所以tstr[5:-1] 相當於tstr[5:7]。
2、下標取值,實際是3個參數,[inx1,inx2,direction]
inx1:起始下標
inx2:結束下標
direction:方向及步進值,默認是1,即左向右。-2就是右至左,間隔1個字元讀取
所以tstr[7:5:-1],7>5,一樣是可以取到值,只是取值的方向是相反的,返回02

㈣ python里的 \t 的長度具體是幾個字元

兩個。

㈤ python 搜索一個指定txt 的字元串 並輸出其下面兩行

fromtkinterimport*
fromtkinterimportttk
fromtkinterimportfont
importos
importarrange

#課程的字典
crslst={}
crslst_in_num={}

#GUI
root=Tk()

#frame2

#右側的內容
frame2=ttk.Frame(root,borderwidth=5,relief="sunken")
frame2.grid(column=1,row=0)

#將數字轉換為漢字的字典
numtoch={1:'一',2:'二',3:'三',4:'四',5:'五'}

#設置課表周圍周一至周五和1至13的標簽
fornuminlist(range(1,6)):
ttk.Label(frame2,text='周'+numtoch[num]).grid(column=num,row=0)
emptylist=[]
fornuminlist(range(1,14)):
ttk.Label(frame2,text='第'+str(num)+'節').grid(column=0,row=num)

#課表主體
schd1=Listbox(frame2,height=13)
schd1.grid(column=1,row=1,rowspan=13,sticky=(N,S,E,W))
schd2=Listbox(frame2,height=13)
schd2.grid(column=2,row=1,rowspan=13,sticky=(N,S,E,W))
schd3=Listbox(frame2,height=13)
schd3.grid(column=3,row=1,rowspan=13,sticky=(N,S,E,W))
schd4=Listbox(frame2,height=13)
schd4.grid(column=4,row=1,rowspan=13,sticky=(N,S,E,W))
schd5=Listbox(frame2,height=13)
schd5.grid(column=5,row=1,rowspan=13,sticky=(N,S,E,W))

#設置字體
ft=font.Font(family='MicrosoftYaHei',size=12,weight='normal')

#為了便捷將五天的課表作為一list
daylist=(schd1,schd2,schd3,schd4,schd5)

#設置課表
fordayindaylist:
day['selectmode']='extended'
day['exportselection']=False
fornuminrange(1,14):
day.insert(0,'無課')
day['font']=ft
day['width']=10


#課表下四個按鈕和一個combobox
#改變顏色
defchcolor():
print("33[1;42m33[0m")


butdont=ttk.Button(frame2,text='不上課',command=chcolor).grid(column=3,row=15)

#清除選擇
butusel=ttk.Button(frame2,text='清除選擇').grid(column=4,row=15)
Label(frame2,text='選擇課表').grid(column=0,row=15,columnspan=2,sticky=W)
outputlist=ttk.Combobox(frame2,textvariable=['待選','ok'])
outputlist.grid(column=1,row=15,columnspan=2,sticky=E)

#frame1
#左側的內容
frame1=ttk.Frame(root,borderwidth=5,relief="sunken")
frame1.grid(column=0,row=0)

#設置上方的label
Label(frame1,text='輸入課程信息').grid(column=0,row=0,columnspan=2)

#輸入課程號的Label和entry
Label(frame1,text='課程號:').grid(column=0,row=1,sticky=W)
coursenum=StringVar()
inputcoursenum=ttk.Entry(frame1,textvariable=coursenum,width=13)
inputcoursenum.grid(column=1,row=1,sticky=E)

#已選課程的listbox
Label(frame1,text='已選課程').grid(column=0,row=3,sticky=W)
courselist=Listbox(frame1,height=8,width=13,exportselection=False)
courselist.grid(column=0,row=4,columnspan=2,rowspan=8,sticky=W)


defisrtcrs():#加入課程
k=0
num=0
print(num,courselist.size())
whilenum<courselist.size():
ifinputcoursenum.get()==courselist.get(num):
k=1
num+=1
ifk==0:
courselist.insert(0,inputcoursenum.get())
crslst[inputcoursenum.get()]=[]
crslst_in_num[inputcoursenum.get()]=[]
print(crslst)
print(crslst_in_num)
return


defdelcrs():#刪除課程
#print(courselist.curselection())
#print(courselist.get(courselist.curselection()))
crslst_in_num.pop(courselist.get(courselist.curselection()))
crslst.pop(courselist.get(courselist.curselection()))
courselist.delete(courselist.curselection())
print(crslst)
print(crslst_in_num)
return


defshwtm(*args):#當選中一門課程,時間listbox改為對應的時間
print('here')
timelist.delete(0,END)
tmlst=crslst[courselist.get(courselist.curselection())]
iflen(tmlst)!=0:
fortimeintmlst:
timelist.insert(END,time)
return


#courselist的選中事件
courselist.bind('<<ListboxSelect>>',shwtm)

#加入課程和刪除課程的按鈕
addcourse=ttk.Button(frame1,text='加入課程',command=isrtcrs).grid(column=0,row=2)
deletcourse=ttk.Button(frame1,text='刪除課程',command=delcrs).grid(column=0,row=12)

#已選課程的時間listbox
Label(frame1,text='時間列表').grid(column=1,row=3,sticky=E)
timelist=Listbox(frame1,height=8,width=13,exportselection=False)
timelist.grid(column=1,row=4,columnspan=2,rowspan=8,sticky=E)


defisrttim():#加入時間
num=1
time=[]
time_in_num=[]
strofod=''
fordayindaylist:
iflen(day.curselection())!=0:
strofod=numtoch[num]
temp=day.curselection()
forclsintemp:
strofod=strofod+''+str(cls+1)
time_in_num.append((num-1)*13+(cls+1))
num+=1
iflen(strofod)!=0:
time.append(strofod)
strofod=[]
iflen(time)!=0:
timelist.insert(END,time)
crslst[courselist.get(courselist.curselection())].append(time)
crslst_in_num[courselist.get(courselist.curselection())].append(time_in_num)
print(crslst)
print(crslst_in_num)
return


defdeltim():#刪除時間
print(timelist.curselection())
forselintimelist.curselection():
crslst[courselist.get(courselist.curselection())].pop(sel)
crslst_in_num[courselist.get(courselist.curselection())].pop(sel)
timelist.delete(timelist.curselection())
print(crslst)
print(crslst_in_num)
return


#加入時間和刪除時間的按鈕
addtime=ttk.Button(frame1,text='加入時間',command=isrttim).grid(column=1,row=2)
delelttime=ttk.Button(frame1,text='刪除時間',command=deltim).grid(column=1,row=12)


defcreatcourselist():#建立課程列文本文檔
withopen('slist.txt','w')asslist:
slist.writelines(str(len(crslst_in_num))+' ')#課程數
forcrs,tlstincrslst_in_num.items():
slist.writelines(crs+' ')#課程名
slist.writelines(str(len(tlst))+' ')#可選時間數
fortimeintlst:
slist.writelines(str(len(time))+' ')#某一可選時間的節數
fornintime:
slist.writelines(str(n)+' ')#第幾節
return


defshowoutput():#在右側的選擇課表combobox里顯示可選課表
filename_list=[]
forfilename_einos.listdir('output'):
filename_list.append(filename_e)
outputlist['values']=filename_list
return


defarrg():#排課
print('here')
creatcourselist()
forfilename_einos.listdir('output'):
os.remove('output/'+filename_e)
arrange.arrangement_mod()
showoutput()
return


#當用戶選擇某一可選課表時,展示課表
defshowcrslist(*args):
print(outputlist.get())
forfilename_einos.listdir('output'):#尋找所選項對應的文件
iffilename_e==outputlist.get():
withopen('output/'+filename_e,'r')asf_r:#打開對應的文件
nr=f_r.readlines()#按行讀取內容
k=0
fordayindaylist:
day.delete(0,END)#刪除課表的原有內容
fornuminrange(1,14):
#print(nr[k])
#加入新的內容
ifnr[k]=='0 ':
day.insert(END,'')
else:
day.insert(END,nr[k].strip())
k+=1
break#完成之後退出循環
return


#可選課表combobox選擇事件
outputlist.bind('<<ComboboxSelected>>',showcrslist)

#排課的按鈕
run=ttk.Button(frame1,text='排課',command=arrg).grid(column=0,row=13,columnspan=2)

#frame_search
frame_s=ttk.Frame(root,borderwidth=5,relief='sunken')
frame_s.grid(column=2,row=0)
ttk.Label(frame_s,text='搜索課程').grid(column=0,row=0,columnspan=2)
ttk.Label(frame_s,text='課程號:').grid(column=0,row=1,sticky=E)

crs_num=StringVar()
crs_search=ttk.Entry(frame_s,textvariable=crs_num,width=12).grid(column=1,row=1,sticky=E)

search_crs=ttk.Button(frame_s,text='搜索').grid(column=0,row=2)
adp=ttk.Button(frame_s,text='加入').grid(column=1,row=2)

ttk.Label(frame_s,text='課程號:').grid(column=0,row=3,columnspan=2,sticky=W)
ttk.Label(frame_s,text='課程名:').grid(column=0,row=4,columnspan=2,sticky=W)
ttk.Label(frame_s,text='上課時間:').grid(column=0,row=5,columnspan=2,sticky=W)

root.mainloop()

㈥ python關於string(字元串)的問題: 刪除重復的字母

你的第三個測試用例有些問題
>>> remove_plicates("The quick brown fox jumps over the lazy dog")
'The quck borwn fx jmps v t lazy dg'
應該是
>>> remove_plicates("The quick brown fox jumps over the lazy dog")
'The quick brown fx jmps v t lazy dg'
==============================================================

#coding: utf-8
import string

def remove_plicates(raw):
"""
Returns a string which is the same as the argument except only the first
occurrence of each letter is present. Upper and lower case letters are
treated as different. Only plicate letters are removed, other characters
such as spaces or numbers are not changed
>>> remove_plicates("Mississippi")
'Misp'
>>> remove_plicates("apple")
'aple'
>>> remove_plicates("The quick brown fox jumps over the lazy dog")
'The quick brown fx jmps v t lazy dg'
>>> remove_plicates("121 balloons 2 u")
'121 balons 2 u'
"""
ret = []
for l in raw:
if l in string.letters and l in ret:
continue
ret.append(l)
return "".join(ret)

if __name__ == '__main__':
import doctest
doctest.testmod()

㈦ python pandas 過濾某列特殊字元求助

漢字也被替換掉了:utf-8
import numpy as np
import pandas as pd
import re
df=pd。
#coding;cidnum'.keyword = df,';)
發現結果把所有的非數字及字母全部替換掉了;;keyword')
df;/, '.str,';'[^a-zA-Z0-9_\]
#將雙引號過濾掉
#將NA的替換掉g
df=df.keyword = df;;path'
df,'name'home/.keyword, '.replace(r;0'file_20150625'.fillna(')
r=u',header=None;cid'.str.lower()
i = 0
df, sep='.keyword,字母意外的所有特殊符號,數字;;u9fa5]'u4e00-\t'想過濾掉除文字。原因是什麼啊.read_csv('.columns=[',具體解決方案如下:

解決方案1:
改成r="\W"試試

解決方案2:
=u'u4e00-\[^a-zA-Z0-9_\u9fa5]'
解決方案3:
你自己先要確定「亂碼」的定義,例如韓文(unicode),如果你裝了韓文字體,那是可以顯示的,不是亂碼,但沒裝的話就是問號一堆,如果是韓文
(euc-kr),在漢字系統那隻是一堆無語義的漢字……可能這個舉例扯遠了,你覺得不會有這種情況,但我只是提醒一下會有意料之外的情況

所以,你需要一個白名單,把你允許的字元unicode范圍都列出來,因為肯定不止漢字,像你列出的文件名中就有空格
另外,沒必要逐個字元檢查,直接用re.sub去除白名單外的字元就可以了

㈧ Python 的\t問題

python-c"print('123456789');print('	A')"#看A的位置就知道了

而且它的長度是可以內定製的:容

#,likethis:
>>>print('Location:'+'10-10-10-10'+' '+'Revision:1').expandtabs(30)
>>>print('District:Tower'+' '+'Date:May16,2012').expandtabs(30)
#Output:
Location:10-10-10-10Revision:1
District:TowerDate:May16,2012

㈨ 使用python讀取字元串的時候出現『\tat』是什麼符號

\後面是轉義字元。
\n表示回車
\t表示表格鍵(鍵盤左側中的Tab鍵)
\tat是由三個字元構成的(Tab符,a字元、t字元)。

㈩ python用了strip沒用 s=open('z.txt') t=read().strip()

f=open('z.txt','r')
forlineinf.readlines():
printline.strip()

閱讀全文

與python過濾特殊t字元串相關的資料

熱點內容
立升飲水機出現e2是什麼原因 瀏覽:874
反滲透高純水是什麼 瀏覽:850
空氣凈化器怎麼會響 瀏覽:859
熱涼凈水器哪個好 瀏覽:442
吉首污水管網清單 瀏覽:797
魚缸低過濾是怎麼做的 瀏覽:943
超濾膜服務商 瀏覽:904
三條腿凈水機濾芯怎麼排序的 瀏覽:288
皮革廢水壓濾機 瀏覽:353
小區自助飲水機怎麼買 瀏覽:823
原裝造紙污水處理設備 瀏覽:889
污水處理含鐵廢水 瀏覽:47
豪爵vf125空氣濾芯如何更換 瀏覽:982
合歡樹樹脂 瀏覽:34
養鴨場污水處理方法 瀏覽:190
手套箱下面裝空調濾芯的塑料叫什麼用 瀏覽:268
工廠污水排放案例分析 瀏覽:446
一天都有多少污水 瀏覽:287
生化純水機什麼情況下要更換濾芯 瀏覽:699
除氯離子交換樹脂 瀏覽:232