Board logo

标题: [文本处理] 批处理如何从多个文本读取变量? [打印本页]

作者: zhiweiloli    时间: 2017-5-21 23:42     标题: 批处理如何从多个文本读取变量?

请教各位大神
使用curl提交信息 需要从4个文本里读取数据
每个文本文件的内容
1.txt
1
2
3

2.txt
a
b
c

3.txt
A
B
C

4.txt



需要的效果
curl -d "1=1&2=a&3=A&4=黑"
curl -d "1=2&2=b&3=B&4=白"
curl -d "1=3&2=c&3=C&4=灰"
作者: ShowCode    时间: 2017-5-22 14:31

test.ps1
  1. $a1 = Get-Content 1.txt
  2. $a2 = Get-Content 2.txt
  3. $a3 = Get-Content 3.txt
  4. $a4 = Get-Content 4.txt
  5. for($i=0; $i -lt $a1.Count; $i++){
  6.     curl -d "1=$a1[$i]&2=$a2[$i]&3=$a3[$i]&4=$a4[$i]"
  7. }
复制代码

作者: zhiweiloli    时间: 2017-5-22 21:07

回复 2# ShowCode


多谢大神 弱弱的问   
这是powershell么?能用批处理做么?
作者: codegay    时间: 2017-5-22 21:54

python3版,模仿楼上的写法。
不用curl
用pip install requests 安装requests。
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2017-05-22 21:38:48
  4. @author: codegay
  5. """
  6. import requests
  7. txt1 = [r.rstrip() for r in open("1.txt").read()]
  8. txt2 = [r.rstrip() for r in open("2.txt").read()]
  9. txt3 = [r.rstrip() for r in open("3.txt").read()]
  10. txt4 = [r.rstrip() for r in open("4.txt").read()]
  11. url = 'http://bathome.net'
  12. data = zip(txt1,txt2,txt3,txt4)
  13. session = requests.sessions()
  14. for t1,t2,t3,t4 in zip(txt1,txt2,txt3,txt4):
  15.     data = {1:t1,2:t2,3:t3,4:t4}
  16.     session.post(url,data=data)
复制代码

作者: zhiweiloli    时间: 2017-5-22 22:23

回复 4# codegay


    谢谢   还是想求个批处理
这个需求是丰富现有的工具  全部推倒重来工程太大。重点是我技术有限只会些简单的批处理。
作者: codegay    时间: 2017-5-22 22:32

回复 5# zhiweiloli


    →_→ 和我没关系。我也不在乎。




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2