简体   繁体   中英

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.

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.

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.

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. There seems to be too much nesting being used for 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?

What about this? Just use 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']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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