vb调用脚本执行文件合并

来源:互联网 发布:属性数据分析引论 编辑:程序博客网 时间:2024/06/01 13:57

看到这个题目你也许会问,都什么年代了还用VB。不得已啊!谁让对方提供的程序是VB的。改写其他的吧,没时间(yongqi),只能用一招损招了,通过vb执行shell调用python写的脚本:
基本要求是这样的:
有若干txt文档,每个文件里只有一列数据,且个数相等,需要将一个文件夹下的所有问价都合并了,生成一个csv:
具体演示如下:
若干txt文档:

abc.txt123456789101112
dec.txt212232425262728292102112122
...

合并的结果csv

    abc dec kkk sdfa0   1   21  121 31211   2   22  212 23122   3   32  321 32313   4   42  421 42134   5   52  521 52135   6   62  621 62136   7   72  721 72137   8   82  821 82138   9   92  921 92139   10  102 10211   10213110  11  112 1121    1121311  12  122 1221    12213

直接上程序,首先是python合并这些txt文档至一个csv

import sysimport os import pandas as pdimport numpy as npdef 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 LongPrivate Sub Form_Load()Dim a As IntegerShell "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
原创粉丝点击