Coding-Page 查詢和上傳下載文件Class

1、各語種的Codepage查詢

Language Keys---語種存放表(T002)
31838ecab81a60d1474df22218dd1f6b.jpg

我們可能常用到的:
LaisoSptxtcodepage
ZHChinese8404
ZFChinese trad8300
ENEnglish1160
Codepage的查詢:

  1. REPORT zrab_zyb_test MESSAGE-ID sabapdocu.   

  2. *CONSTANTS: tcode(8) TYPE c VALUE 'YPPM1021'.   
  3. DATA:  l_codepage TYPE cpcodepage,   
  4.        l_flag TYPE c.   

  5. CALL FUNCTION 'NLS_GET_FRONTEND_CP'
  6.   EXPORTING   
  7.     langu                 = 'M'  "注意,此處使用SPRAS類型,1 Char   
  8.     fetype                = 'MS'
  9.   IMPORTING   
  10.     frontend_codepage     = l_codepage   
  11.   EXCEPTIONS   
  12.     illegal_syst_codepage = 1
  13.     no_frontend_cp_found  = 2
  14.     internal_or_db_error  = 3
  15.     OTHERS                = 4.   
  16. *IF sy-subrc <> 0.   
  17. *  CASE sy-subrc.   
  18. *    WHEN 1.   
  19. *      MESSAGE i055.   
  20. *    WHEN 2.   
  21. *   ***   
  22. *  ENDCASE.   
  23. *  LEAVE TO TRANSACTION tcode.   
  24. *ENDIF.   

  25. WRITE: / l_codepage.  

REPORT zrab_zyb_test MESSAGE-ID sabapdocu.*CONSTANTS: tcode(8) TYPE c VALUE 'YPPM1021'.DATA:  l_codepage TYPE cpcodepage,       l_flag TYPE c.CALL FUNCTION 'NLS_GET_FRONTEND_CP'  EXPORTING    langu                 = 'M'  "注意,此處使用SPRAS類型,1 Char    fetype                = 'MS'  IMPORTING    frontend_codepage     = l_codepage  EXCEPTIONS    illegal_syst_codepage = 1    no_frontend_cp_found  = 2    internal_or_db_error  = 3    OTHERS                = 4.*IF sy-subrc <> 0.*  CASE sy-subrc.*    WHEN 1.*      MESSAGE i055.*    WHEN 2.*   ****  ENDCASE.*  LEAVE TO TRANSACTION tcode.*ENDIF.WRITE: / l_codepage.


2、Upload/Download
upload文件存在與否的判斷

  1. form check_file_exist .   

  2.   data: l_result type abap_bool,   
  3.         l_file type string.   

  4.   l_file = p_upath.   
  5.   call method cl_gui_frontend_services=>file_exist   
  6.     exporting   
  7.       file                 = l_file   
  8.     receiving   
  9.       result               = l_result   
  10.     exceptions   
  11.       cntl_error           = 1
  12.       error_no_gui         = 2
  13.       wrong_parameter      = 3
  14.       not_supported_by_gui = 4
  15.       others               = 5.   

  16. if sy-subrc <> 0.   
  17. *    CASE sy-subrc.   
  18. *      WHEN 1.   
  19. *        MESSAGE i039.   
  20. *      WHEN 2.   
  21. *        MESSAGE i040.   
  22. *      WHen OTHERS.   
  23. *        MESSAGE i041.   
  24. *    ENDCASE.   
  25. *    LEAVE TO TRANSACTION tcode.   
  26.   endif.   

  27. if l_result <> 'X'.   
  28.     message e888 with 'Invalid file'.   
  29.   endif.   

  30. endform.                    " check_file_exist  

form check_file_exist .  data: l_result type abap_bool,        l_file type string.  l_file = p_upath.  call method cl_gui_frontend_services=>file_exist    exporting      file                 = l_file    receiving      result               = l_result    exceptions      cntl_error           = 1      error_no_gui         = 2      wrong_parameter      = 3      not_supported_by_gui = 4      others               = 5.  if sy-subrc <> 0.*    CASE sy-subrc.*      WHEN 1.*        MESSAGE i039.*      WHEN 2.*        MESSAGE i040.*      WHen OTHERS.*        MESSAGE i041.*    ENDCASE.*    LEAVE TO TRANSACTION tcode.  endif.  if l_result <> 'X'.    message e888 with 'Invalid file'.  endif.endform.                    " check_file_exist


