python 字符提取

想把如下坐标绘图,需要提取x y 值坐标,放到循环中画图,应如何处理,是用正则还是有其他方法?
(10,10)
(11,10)
(12,10)
(13,10)
(14,10)
(15,10)
(16,10)
(17,10)
(18,10)
(19,10)
(20,10)
(21,10)
(22,10)
(23,10)
(24,10)
(25,10)
(26,10)
(118,192)
(119,192)
(120,192)
(121,192)
(122,192)
(123,192)

阅读 2.1k
2 个回答

弄好了 但不知是不是最优方法

import re
import numpy as np  
import matplotlib.pyplot as plt  

a=[]
b=[]
for line in open("t.txt"):  
    q=re.split('\D',line)
    x=int(q[1])
    y=int(q[2])
    a.append(x)
    b.append(y)
print a,b
plt.plot(a,b,'or')
plt.show()

自己的小程序的话,可以用eval

from PIL import Image


def draw():
    # read file
    pixels_array = []
    with open('pixels.txt', 'r') as fp_iter:
        for line in fp_iter:
            pixels_array.append(eval(line))

    # draw
    color = (128, 128, 128)
    im = Image.new('RGB', (256, 256), (255, 255, 255))
    for position in pixels_array:
        im.putpixel(position, color)
    im.show()
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题