将元组列表写入txt文件 [英] Write list of tuples to txt file

查看:122
本文介绍了将元组列表写入txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下元组列表:

list=[((0.0, 0.0), (0.00249999994412065, -509.707885742188), (0.00499999988824129, -1017.52648925781), (0.0087500000372529, -1778.51281738281), (0.0143750002607703, -2918.21899414063), (0.0228125005960464, -4609.91650390625))]

我想以以下格式将信息写入txt文件:

I'd like to write the information to a txt file in this format:

0.0 0.0
0.00249999994412065 -509.707885742188
.... 

我一直在使用以下代码:

I've been using this code:

with open(fname, 'w') as graphd:
    for row in list:
        print >>graphd, ', '.join(map(str, row))
graphd.close()

其中: fname是文件的路径 list是元组列表

where: fname is the path to the file list is the list of tuples

这将导致:(接近,但仍然不是我想要的...)

This results in: (which is close but still not what I want...)

(0.0, 0.0), (0.00249999994412065, -509.707885742188), (0.00499999988824129, -1017.52648925781), (0.0087500000372529, -1778.51281738281), (0.0143750002607703, -2918.21899414063), (0.0228125005960464, -4609.91650390625), (0.0328124985098839, -6560.962890625), (0.0428125001490116, -8467.638671875), (0.0528125017881393, -10321.19140625), (0.0628124997019768, -12137.498046875), (0.0728124976158142, -13877.9580078125), (0.0828125029802322, -15571.837890625), (0.0928125008940697, -17186.35546875), (0.102812498807907, -18728.310546875), (0.112812496721745, -20191.1640625), (0.122812502086163, -21548.513671875), (0.1328125, -22796.673828125), (0.142812505364418, -23935.923828125), (0.152812495827675, -24970.046875), (0.162812501192093, -25903.265625), (0.172812506556511, -26744.365234375), (0.182812497019768, -27502.3125), (0.192812502384186, -28186.765625), (0.202812492847443, -28805.953125), (0.212812498211861, -29367.408203125), (0.222812503576279, -29877.845703125), (0.232812494039536, -30343.181640625), (0.242812499403954, -30768.73046875), (0.252812504768372, -31159.8515625), (0.262812495231628, -31519.955078125), (0.272812485694885, -31852.59765625), (0.282812505960464, -32160.71875), (0.292812496423721, -32446.474609375), (0.302812486886978, -32712.138671875), (0.312812507152557, -32959.703125), (0.322812497615814, -33190.91015625), (0.332812488079071, -33407.29296875), (0.34281250834465, -33610.2109375), (0.352812498807907, -33800.859375), (0.362812489271164, -33980.30078125), (0.372812509536743, -34149.484375), (0.3828125, -34309.25390625), (0.392812490463257, -34460.3671875), (0.402812510728836, -34603.5), (0.412812501192093, -34739.26171875), (0.42281249165535, -34868.20703125), (0.432812511920929, -34990.828125), (0.442812502384186, -35107.5703125), (0.452812492847443, -35218.8515625), (0.462812513113022, -35325.04296875), (0.472812503576279, -35426.48828125), (0.482812494039536, -35523.48828125), (0.492812514305115, -35616.33203125), (0.502812504768372, -35705.28515625), (0.512812495231628, -35790.578125), (0.522812485694885, -35872.4375), (0.532812476158142, -35951.0625), (0.542812526226044, -36026.640625), (0.552812516689301, -36099.3515625), (0.562812507152557, -36169.34765625), (0.572812497615814, -36236.77734375), (0.582812488079071, -36301.78515625), (0.592812478542328, -36364.49609375), (0.602812528610229, -36425.02734375), (0.612812519073486, -36483.4921875), (0.622812509536743, -36539.9921875), (0.6328125, -36594.62890625), (0.642812490463257, -36647.4921875), (0.652812480926514, -36698.6640625), (0.662812471389771, -36748.22265625), (0.672812521457672, -36796.25), (0.682812511920929, -36842.8125), (0.692812502384186, -36887.97265625), (0.702812492847443, -36931.796875), (0.712812483310699, -36974.33984375), (0.722812473773956, -37015.66015625), (0.732812523841858, -37055.8125), (0.742812514305115, -37094.83984375), (0.752812504768372, -37132.7890625), (0.762812495231628, -37169.70703125), (0.772812485694885, -37205.6328125), (0.782812476158142, -37240.609375), (0.792812526226044, -37274.671875), (0.802812516689301, -37307.85546875), (0.812812507152557, -37340.19140625), (0.822812497615814, -37371.71875), (0.832812488079071, -37402.45703125), (0.842812478542328, -37432.4453125), (0.852812528610229, -37461.703125), (0.862812519073486, -37490.26171875), (0.872812509536743, -37518.14453125), (0.8828125, -37545.375), (0.892812490463257, -37571.98046875), (0.902812480926514, -37597.97265625), (0.912812471389771, -37623.37890625), (0.922812521457672, -37648.21875), (0.932812511920929, -37672.51171875), (0.942812502384186, -37696.26953125), (0.952812492847443, -37719.515625), (0.962812483310699, -37742.26171875), (0.972812473773956, -37764.53125), (0.982812523841858, -37786.33203125), (0.992812514305115, -37807.6796875), (1.00281250476837, -37828.58984375), (1.01281249523163, -37849.078125), (1.02281248569489, -37869.15625), (1.03281247615814, -37888.83203125), (1.0428124666214, -37908.12109375), (1.05281245708466, -37927.03515625), (1.06281244754791, -37945.58203125), (1.07281255722046, -37963.7734375), (1.08281254768372, -37981.62109375), (1.09281253814697, -37999.1328125), (1.10281252861023, -38016.3203125), (1.11281251907349, -38033.1875), (1.12281250953674, -38049.75), (1.1328125, -38081.30859375), (1.14281249046326, -38117.46484375), (1.15281248092651, -38152.9765625), (1.16281247138977, -38205.91015625), (1.17281246185303, -38262.1171875), (1.18281245231628, -38317.31640625), (1.19281244277954, -38371.5546875), (1.20281255245209, -38438.77734375), (1.21281254291534, -38511.60546875), (1.2228125333786, -38595.37109375), (1.23281252384186, -38688.0703125), (1.24281251430511, -38779.234375), (1.25281250476837, -38868.90625), (1.26281249523163, -38957.11328125), (1.27281248569489, -39043.90234375), (1.28281247615814, -39129.30078125), (1.2928124666214, -39213.34375), (1.30281245708466, -39296.0625), (1.31281244754791, -39377.48828125), (1.32281255722046, -39457.6484375), (1.33281254768372, -39536.578125), (1.34281253814697, -39614.30078125), (1.35281252861023, -39690.84375), (1.36281251907349, -39766.23828125), (1.37281250953674, -39840.5078125), (1.3828125, -39913.67578125), (1.39281249046326, -39985.765625), (1.40281248092651, -40056.8046875), (1.41281247138977, -40126.8125), (1.42281246185303, -40195.81640625), (1.43281245231628, -40263.828125), (1.44281244277954, -40330.87890625), (1.45281255245209, -40396.98046875), (1.46281254291534, -40462.16015625), (1.4728125333786, -40526.43359375), (1.48281252384186, -40589.8203125), (1.49281251430511, -40652.3359375), (1.50281250476837, -40714.00390625), (1.51281249523163, -40774.83203125), (1.52281248569489, -40835.77734375), (1.53281247615814, -40903.80078125), (1.5428124666214, -40970.921875), (1.55281245708466, -41037.16015625), (1.56281244754791, -41104.97265625), (1.57281255722046, -41179.66796875), (1.58281254768372, -41268.05859375), (1.59281253814697, -41356.78515625), (1.60281252861023, -41444.3828125), (1.61281251907349, -41530.87890625), (1.62281250953674, -41623.875), (1.6328125, -41731.9765625), (1.64281249046326, -41841.390625), (1.65281248092651, -41949.47265625), (1.66281247138977, -42056.23828125), (1.67281246185303, -42161.71875), (1.68281245231628, -42265.93359375), (1.69281244277954, -42368.90234375), (1.70281255245209, -42473.17578125), (1.71281254291534, -42581.99609375), (1.7228125333786, -42689.55078125), (1.73281252384186, -42800.32421875), (1.74281251430511, -42912.33984375), (1.75281250476837, -43023.0546875), (1.76281249523163, -43132.5), (1.77281248569489, -43242.2890625), (1.78281247615814, -43356.40625), (1.7928124666214, -43469.234375), (1.80281245708466, -43580.78515625), (1.81281244754791, -43691.0625), (1.82281255722046, -43800.109375), (1.83281254768372, -43907.953125), (1.84281253814697, -44014.60546875), (1.85281252861023, -44120.09375), (1.86281251907349, -44224.4296875), (1.87281250953674, -44327.640625), (1.8828125, -44432.32421875), (1.89281249046326, -44538.984375), (1.90281248092651, -44646.5625), (1.90531253814697, -44674.09375), (1.90625, -44689.54296875), (1.90660154819489, -44698.22265625), (1.90673339366913, -44703.1015625), (1.90674579143524, -44703.35546875), (1.90676426887512, -44703.7421875), (1.90679216384888, -44704.3203125), (1.90683376789093, -44705.1875), (1.90689635276794, -44706.76171875)

