用python从文本文件中读取坐标

用python从文本文件中读取坐标,python,Python,我有一个文本文件(coordinates.txt): 我有一个python脚本,里面有一个while循环: count = 0 while True: count += 1 c1 = c2 = 对于上述循环的每次运行,我需要读取每一行(计数),并将c1、c2设置为每一行的编号(用逗号分隔)。有人能告诉我最简单的方法吗 ============================ import csv count = 0 while True: count += 1

我有一个文本文件(
coordinates.txt
):

我有一个python脚本,里面有一个while循环:

count = 0
while True:
 count += 1
 c1 = 
 c2 = 
对于上述循环的每次运行,我需要读取每一行(计数),并将
c1、c2
设置为每一行的编号(用逗号分隔)。有人能告诉我最简单的方法吗

============================

import csv

count = 0

while True:
        count += 1
        print 'val:',count
        for line in open('coords.txt'):
                c1, c2 = map(float, line.split(','))
                break
        print 'c1:',c1
        if count == 2: break

正如我在上面所评论的那样,最好的方法是:

import csv

with open('coordinates.txt') as f:
    reader = csv.reader(f)
    for count, (c1, c2) in enumerate(reader):
        # Do what you want with the variables.
        # You'll probably want to cast them to floats.

正如@abarnert所指出的那样,我还提供了一种更好的方法,使用
enumerate
使用
count
变量。

这正是
csv
模块的目的。我对这一点非常陌生。任何帮助都会非常感激。这会在换行中留下,OP不会想要的。而且,它基本上只是重新发明了
csv
+1。如果您需要
count
,您可以使用
enumerate(reader)
,而不是手动维护它。@abarnert谢谢您提醒我,我不想包括这个。更新。
f=open('coordinates.txt','r')
count =0
for x in f:
    x=x.strip()
    c1,c2 = x.split(',')
    count +=1
import csv

with open('coordinates.txt') as f:
    reader = csv.reader(f)
    for count, (c1, c2) in enumerate(reader):
        # Do what you want with the variables.
        # You'll probably want to cast them to floats.