简体   繁体   English

将文本文件转换为列表

[英]Converting text file to list

We had our customer details spread over 4 legacy systems and have subsequently migrated all the data into 1 new system. 我们将客户详细信息分布在4个旧系统上,随后将所有数据迁移到1个新系统中。

Our customers previously had different account numbers in each system and I need to check which account number has been used in the new system, which supports API calls. 我们的客户以前在每个系统中都有不同的帐号,我需要检查新系统中使用了哪个帐号,该系统支持API调用。

I have a text file containing all the possible account numbers, structured like this: 我有一个文本文件,其中包含所有可能的帐号,其结构如下:

30000001, 30000002, 30000003, 30000004
30010000, 30110000, 30120000, 30130000
34000000, 33000000, 32000000, 31000000

Where each row represents all the old account numbers for each customer. 其中每一行代表每个客户的所有旧帐号。

I'm not sure if this is the best approach but I want to open the text file and create a nested list: 我不确定这是否是最好的方法,但是我想打开文本文件并创建一个嵌套列表:

[['30000001', '30000002', '30000003', '30000004'], ['30010000', '30110000', '30120000', '30130000'], ['34000000', '33000000', '32000000', '31000000']]

Then I want to iterate over each list but to save on API calls, as soon as I have verified the new account number in a particular list, I want to break out and move onto the next list. 然后,我要遍历每个列表,但要保存API调用,一旦我在特定列表中验证了新的帐号,就想进入下一个列表。

import json
from urllib2 import urlopen

def read_file():
    lines = [line.rstrip('\n') for line in open('customers.txt', 'r')]
    return lines

def verify_account(*customers):
    verified_accounts = []
    for accounts in customers:
        for account in accounts:
            url = api_url + account
            response = json.load(urlopen(url))
            if response['account_status'] == 1:
                verified_accounts.append(account)
                break

    return verified_accounts

The main issue is when I read from the file it returns the data like below so I can't iterate over the individual accounts. 主要问题是,当我从文件读取数据时,它会返回如下数据,因此我无法遍历各个帐户。

['30000001, 30000002, 30000003, 30000004', '30010000, 30110000, 30120000, 30130000', '34000000, 33000000, 32000000, 31000000']

Also, is there a more Pythonic way of using list comprehensions or similar to iterate and check the account numbers. 此外,还有一种使用列表推导或类似方法迭代和检查帐号的更Python化的方法。 There seems to be too much nesting being used for Python? 似乎在Python中使用了太多嵌套?

The final item to mention is that there are over 255 customers to check, well there is almost 1000. Will I be able to pass more than 255 parameters into a function? 最后要提到的是,要检查的客户超过255个,而差不多有1000个客户。我是否可以将255个以上的参数传递给函数?

What about this? 那这个呢? Just use str.split() : 只需使用str.split()

l = []
with open('customers.txt', 'r') as f:
    for i in f:
        l.append([s.strip() for s in i.split(',')])

Output: 输出:

[['30000001', '30000002', '30000003', '30000004'],
 ['30010000', '30110000', '30120000', '30130000'], 
 ['34000000', '33000000', '32000000', '31000000']]

How about this? 这个怎么样?

with open('customers.txt','r') as f:
    final_list=[i.split(",") for i in f.read().replace(" ","").splitlines()]

print final_list

Output: 输出:

[['30000001', '30000002', '30000003', '30000004'], 
 ['30010000', '30110000', '30120000', '30130000'], 
 ['34000000', '33000000', '32000000', '31000000']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM