Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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
将数据保存在不带括号和逗号的txt文件中(Python)_Python - Fatal编程技术网

将数据保存在不带括号和逗号的txt文件中(Python)

将数据保存在不带括号和逗号的txt文件中(Python),python,Python,我的源数据附加在以下数组中: x,y,w,h = track_window listing.append([x,y]): 在哪里 及 所以x值应该被写入一个文本文件,然后y值不带逗号和括号,就像这种形式,每两个数字和每行(8)个数字之间有一个空格 x: y: 我怎样才能做到呢 我所做的: with open('text.txt', 'rb') as f: val = pickle.load(f) for i in range(2): if i==0: #d

我的源数据附加在以下数组中:

x,y,w,h = track_window
listing.append([x,y]): 
在哪里

所以x值应该被写入一个文本文件,然后y值不带逗号和括号,就像这种形式,每两个数字和每行(8)个数字之间有一个空格

x:

y:

我怎样才能做到呢

我所做的:

with open('text.txt', 'rb') as f:
   val = pickle.load(f)
   for i in range(2):
      if i==0:
      #def Extract(lst):
        a = [item[0] for item in val] 
        #return a        
      if i==1:
      #def Extract(lst):
        b = [item[1] for item in val] 
      #return b
  
      #print(val)
      #print(Extract(val)) 
      print(a)
      print(b)

  f.close()

谢谢

很高兴看到您尝试了什么。尽管如此,这将为你工作

def write_in_chunks(f, lst, n):
    for i in range(0, len(lst), n):
        chunk = lst[i : i+n]
        f.write(" ".join(str(val) for val in chunk) + "\n")

        
x = [300, 300, 300, 296, 291, 294, 299, 284, 303, 323, 334, 343, 354, 362, 362, 362, 360, 361, 351]

y = [214, 216, 214, 214, 216, 216, 215, 219, 217, 220, 218, 218, 222, 222, 222, 223, 225, 224, 222]

with open("output.txt", "w") as f:
    write_in_chunks(f, x, 8)
    write_in_chunks(f, y, 8)
创建
output.txt
包含

300 300 300 296 291 294 299 284
303 323 334 343 354 362 362 362
360 361 351
214 216 214 214 216 216 215 219
217 220 218 218 222 222 222 223
225 224 222

在输出中添加额外的空行留给读者作为练习。。。(提示:查看现有换行符的编写位置)。

谢谢@alaniwi,我添加了我所做的。但是你的解决方案为我省了很多麻烦,请提供一个解决方案。
300 300 300 296 291 294 299 284 

303 323 334 343 354 362 362 362 

360 361 351
with open('text.txt', 'rb') as f:
   val = pickle.load(f)
   for i in range(2):
      if i==0:
      #def Extract(lst):
        a = [item[0] for item in val] 
        #return a        
      if i==1:
      #def Extract(lst):
        b = [item[1] for item in val] 
      #return b
  
      #print(val)
      #print(Extract(val)) 
      print(a)
      print(b)

  f.close()
def write_in_chunks(f, lst, n):
    for i in range(0, len(lst), n):
        chunk = lst[i : i+n]
        f.write(" ".join(str(val) for val in chunk) + "\n")

        
x = [300, 300, 300, 296, 291, 294, 299, 284, 303, 323, 334, 343, 354, 362, 362, 362, 360, 361, 351]

y = [214, 216, 214, 214, 216, 216, 215, 219, 217, 220, 218, 218, 222, 222, 222, 223, 225, 224, 222]

with open("output.txt", "w") as f:
    write_in_chunks(f, x, 8)
    write_in_chunks(f, y, 8)
300 300 300 296 291 294 299 284
303 323 334 343 354 362 362 362
360 361 351
214 216 214 214 216 216 215 219
217 220 218 218 222 222 222 223
225 224 222