String 如何使用.txt文件中的列表和行

String 如何使用.txt文件中的列表和行,string,list,python-2.7,rows,equation,String,List,Python 2.7,Rows,Equation,我正在处理一个文本文件(现在称之为user.txt),希望能够跳过每行中的第一项[0],在等式[3:4]中使用以下数字,然后从该特定行中吐出相等的答案,并将其添加到列表中每行[5]的末尾。但我正在努力跳过[0],然后用我的等式继续,然后吐出我的答案。我是python新手,正在寻找关于如何实现这一点的好结构的建议。下面是我的user.txt列表示例 ['Port', '1853282.679', '1673876.66', '1 ', '2'] ['Bruns', '1963178.059', '

我正在处理一个文本文件(现在称之为user.txt),希望能够跳过每行中的第一项[0],在等式[3:4]中使用以下数字,然后从该特定行中吐出相等的答案,并将其添加到列表中每行[5]的末尾。但我正在努力跳过[0],然后用我的等式继续,然后吐出我的答案。我是python新手,正在寻找关于如何实现这一点的好结构的建议。下面是我的user.txt列表示例

['Port', '1853282.679', '1673876.66', '1 ', '2']
['Bruns', '1963178.059', '1695301.229', '0 ', '1']
['Tops', '2092564.258', '1666785.835', '5 ', '6']
['Brave', '3464227.016', '1699924.786', '1 ', '1']
['Crew', '3056933.525', '1688585.272', '9 ', '3']
['Hann', '3180151.244', '1670897.027', '7 ', '3']
['Luke', '3403469.566', '1694894.58', '0 ', '1']
.......

如果您已经在阅读这些行,您可以像这样解析每一行:

line = "['Port', '1853282.679', '1673876.66', '1 ', '2']"
line = line[1:-1] # Line is now "'Port', '1853282.679', '1673876.66', '1 ', '2'"
line = line.split(',') # Line is now a list containing the former comma separated params

现在,您可以使用诸如
行[0]
行[1]
等参数。如果您已经在阅读这些行,您可以像这样解析每一行:

line = "['Port', '1853282.679', '1673876.66', '1 ', '2']"
line = line[1:-1] # Line is now "'Port', '1853282.679', '1673876.66', '1 ', '2'"
line = line.split(',') # Line is now a list containing the former comma separated params

现在,您可以使用诸如
行[0]
行[1]
等参数,尝试用代码逐字翻译您的需求:

#coding:utf-8

##I am working out of a text file (call it user.txt for now)
user_txt = ('''['Port', '1853282.679', '1673876.66', '1 ', '2']
['Bruns', '1963178.059', '1695301.229', '0 ', '1']
['Tops', '2092564.258', '1666785.835', '5 ', '6']
['Brave', '3464227.016', '1699924.786', '1 ', '1']
['Crew', '3056933.525', '1688585.272', '9 ', '3']
['Hann', '3180151.244', '1670897.027', '7 ', '3']
['Luke', '3403469.566', '1694894.58', '0 ', '1']'''
    .replace('[','')
    .replace(']','')
    .replace(' ','')
    .replace("'",''))
lines = user_txt.split('\n')
##and would like to be able to skip the first item [0] in each row,
rows = [line.split(',')[1:] for line in lines]
##use the following numbers in an equation [3:4],
equations = [int(row[2]) + int(row[3]) for row in rows]
##and then spit out my equated answer from that specific row
print equations
##and add it to the end of the row [5] for each individual line going down the list.
print [row + [int(row[2]) + int(row[3])] for row in rows]

Phil,试着用代码逐字翻译你的需求:

#coding:utf-8

##I am working out of a text file (call it user.txt for now)
user_txt = ('''['Port', '1853282.679', '1673876.66', '1 ', '2']
['Bruns', '1963178.059', '1695301.229', '0 ', '1']
['Tops', '2092564.258', '1666785.835', '5 ', '6']
['Brave', '3464227.016', '1699924.786', '1 ', '1']
['Crew', '3056933.525', '1688585.272', '9 ', '3']
['Hann', '3180151.244', '1670897.027', '7 ', '3']
['Luke', '3403469.566', '1694894.58', '0 ', '1']'''
    .replace('[','')
    .replace(']','')
    .replace(' ','')
    .replace("'",''))
lines = user_txt.split('\n')
##and would like to be able to skip the first item [0] in each row,
rows = [line.split(',')[1:] for line in lines]
##use the following numbers in an equation [3:4],
equations = [int(row[2]) + int(row[3]) for row in rows]
##and then spit out my equated answer from that specific row
print equations
##and add it to the end of the row [5] for each individual line going down the list.
print [row + [int(row[2]) + int(row[3])] for row in rows]

谢谢你的建议,这将工作,但我有一个txt文件有100行,我需要从txt文件拉。对从txt文件中提取有什么建议吗?谢谢你的建议,这会有用的,但是我有一个包含100行的txt文件,我需要从txt文件中提取。对从txt文件中提取有什么建议吗?