Home > Back-end >  how to get data from text file and rewrite to another from
how to get data from text file and rewrite to another from

Time:04-07

  • hello beautiful people so i have a text file like this :

  • user = user447

  • pass = 455555az

  • type = registred

  • date = 1 year

    and i want to read the file and rewrite it like this

  1. user|pass|type|date, line by line, i tried so many ways , i seem stuck since i have to deal with 1 million account
with open(file, "r") as f:
 data = []
 for line in f:
    key = line.rstrip('\n').split('=')
    key1 = key[1:2]

CodePudding user response:

You don't need to read the entire file all at once, instead, you can just read it in parts and write as you read (note the with block is used for two open() context managers, though you can nest them inside each other just as easily)

with open(source) as fh_src, open(destination, "w") as fh_dest:
    block = []
    for lineno, line in enumerate(fh_src, 1):
        # .split("=", 1)[-1] captures everything after the first =
        # this is also an opportunity to verify the key
        block.append(line.split("=", 1)[-1].strip())
        if len(block) == 4:
            fh_dest.write("{}|{}|{}|{}\n".format(*block))
            block = []  # reset block after each write

it's definitely worth creating some safeguards, however!

  • checking if lines really start with some key if you have a set of known keys or have some you intend to omit, or if you have some dynamic set of keys (say some users have a collection of previous password hashes, or different comments)
  • checking if block at the end (it should be cleared and write!)
  • checking = is really in each line or that any comments are kept or discarded
  • opening "w" will remove destination if it exists already (perhaps from a botched previous run), which may be undesirable

(lineno is only included to simplify discovering bad lines)

  • Related