python去除字符串外的引号

目录

for line in f_write:
	line.strip('\n').split().decode(encoding_type)  

TypeError: a bytes-like object is required, not ‘str’
line.strip().split(‘,‘)含义:

strip() 用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
split(‘ ’): 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串。

line.strip().decode(encoding_type)
Out[23]: '"2022-08-29 13:54:38",49533130,4.33,-4.62,-1.1,22.52,0.04,0.02,0.05,12.07'

一个引号将多个元素变成了字符串

str = "00000003210Runoob01230000000"; 
print str.strip( '0' );  # 去除首尾字符 0
 
 
str2 = "   Runoob      ";   # 去除首尾空格
print str2.strip();

输出:

3210Runoob0123
Runoob

eval 使用遥感影像头文件时,需要获得里面的日期和时间信息,得到的字符串两端带有双引号,可以使用eval()函数去除。

line
Out[9]: '"2022-08-29 13:54:37.4",49533124,4.32,-4.47,-1.15,22.48,0.02,0.02,0.02,12.07'
eval(line)
Out[10]: 
('2022-08-29 13:54:37.4', 49533124, 4.32, -4.47, -1.15, 22.48, 0.02, 0.02, 0.02, 12.07)
     for line in tqdm(f_write):
         encoding_type = chardet.detect(line)['encoding']

         if encoding_type == 'utf-8':
             line = eval(line.strip().decode(encoding_type))
             # line = line.split(',')
             # ans.append(line)
             # print('utf8', len(line), line)
         elif encoding_type == 'ascii':
             line = eval(line.strip().decode(encoding_type))
             # line = line.split(',')
             # ans.append(line)
             # print('ascii', len(line), line)
         else:
             continue
         if len(line) == 10:
             ans.append(line)
         else:
             print(len(line), line)
 f_write.close()
 # ans_df = pd.DataFrame(ans[3:], columns=ans[0])

猜你喜欢

转载自blog.csdn.net/weixin_46713695/article/details/129749013
今日推荐