看到这个题目你也许会问,都什么年代了还用VB。不得已啊!谁让对方提供的程序是VB的。改写其他的吧,没时间(yongqi),只能用一招损招了,通过vb执行shell调用python写的脚本:
基本要求是这样的:
有若干txt文档,每个文件里只有一列数据,且个数相等,需要将一个文件夹下的所有问价都合并了,生成一个csv:
具体演示如下:
若干txt文档:
abc.txt
1
2
3
4
5
6
7
8
9
10
11
12
dec.txt
21
22
32
42
52
62
72
82
92
102
112
122
...
合并的结果csv
abc dec kkk sdfa
0 1 21 121 3121
1 2 22 212 2312
2 3 32 321 3231
3 4 42 421 4213
4 5 52 521 5213
5 6 62 621 6213
6 7 72 721 7213
7 8 82 821 8213
8 9 92 921 9213
9 10 102 10211 102131
10 11 112 1121 11213
11 12 122 1221 12213
直接上程序,首先是python合并这些txt文档至一个csv
import sys
import os
import pandas as pd
import numpy as np
def main(root_path , save_path):
filenames = os.listdir(root_path)
pdlist = []
for fn in filenames:
abs_path = os.path.join(root_path , fn)
if os.path.isdir(abs_path):
pass
else:
pdlist.append(pd.DataFrame(np.asarray(pd.read_table(abs_path , header = None)),columns = [fn.split('.')[0]]))
fin_df = pdlist[0]
for i in range(1,len(pdlist)):
fin_df = fin_df.join(pdlist[i],how = "right")
fin_df.to_csv(save_path)
if __name__ == "__main__":
root_path = sys.argv[1]
save_path = sys.argv[2]
#root_path = r"C:\Users\APAC\Desktop\zili\testpythonfile"
#save_path = r"d:\abc.csv"
main(root_path , save_path)
VB的话通过shell调用python程序
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Sub Form_Load()
Dim a As Integer
Shell "cmd.exe /k D:\software\anaconda\python.exe D:\hemin_workspace\python_code\readfileByfile.py C:\Users\APAC\Desktop\zili\testpythonfile d:\abc.csv"
'a = ShellExecute(Handle, "open", "D:\software\anaconda\python.exe", "D:\hemin_workspace\python_code\readfileByfile.py ", "", 0)
'a = ShellExecute(Handle, "open", "D:\QQ\Bin\QQScLauncher.exe", "", "", 0)
End Sub