-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatchForWeibo.lua
170 lines (154 loc) · 4 KB
/
batchForWeibo.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
local tUser = {
csvFile = "ANiu\\csvInput.csv";
--luaFile = "csvtest.lua";
--tableTestFile1 = "tabletest1.lua";
--tableTestFile2 = "tabletest2.lua";
--csvTestFile = "tabletest.csv";
csvOutputFile = "Outputs\\ANiuOutput.csv";
};
dofile( "table.save-1.0.lua" );
-- include module Csv
local mCsv = require "csv2table"
-- do
-- table.getn = function (t)
-- if t.n then
-- return t.n
-- else
-- local n = 0
-- for i in pairs(t) do
-- if type(i) == "number" then
-- n = math.max(n, i)
-- end
-- end
-- return n
-- end
-- end
-- end
-- function csv2table()
-- local t = {}
-- t = mCsv.load(tUser.csvFile);
-- table.save( t, tUser.luaFile );
-- end
-- function saveCsvTest()
-- local t = {}
-- t = table.load(tUser.luaFile)
-- mCsv.save( t, tUser.csvTestFile )
-- end
--[[ #地铁客流排行榜# 3月31日(周四),北京地铁路网客运量1168.62万,上海地铁客运量1019.8万,广州地铁客运量为720.8万, 深圳地铁路网客运量346.68万,南京地铁客运量为231.6万,武汉地铁客运量206.36万,成都地铁客运量139.33万。,
2016-04-01 12:39:00 来自 微博 weibo.com]]
-- row one have date,week,city,ridership,
-- row two have date time of sent
function parserANiuLine(lineTbl)
local tCity = {
BJ = "北京",
SH = "上海",
GZ = "广州",
SZ = "深圳",
NJ = "南京",
WH = "武汉",
CQ = "重庆",
CD = "成都",
ZZ = "郑州",
CS = "长沙",
SZ1 = "苏州",
XA = "西安",
}
local sParser = {
TopicKW = "地铁客流排行榜",
DateInput = "(%d+)月(%d+)日",
WeekInput = "((周.-))",
WeekInput2 = "((.-))",
Ridership = "%%city.-(%d+%.?%d*)万",
RidershipSZ = "深圳.-(%d+%.?%d*)万",
DateSent = "(%d%d%d%d%-%d%d%-%d%d)",
}
local sData = {
-- DateInput = {
-- month = {},
-- day = {},
-- },
--WeekInput = {},
--Riderships = {},
-- DateSent = {
-- year = {},
-- month = {},
-- day = {},
-- }
}
local str = lineTbl[1]
local isValid
-- find out if we have TopicKW or not first!
isValid = string.find(str, sParser.TopicKW)
if isValid == nil then
return nil
end
-- sData.DateInput.month, sData.DateInput.day = string.match(str, sParser.DateInput)
sData.InputMonth, sData.InputDay = string.match(str, sParser.DateInput)
sData.InputWeek = string.match(str, sParser.WeekInput) or string.match(str, sParser.WeekInput2)
for k, v in pairs(tCity) do
sData[v] = string.match(str, string.gsub(sParser.Ridership, "%%city", v ))
end
str = lineTbl[2]
-- sData.DateSent.year, sData.DateSent.month, sData.DateSent.day = string.match(str, sParser.DateSent)
sData.DateSent = string.match(str, sParser.DateSent)
sData.RawMsg = lineTbl[1]
return sData
end
function kvTbl2ivTbl(Tbl)
local ret = {}
local header = {}
local headerT = {}
for index, subTbl in ipairs(Tbl) do
if type(subTbl) == "table" then
local subTableRet = {}
for name, value in pairs(subTbl) do
if not headerT[name] then
table.insert(header, name)
headerT[name] = #header
end
local num = headerT[name]
subTableRet[num] = value
end
table.insert(ret, subTableRet)
else
table.insert(ret, subTbl)
end
end
for index, subTbl in ipairs(ret) do
if type(subTbl) == "table" then
--if (#subTbl ~= #header) then
for subIndex, value in pairs(header) do
if not subTbl[subIndex] then subTbl[subIndex] = " " end
end
--end
end
end
table.insert(ret, 1, header)
return ret
end
function parserANiuTbl(rawTbl)
local fullTbl = {}
local lineData = {}
for k, lineTbl in ipairs(rawTbl) do
lineData = parserANiuLine(lineTbl)
if lineData ~= nil then
table.insert(fullTbl, lineData)
end
end
return fullTbl
end
function parserTableForRidership()
local t = {}
local s = {}
-- t = table.load(tUser.luaFile)
-- from 阿牛
t = mCsv.load(tUser.csvFile);
s = parserANiuTbl(t)
--table.save( s, tUser.tableTestFile1 );
local ss = kvTbl2ivTbl(s)
--table.save( ss, tUser.tableTestFile2 );
mCsv.save( ss, tUser.csvOutputFile )
end
--csv2table();
--saveCsvTest()
parserTableForRidership();