推荐答案

问题是您要遍历列表,但是列表中仅包含一项,即一个元组的元组.因此,您必须遍历该元组的元组,即lis[0]

Problem is that you're iterating over the list but the list contains only one item, that is a tuple of tuples. So you must iterate over that tuple of tuples i.e lis[0]

lis=[((0.0, 0.0), (0.00249999994412065, -509.707885742188), (0.00499999988824129, -1017.52648925781), (0.0087500000372529, -1778.51281738281), (0.0143750002607703, -2918.21899414063), (0.0228125005960464, -4609.91650390625), (0.0328124985098839, -6560.962890625), (0.0428125001490116, -8467.638671875), (0.0528125017881393, -10321.19140625), (0.0628124997019768, -12137.498046875), (0.0728124976158142, -13877.9580078125), (0.0828125029802322, -15571.837890625), (0.0928125008940697, -17186.35546875), (0.102812498807907, -18728.310546875), (0.112812496721745, -20191.1640625), (0.122812502086163, -21548.513671875), (0.1328125, -22796.673828125), (0.142812505364418, -23935.923828125), (0.152812495827675, -24970.046875), (0.162812501192093, -25903.265625), (0.172812506556511, -26744.365234375), (0.182812497019768, -27502.3125), (0.192812502384186, -28186.765625), (0.202812492847443, -28805.953125), (0.212812498211861, -29367.408203125), (0.222812503576279, -29877.845703125), (0.232812494039536, -30343.181640625), (0.242812499403954, -30768.73046875), (0.252812504768372, -31159.8515625), (0.262812495231628, -31519.955078125), (0.272812485694885, -31852.59765625), (0.282812505960464, -32160.71875), (0.292812496423721, -32446.474609375), (0.302812486886978, -32712.138671875), (0.312812507152557, -32959.703125), (0.322812497615814, -33190.91015625), (0.332812488079071, -33407.29296875), (0.34281250834465, -33610.2109375), (0.352812498807907, -33800.859375), (0.362812489271164, -33980.30078125), (0.372812509536743, -34149.484375), (0.3828125, -34309.25390625), (0.392812490463257, -34460.3671875), (0.402812510728836, -34603.5), (0.412812501192093, -34739.26171875), (0.42281249165535, -34868.20703125), (0.432812511920929, -34990.828125), (0.442812502384186, -35107.5703125), (0.452812492847443, -35218.8515625), (0.462812513113022, -35325.04296875), (0.472812503576279, -35426.48828125), (0.482812494039536, -35523.48828125), (0.492812514305115, -35616.33203125), (0.502812504768372, -35705.28515625), (0.512812495231628, -35790.578125), (0.522812485694885, -35872.4375), (0.532812476158142, -35951.0625), (0.542812526226044, -36026.640625), (0.552812516689301, -36099.3515625), (0.562812507152557, -36169.34765625), (0.572812497615814, -36236.77734375), (0.582812488079071, -36301.78515625), (0.592812478542328, -36364.49609375), (0.602812528610229, -36425.02734375), (0.612812519073486, -36483.4921875), (0.622812509536743, -36539.9921875), (0.6328125, -36594.62890625), (0.642812490463257, -36647.4921875), (0.652812480926514, -36698.6640625), (0.662812471389771, -36748.22265625), (0.672812521457672, -36796.25), (0.682812511920929, -36842.8125), (0.692812502384186, -36887.97265625), (0.702812492847443, -36931.796875), (0.712812483310699, -36974.33984375), (0.722812473773956, -37015.66015625), (0.732812523841858, -37055.8125), (0.742812514305115, -37094.83984375), (0.752812504768372, -37132.7890625), (0.762812495231628, -37169.70703125), (0.772812485694885, -37205.6328125), (0.782812476158142, -37240.609375), (0.792812526226044, -37274.671875), (0.802812516689301, -37307.85546875), (0.812812507152557, -37340.19140625), (0.822812497615814, -37371.71875), (0.832812488079071, -37402.45703125), (0.842812478542328, -37432.4453125), (0.852812528610229, -37461.703125), (0.862812519073486, -37490.26171875), (0.872812509536743, -37518.14453125), (0.8828125, -37545.375), (0.892812490463257, -37571.98046875), (0.902812480926514, -37597.97265625), (0.912812471389771, -37623.37890625), (0.922812521457672, -37648.21875), (0.932812511920929, -37672.51171875), (0.942812502384186, -37696.26953125), (0.952812492847443, -37719.515625), (0.962812483310699, -37742.26171875), (0.972812473773956, -37764.53125), (0.982812523841858, -37786.33203125), (0.992812514305115, -37807.6796875), (1.00281250476837, -37828.58984375), (1.01281249523163, -37849.078125), (1.02281248569489, -37869.15625), (1.03281247615814, -37888.83203125), (1.0428124666214, -37908.12109375), (1.05281245708466, -37927.03515625), (1.06281244754791, -37945.58203125), (1.07281255722046, -37963.7734375), (1.08281254768372, -37981.62109375), (1.09281253814697, -37999.1328125), (1.10281252861023, -38016.3203125), (1.11281251907349, -38033.1875), (1.12281250953674, -38049.75), (1.1328125, -38081.30859375), (1.14281249046326, -38117.46484375), (1.15281248092651, -38152.9765625), (1.16281247138977, -38205.91015625), (1.17281246185303, -38262.1171875), (1.18281245231628, -38317.31640625), (1.19281244277954, -38371.5546875), (1.20281255245209, -38438.77734375), (1.21281254291534, -38511.60546875), (1.2228125333786, -38595.37109375), (1.23281252384186, -38688.0703125), (1.24281251430511, -38779.234375), (1.25281250476837, -38868.90625), (1.26281249523163, -38957.11328125), (1.27281248569489, -39043.90234375), (1.28281247615814, -39129.30078125), (1.2928124666214, -39213.34375), (1.30281245708466, -39296.0625), (1.31281244754791, -39377.48828125), (1.32281255722046, -39457.6484375), (1.33281254768372, -39536.578125), (1.34281253814697, -39614.30078125), (1.35281252861023, -39690.84375), (1.36281251907349, -39766.23828125), (1.37281250953674, -39840.5078125), (1.3828125, -39913.67578125), (1.39281249046326, -39985.765625), (1.40281248092651, -40056.8046875), (1.41281247138977, -40126.8125), (1.42281246185303, -40195.81640625), (1.43281245231628, -40263.828125), (1.44281244277954, -40330.87890625), (1.45281255245209, -40396.98046875), (1.46281254291534, -40462.16015625), (1.4728125333786, -40526.43359375), (1.48281252384186, -40589.8203125), (1.49281251430511, -40652.3359375), (1.50281250476837, -40714.00390625), (1.51281249523163, -40774.83203125), (1.52281248569489, -40835.77734375), (1.53281247615814, -40903.80078125), (1.5428124666214, -40970.921875), (1.55281245708466, -41037.16015625), (1.56281244754791, -41104.97265625), (1.57281255722046, -41179.66796875), (1.58281254768372, -41268.05859375), (1.59281253814697, -41356.78515625), (1.60281252861023, -41444.3828125), (1.61281251907349, -41530.87890625), (1.62281250953674, -41623.875), (1.6328125, -41731.9765625), (1.64281249046326, -41841.390625), (1.65281248092651, -41949.47265625), (1.66281247138977, -42056.23828125), (1.67281246185303, -42161.71875), (1.68281245231628, -42265.93359375), (1.69281244277954, -42368.90234375), (1.70281255245209, -42473.17578125), (1.71281254291534, -42581.99609375), (1.7228125333786, -42689.55078125), (1.73281252384186, -42800.32421875), (1.74281251430511, -42912.33984375), (1.75281250476837, -43023.0546875), (1.76281249523163, -43132.5), (1.77281248569489, -43242.2890625), (1.78281247615814, -43356.40625), (1.7928124666214, -43469.234375), (1.80281245708466, -43580.78515625), (1.81281244754791, -43691.0625), (1.82281255722046, -43800.109375), (1.83281254768372, -43907.953125), (1.84281253814697, -44014.60546875), (1.85281252861023, -44120.09375), (1.86281251907349, -44224.4296875), (1.87281250953674, -44327.640625), (1.8828125, -44432.32421875), (1.89281249046326, -44538.984375), (1.90281248092651, -44646.5625), (1.90531253814697, -44674.09375), (1.90625, -44689.54296875), (1.90660154819489, -44698.22265625), (1.90673339366913, -44703.1015625), (1.90674579143524, -44703.35546875), (1.90676426887512, -44703.7421875), (1.90679216384888, -44704.3203125), (1.90683376789093, -44705.1875), (1.90689635276794, -44706.76171875))]
with open("abc","w") as f:
    for line in lis[0]:
        strs=" ".join(str(x) for x in line)
        f.write(strs+"\n")

输出(abc的内容):

output (abc's content):

0.0 0.0
0.00249999994412 -509.707885742
0.00499999988824 -1017.52648926
0.00875000003725 -1778.51281738
0.0143750002608 -2918.21899414
.....

这篇关于将元组列表写入txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