download文件格式及保存路徑存在與否的判斷

  1. form check_file_path .   
  2.   data: l_result type abap_bool,   
  3.         l_directory type string,   
  4.         l_filename type string,   
  5.         l_index type i.   
  6.   data: begin of lt_tab occurs 0,   
  7.           cont(100) type c,   
  8.         end of lt_tab.   


  9. if not p_spath cs '.txt'.   
  10.     message e888 with 'Invalid file format.'.   
  11.   elseif not p_spath cs ':\'.  
  12.     message e888 with 'Invalid driver.'.  
  13.   endif.  
  14. *** 通過split分割,concatenate組合,實例存儲路徑、文件名的分離  
  15.   split p_spath at '\' into table lt_tab.  
  16.   describe table lt_tab lines l_index.  
  17.   read table lt_tab index l_index.  
  18.   l_filename = lt_tab-cont.  
  19.   delete lt_tab index l_index.  
  20.   loop at lt_tab.  
  21.     concatenate l_directory lt_tab-cont '\' into l_directory.  
  22.     clear lt_tab.  
  23.   endloop.  

  24.   call method cl_gui_frontend_services=>directory_exist  
  25.     exporting  
  26.       directory            = l_directory  
  27.     receiving  
  28.       result               = l_result  
  29.     exceptions  
  30.       cntl_error           = 1  
  31.       error_no_gui         = 2  
  32.       wrong_parameter      = 3  
  33.       not_supported_by_gui = 4  
  34.       others               = 5.  
  35.   if sy-subrc <> 0.  
  36. *    CASE sy-subrc.  
  37. *      WHEN 1.  
  38. *        MESSAGE i039.  
  39. *      WHEN 2.  
  40. *        MESSAGE i040.  
  41. *      WHen OTHERS.  
  42. *        MESSAGE i041.  
  43. *    ENDCASE.  
  44. *    LEAVE TO TRANSACTION tcode.  
  45.   endif.  
  46.   if l_result <> 'X'.  
  47.     message e888 with 'Invalid save path.'.   
  48.   endif.   

  49. endform.                    " check_file_path   

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21122155/viewspace-578423/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/21122155/viewspace-578423/

帮我将以下代码写注释# coding=gbk # -- coding:uft-8 -- # 贝壳网小区 import requests from lxml import etree from time import sleep import hashlib from urllib import parse import pandas as pd def getPosi(tar): try: ak = 'C8rQZy1askzzMtdY3ChAZUer1P0PRjI0' sk = 'shShi1VLCkH1gGR4v75d2LTnrn2Vm5Mg' add = f'/geocoding/v3/?address={tar}&output=json&ak={ak}&city=大连市' add = parse.quote(add, safe="/:=&?#+!$,;'@()*[]") sn = hashlib.md5(parse.quote_plus(add + sk).encode('utf-8')).hexdigest() url = f'https://api.map.baidu.com{add}&sn={sn}' dic = requests.get(url).json() lat = dic['result']['location']['lat'] lng = dic['result']['location']['lng'] return lat, lng except: return None, None def collect(): items = { 'ganjingzi': 22, 'zhongshan': 19, 'shahekou': 14, 'xigang': 12 } resLs = [] for key in items: for page in range(items[key]): page += 1 url = f'https://dl.ke.com/xiaoqu/{key}/pg{page}/' headers = { 'User-Agent': ua, 'Referer': url } while True: try: res = requests.get(url=url, headers=headers, timeout=(5, 5)).content.decode('utf-8') break except: print('again') tree = etree.HTML(res) for li in tree.xpath('//ul[@class="listContent"]/li'): href = li.xpath('./a/@href')[0] while True: try: res = requests.get(url=href, headers=headers, timeout=(5, 5)).content.decode('utf-8') break except: print('again') tree = etree.HTML(res) dic = { 'href': href, 'key': key, 'name': tree.xpath('//h1/@title')[0], 'price': (tree.xpath('//span[@class="xiaoquUnitPrice"]/text()') + [''])[0], 'property': tree.xpath('//span[@class="xiaoquInfoContent"]/text()')[1].strip(), 'building': tree.xpath('//span[@class="xiaoquInfoContent"]/text()')[4].strip(), 'house': tree.xpath('//span[@class="xiaoquInfoContent"]/text()')[5].strip() } dic['lat'], dic['lng'] = getPosi(dic['name']) print(dic) resLs.append(dic) sleep(3) df = pd.DataFrame(resLs) df.to_excel('贝壳网小区.xlsx', encoding='utf-8', index=False) if name == 'main': ua = 'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/91.0.4472.106Safari/537.36' collect()
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值