Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
merge files
#4
You open a bunch of files (two for every time through the for loop), but you only ever close the last two that you opened (you close them outside the for loop).  Things like this are why there's a with block, so whatever you open is closed for you automatically when you can't use it anymore.  But if you're using python2.7 as your path indicates, you might not have access to it.

But you do have access to the csv module, so you don't need to do things like split the line on tabs.

You can probably delete most of those blocks, if you generate the id of the line a little better.  Instead of locusnumber, and newlocusnumber, turning it into something much simpler, it looks like the format is Cabther_[AB]NNNN, where A/B is determined by whether or not locusnumber is less than the magic number 2274.  So something like this:
def get_row_label(locusnumber):
    tag = "A" if locusnumber < 2274 else "B"
    return "Cabther_{0}{1:04.0f}".format(tag, locusnumber)

# for file in files
    locusnumber = 0
    for line in infile:
        label = get_row_label(locusnumber)
        elements = line.split("\t")
        if elements[0] == label:
            outfile.write(line)
        else:
            #etc
            outfile.write("\n" * difference + line)
And that's it, no more of the same block 7 times in a row.
Reply


Messages In This Thread
merge files - by AGC - Oct-04-2017, 07:09 PM
RE: merge files - by ichabod801 - Oct-04-2017, 07:23 PM
RE: merge files - by AGC - Oct-04-2017, 07:31 PM
RE: merge files - by nilamo - Oct-04-2017, 08:25 PM
RE: merge files - by AGC - Oct-04-2017, 08:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 3,588 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
  I'm trying to merge 2 .csv files with no joy! Sick_Stigma 3 1,065 Aug-03-2024, 03:20 PM
Last Post: mariadsouza362
  merge all xlsb files into csv mg24 0 861 Nov-13-2023, 08:25 AM
Last Post: mg24
  merge two csv files into output.csv using Subprocess mg24 9 3,750 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  Merge all json files in folder after filtering deneme2 10 4,733 Sep-18-2022, 10:32 AM
Last Post: deneme2
  How to merge all the files in a directory in to one file sutra 3 3,533 Dec-10-2020, 12:09 AM
Last Post: sutra
  Merge JSON Files Ugo 4 6,047 Aug-20-2020, 06:25 AM
Last Post: ndc85430
  How to read multiple csv files and merge data rajeshE 0 2,599 Mar-28-2020, 04:01 PM
Last Post: rajeshE
  error merge text files ledgreve 3 3,542 Nov-18-2019, 12:41 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020