Python Forum
"Split" file and comparison with CSV
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Split" file and comparison with CSV
#11
Yes I did, I don't understand this part (I should have told, sorry) :

for line in in_file:
        if block and line.strip() == '/##/':
            do_comparison(block=block)
            block = []
        block.append(line) # eventually use line.strip() to remove trailing new line \n
    do_comparison(block) # that is for the last block
Reply
#12
input_file = 'input_file.txt' # '/path/to/inputfile.txt'

def do_comparison(block):
    # do comparison here
    print(block)
    

with open(input_file) as in_file:
    header = next(in_file) # read the header
    block = [] # start with empty list, i.e. first block
    for line in in_file: # iterate over lines in the input file after the header
        if block and line.strip() == '/##/': # if block is not empty list and the line is /##/ - that is a start of new block
            do_comparison(block=block) # call comparison function
            block = [] # reset the block to be empty list again, i.e. start new block
        block.append(line) # add line to block. eventually use line.strip() to remove trailing new line \n
    do_comparison(block) # that is comparison for the last block
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#13
Ok thanks, it's more clear. Do you think I can work with file for the moment and when it's over, change the code to do this ?

With the files, I think it would be easier not to get lost for the moment...

And why do I have to do block=block ? do_comparison(block) isn't enough (like for the last one) ?
Reply
#14
you can do like I did on the last line:
do_comparison(block)
in this case you will pass block as positional argument
in my code I pass block as keyword argument. i.e. on the left side is the keyword block and the right-hand side is the value (i.e. block variable) I pass as argument.
In this case you can use both ways. But there are cases when you are allowed to pass only positional or only keyword arguments. I use keyword arguments as it makes code a bit more readable.
Also note you can use different variable name, not block, e.g.

with open(input_file) as in_file:
    header = next(in_file) # read the header
    foo = [] # start with empty list, i.e. first block
    for line in in_file: # iterate over lines in the input file after the header
        if foo and line.strip() == '/##/': # if foo is not empty list and the line is /##/ - that is a start of new block
            do_comparison(block=foo) # call comparison function
            foo = [] # reset the block to be empty list again, i.e. start new block
        foo.append(line) # add line to block. eventually use line.strip() to remove trailing new line \n
    do_comparison(foo) # that is comp
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#15
Ok thanks, I think I got it. I'll come back if I have other problem !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to "tee" (=split) output to screen and into file? pstein 6 1,363 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  Split pdf in pypdf based upon file regex standenman 1 2,071 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
Photo String comparison in a csv file in Python Pandas fleafy 2 1,148 Nov-18-2022, 09:38 PM
Last Post: fleafy
  How to split file by same values from column from imported CSV file? Paqqno 5 2,770 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  [split] Results of this program in an excel file eisamabodian 1 1,572 Feb-11-2022, 03:18 PM
Last Post: snippsat
  split txt file data on the first column value shantanu97 2 2,426 Dec-29-2021, 05:03 PM
Last Post: DeaD_EyE
  [split] Help- converting file with pyton script eltomassito 6 3,234 Jul-02-2021, 05:29 PM
Last Post: snippsat
  Split Characters As Lines in File quest_ 3 2,502 Dec-28-2020, 09:31 AM
Last Post: quest_
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,315 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Split and sort input file aawaleh 4 2,978 Apr-10-2020, 09:59 PM
Last Post: aawaleh

Forum Jump:

User Panel Messages

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