2013-04-12 89 views
1

我有这个项目,我必须在Python中编写代码,但对于初学者来说,这是非常困难的。基本上,我从来没有用python进行编程,并且从昨天开始只开始谷歌学习,所以我想也许你们可以帮助它,因为我甚至无法开始解决这个问题。从文本文件中选择特定的信息并将它们转换为Python中的数组/列表

我给出一个初始文本文件,让我们input.txt中调用它,它具有以下列方式如下数据:

Thomas Hales 
12 2345 
45 6780 

Peter Lebones 
10 15430 
11 1230 
23 3450 
John White 
2 12130 
11 32410 
15 4520 

有根据他们给出的姓名和电话号码。出于此问题的目的,左列中的数字仅仅是标识号。右栏中的数字是人们在银行投资的金额。

我应该采取文本文件中的所有数据,以各种方式操纵它,然后创建一个新的文本文件(所有这些都由python运行的脚本完成),称为output.txt 上面的例子中,包含此:

Thomas Hales 45 
Peter Lebones 10 
John White 11 

我有这个地步(但它不工作,再加上它是一团糟,我与别人的帮助下,谁也不会知道是不是真的做到了。他在做什么):

import sys 
import subprocess 
import re 
import string 


try: 
    fread=open(sys.argv[1]).readlines() 
except IOError: 
    print "There is no file like that!" 
    sys.exit() 
except IndexError: 
    print "There is no argumentum given" 
alpha = string.ascii_letters 
writeout=open("result.txt","w") 
inputarray=fread.readlines() 
for ... in inputarray: # not sure what goes in the "..." part 
    array=inputarray.split('\n') 
for i in range(len(array)-1): 
    if array[i].isalpha(): 
    writeout.write(array[i]+" ") 

fread.close() 
writeout.close() 

所以基本上,我给了一个文本文件。那么我应该为每个人选择最高的投资,并将左栏中的数字与最高的投资相关联。然后,我应该让脚本制作一个output.txt文件,其中包含每个人的姓名和最高投资的“Id号码”。

回答

2

我假设当一行以数字开头时,我们有一个投资,否则就是一个名字。

每次找到一个名字,写出来的以前的名称和最高的投资标识符:

with open(sys.argv[1]) as inputfile, open("result.txt","w") as outputfile: 
    name = None 
    investment_id = max_investment = 0 
    for line in inputfile: 
     if not line.strip(): continue # skip empty lines 

     if not line[:1].isdigit(): # name 
      if name and investment_id: 
       # write previous name 
       outputfile.write('{} {}\n'.format(name, investment_id)) 
      name = line.strip() 
      investment_id = max_investment = 0 

     else: 
      id, investment = [int(i) for i in line.split()] 
      if investment > max_investment: 
       max_investment = investment 
       investment_id = id 

    if name and investment_id: 
     # write last name 
     outputfile.write('{} {}\n'.format(name, investment_id)) 

对于示例输入,这写道:

Thomas Hales 45 
Peter Lebones 10 
John White 11 
+0

非常感谢你的帮助。现在唯一的一点是我尝试使用execfile(“filename.py”,'input.txt')运行它,但它说TypeError必须不是str。通常我知道以python filename.py'input.txt'运行会更好,但实际上,我运行的是Windows 7,并且我昨天试图用python file.py打开任何文件并且没有任何工作。该文件与python.exe位于相同的路径,所以不知道有什么问题。 – user1966576

+0

嗯,我刚刚读到你不能传递参数与execfile ...但我无法得到它打开脚本传统的python script.py arg1方式,并且子进程不工作或者...我可以运行文件与execfile,但它说它缺少一个参数... – user1966576

+0

'subprocess'应该在Windows上工作得很好。确保你使用文件的* full *路径。 –

1

也许这基本配方处理一行一行的文件将帮助你开始右脚。

import sys 

file_name = sys.argv[1] 

# This takes care of closing the file after we're done processing it. 
with open(file_name) as file_handle: 

    # Once we have a file handle, we can iterate over it. 
    for line in file_handle: 

     # This is where your real programming logic will go. 
     # For now, we're just printing the input line. 
     print line, 

我怀疑你可能还会发现split()是有用的,因为它可以让你打散数字线。例如,你可以试试这个它是如何工作的实验:

parts = line.split() 
print parts 
1

使用Python re模块可以给你一个很好的发射平台,只需打破行到的东西,你可以遍历。

>>> results = re.findall("(\w+) (\w+)",buff,re.S) 
[('Thomas', 'Hales'), ('12', '2345'), ('45', '6780'), ('63', '3210'), ('Peter', 'Lebones'), ('10', '15430'), ('23', '3450'), ('John', 'White'), ('2', '12130'), ('11', '32410'), ('15', '4520')] 
1
with open("input.txt", "r") as inp, open("output.txt", "w") as out: 
     data = inp.readlines() 
     for i in xrange(0, len(data), 4): 
      name = data[i].strip() 
      maxi = 0 
      true_code = 0 
      for item in data[i+1: i+4]: 
       code, bal = item.strip().split(" ") 
       code, bal = int(code), int(bal) 
       if bal >= maxi: 
        maxi = bal 
        true_code = code 
      out.write("%s %s" %(name, true_code)) 
+0

这给出了“需要超过1个值才能解包”的错误,但谢谢。 – user1966576

+0

可能是由于代码与天平之间存在多个空格字符,例如11__1230而不是11_1230 – Zangetsu

相关问题