我是Python的新手,我正在尝试从user.csv文件中的每个用户的URL获取响应,返回JSON消息,然后将其解析为CSV。我不确定在返回部分放什么或如何拆分消息。这是我收到HTTP请求时返回给我的消息示例:
{" msg":{" mes":" four"," high":1230," low&#34 ;:0}}"
这是我到目前为止所得到的但是我收到了一个错误:
TypeError:期望的字符串或缓冲区
@echo off
::This tells NirCmd to wait for 5 seconds, and then proceed to the next command.
nircmd wait 5000
echo This is one line
nircmd wait 5000
echo This is another line
nircmd wait 5000
echo Oh, hey! Another line!
nircmd wait 5000
echo Take a guess what this is... Another line!
...
答案 0 :(得分:1)
根据您提供的信息,您可以执行以下操作:
import requests
import csv
url = 'http://test_url'
with open('user.csv', 'rU') as data_file:
data = csv.DictReader(data_file)
for row in data:
current_user = row['mdn']
r = requests.get(url) # probably you also need to send current_user somehow. "r = requests.get(url+'?user='+current_user)" maybe?
string_json = r.text.encode('utf-8')
json = eval(string_json) # only use this command if you are sure that string_json only contains a valid json and no malicious code instead
with open('outputfile.csv','a') as outfile:
outfile.write(current_user+';'+json["msg"]["mes"]+';'+json["msg"]["high"]+';'+json["msg"]["low"]+'\n')
请提供有关HTTP请求使用情况和csv格式的更多信息,以获得更准确的答案