python - 将数据移动到制表符分隔的文件python

标签 python

我需要将这三个数据列表移动到制表符分隔的文本文件中。我只知道如何连续打印它们(正如我在下面所做的那样)。

import requests
import datetime as dt
import xmltodict
url='http://forecast.weather.gov/MapClick.php?lat=47.6062&lon=-122.3321&FcstType=digitalDWML'
r = requests.get(url)

with open('KBFI_2.txt','wb') as outfile:
    result = xmltodict.parse(r.text)
    pop = result['dwml']['data']['parameters']['probability-of-precipitation']['value']
    pop = '\n'.join(pop)
    hqpf = result['dwml']['data']['parameters']['hourly-qpf']['value']
    hqpf = '\n'.join(hqpf)
    d = result['dwml']['data']['time-layout']['start-valid-time']
    for dte in d:
        date = dt.datetime.strptime(dte.rsplit("-",1)[0],"%Y-%m-%dT%H:%M:%S")
        print >>outfile, date
    print >>outfile, pop
    print >>outfile, hqpf
    outfile.close()

我确信这是一个非常简单的修复,但我是新手,所以我迷路了。感谢所有帮助!

最佳答案

您似乎知道如何将事物组合成一个字符串中的多行('\n'.join);要使行以制表符分隔,请对每行使用 '\t'.join

另一部分是获取 3 个列表并将它们转换为三元组列表,以便您可以使用 join 将每个三元组变成制表符分隔的行。像这样的事情:

for a,b,c in zip(list1, list2, list3):
    print >>outfile, "\t".join([a,b,c])

最后,您可以使用以下方法制作日期列表:

[dt.datetime.strptime(x.rsplit("-",1)[0],"%Y-%m-%dT%H:%M:%S") for x in dte]

关于python - 将数据移动到制表符分隔的文件python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31794576/

相关文章:

python - 为什么python会在大指数结果的末尾添加一个 'L'?

python - FileNotFoundError : Could not find module when using jupyter notebook

python - 带菜单栏的 wx.Panel

python - 除了 KeyboardInterrupt : is outside the class 之外,如何使用 sock.close() 正常关闭套接字

python - (使用 Pandas 解决 SQL 问题)从给定的 pandas 数据帧中,找到请求的总体接受率,四舍五入到小数点后两位

python - 如何将 pandas 中的列索引转换为行索引?

python - python的pp模块上的PicklingError

python - 值错误 : non-hexadecimal number found in fromhex() arg at position

python - SQLAlchemy - 使用不同的过滤器添加 OR 条件

python - 斯坦福西类牙语 POS Tagger 标签的含义