|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
先谢谢大侠,下面是我的需求:
webpage.txt文本内容如下:
var r = [["000001","HXCZHH","华夏成长混合","混合型-灵活","HUAXIACHENGZHANGHUNHE"],["000002","HXCZHH","华夏成长混合(后端)","混合型-灵活","HUAXIACHENGZHANGHUNHE"],["000003","ZHKZZZQA","中海可转债债券A","债券型-可转债","ZHONGHAIKEZHUANZHAIZHAIQUANA"],["980003","TPYLGYGDCYZ","太平洋六个月滚动持有债","债券型-长债","TAIPINGYANGLIUGEYUEGUNDONGCHIYOUZHAI"]];
我想用下面方法提取列表:
with open(r'C:\Users\ppp\Desktop\webpage.txt', "r") as f:
#print(type(f))
response = f.read()
data_list = re.findall(r'\[(.*?)\]',response)
上面这个方法提不出,要怎么才能从桌面文本webpage.txt中 ,提取出列表data_list:
data_list = [["000001","HXCZHH","华夏成长混合","混合型-灵活","HUAXIACHENGZHANGHUNHE"],["000002","HXCZHH","华夏成长混合(后端)","混合型-灵活","HUAXIACHENGZHANGHUNHE"],["000003","ZHKZZZQA","中海可转债债券A","债券型-可转债","ZHONGHAIKEZHUANZHAIZHAIQUANA"],["980003","TPYLGYGDCYZ","太平洋六个月滚动持有债","债券型-长债","TAIPINGYANGLIUGEYUEGUNDONGCHIYOUZHAI"]]
df = pd.dataframe(data_list,columns=["代码" ,"名称首字拼音","名称","类型", "其它"])
我最终要的结果是下面这个样子:
代码 名称首字拼音 名称 类型 其它
"000001" HXCZHH" "华夏成长混合" "混合型-灵活" "HUAXIACHENGZHANGHUNHE"
"000002" "HXCZHH" "华夏成长混合(后端)" "混合型-灵活" "HUAXIACHENGZHANGHUNHE"
"000003" ZHKZZZQA" "中海可转债债券A" "债券型-可转债" "ZHONGHAIKEZHUANZHAIZHAIQUANA"
"980003" "TPYLGYGDCYZ" "太平洋六个月滚动持有债" "债券型-长债" "TAIPINGYANGLIUGEYUEGUNDONGCHIYOUZHAI"
import re
import pandas as pd
with open(r'C:\Users\ppp\Desktop\webpage.txt', "r") as f:
#print(type(f))
response = f.read()
data_list = re.findall(r'\[(?:.*)\]', response)
df = pd.DataFrame(data_list[0], columns=["代码" ,"名称首字拼音","名称","类型", "其它"])
|
|