Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将元组列表写入txt文件_Python_List_Tuples - Fatal编程技术网

Python 将元组列表写入txt文件

Python 将元组列表写入txt文件,python,list,tuples,Python,List,Tuples,考虑以下元组列表: 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))] 我想用以下格式将信息写入

考虑以下元组列表:

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文件:

0.0 0.0
0.00249999994412065 -509.707885742188
.... 
我一直在使用以下代码:

with open(fname, 'w') as graphd:
    for row in list:
        print >>graphd, ', '.join(map(str, row))
graphd.close()
其中:
fname
是文件的路径
list
是元组列表

结果是:(虽然很接近,但仍然不是我想要的…)


问题是您正在迭代列表,但列表只包含一个项,即元组的元组。因此,您必须迭代该元组的元组,即
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的内容):


获取
行中的第一个和第二个元素
并与
\n
联接:

with open(fname, 'w') as graphd:
    for row in list:
        print >>graphd, '\n'.join(map(str, row[0])).join(" ").join(map(str, row[0]))
graphd.close()
我会将数据存储为。这使得使用Python很容易再次导出和导入数据。这使得与其他语言或程序共享数据变得容易

import json
with open('data.json', 'w') as outfile:
  json.dump(list, outfile)
然后可以使用重新加载数据

如果需要更可读的数据,可以将indent=4添加到json.dump

json.dump(list, outfile, indent=4)
输出将是

[
    [
        [
            0.0, 
            0.0
        ], 
        [
            0.00249999994412065, 
            -509.707885742188
        ], 
        [
            0.00499999988824129, 
            -1017.52648925781
        ], 
        ....

您不需要这个列表,所以像这样创建元组的元组

my_data = (3123, 123123), (133232, 1231232)
for row in my_data:
    handler.write(' '.join(str(r) for r in row) + "\n")

不要使用
列表
名称。这是一个保留关键字@namit:它不是一个保留关键字。它是内置类型的名称,因此使用
list
会屏蔽该类型。这不是个好主意,但保留关键字会导致语法错误。”除了“或”if“是保留关键字。不需要显式关闭
graphd
。将它作为上下文管理器与
一起使用
可确保它自动为您关闭。
graphd.close()
行是完全多余的。@namit:我刚才用列表作为一个坏例子。在我的代码中,它有一个有趣的名字…@MartijnPieters:谢谢澄清:)这比其他方法快吗?我可以用类似的nodepad打开json吗?我不会说它更快,但如果需要再次导入代码,这无疑是最简单的方法。只需执行
list=json.load(infle)
即可将数据作为列表再次加载。循环不需要潮汐。是的,您仍然可以在记事本中打开它。JSON具有广泛的可移植性。该规范非常简单,因此易于正确实现。如果使用JSON保存文件,那么可以很容易地将文件导入Python、JavaScript、Ruby、Perl、C++、C++等。JSON是一个纯文本文件,所以你可以用记事本打开它。。。如果您在Linux上编写JSON文件,您可能需要使用写字板,因为行尾与Windows中的行尾不完全相同,并且记事本无法处理它。我很确定Python中内置的JSON支持是用C编写的,所以会更快,因为JSON使用的格式略有不同。如果再次导入,它的格式应与list/tuples完全相同。顺便说一句,请尝试将
json.dump
更改为
json.dump(list,outfile,indent=4)
,以提高可读性。@jpcgander这在我的系统上只需要1.57毫秒。@jpcgander这可能会有帮助:
json.dump(list, outfile, indent=4)
[
    [
        [
            0.0, 
            0.0
        ], 
        [
            0.00249999994412065, 
            -509.707885742188
        ], 
        [
            0.00499999988824129, 
            -1017.52648925781
        ], 
        ....
my_data = (3123, 123123), (133232, 1231232)
for row in my_data:
    handler.write(' '.join(str(r) for r in row) + "\n")