文本中列表套列表转数据帧的问题,求帮忙
此问题,isdkz大师提供的方法,我运行时会出现“ValueError: DataFrame constructor not properly called!” 错误提示,不知道到底是什么原因,我也觉得代码完全没问题,就是不出结果,麻烦大师们再帮忙看看,谢谢,具体如下:
桌面文件:webpage.txt文本内容如下:
var r = [["000001","HXCZHH","华夏成长混合","混合型-灵活","HUAXIACHENGZHANGHUNHE"],["000002","HXCZHH","华夏成长混合(后端)","混合型-灵活","HUAXIACHENGZHANGHUNHE"],["000003","ZHKZZZQA","中海可转债债券A","债券型-可转债","ZHONGHAIKEZHUANZHAIZHAIQUANA"],["980003","TPYLGYGDCYZ","太平洋六个月滚动持有债","债券型-长债","TAIPINGYANGLIUGEYUEGUNDONGCHIYOUZHAI"]];
我想得到这个数据帧:
代码 名称首字拼音 名称 类型 其它
"000001" HXCZHH" "华夏成长混合" "混合型-灵活" "HUAXIACHENGZHANGHUNHE"
"000002""HXCZHH" "华夏成长混合(后端)" "混合型-灵活" "HUAXIACHENGZHANGHUNHE"
"000003"ZHKZZZQA" "中海可转债债券A" "债券型-可转债" "ZHONGHAIKEZHUANZHAIZHAIQUANA"
"980003""TPYLGYGDCYZ" "太平洋六个月滚动持有债" "债券型-长债" "TAIPINGYANGLIUGEYUEGUNDONGCHIYOUZHAI"
根据isdkz大师提供的方法(最下面是代码),我的会出现“ValueError: DataFrame constructor not properly called!” 错误提示
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, columns=["代码" ,"名称首字拼音","名称","类型", "其它"])
print(df) ok等会啊 import re
import pandas as pd
with open(r'C:\Users\OYF\Desktop\ppp\webpage.txt', "r") as f:
response= f.read()
data_list = re.findall(r'\[(.*)\]', response)
df = pd.DataFrame(eval(data_list), columns=["代码" ,"名称首字拼音","名称","类型", "其它"])
print(df)
页:
[1]