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
#8
amend my previous example

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)
    block = []
    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
Output:
['/##/\n', '/donnee1/XXXXXXXX\n', '/donnee2/A\n'] ['/##/\n', '/donnee1/YYYYY\n', '/donnee2/B\n']
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


Messages In This Thread
RE: "Split" file and comparison with CSV - by buran - Aug-07-2018, 07:25 AM
RE: "Split" file and comparison with CSV - by buran - Aug-07-2018, 08:49 AM
RE: "Split" file and comparison with CSV - by buran - Aug-07-2018, 08:58 AM
RE: "Split" file and comparison with CSV - by buran - Aug-07-2018, 09:49 AM
RE: "Split" file and comparison with CSV - by buran - Aug-07-2018, 10:04 AM
RE: "Split" file and comparison with CSV - by buran - Aug-07-2018, 10:08 AM
RE: "Split" file and comparison with CSV - by buran - Aug-07-2018, 10:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to "tee" (=split) output to screen and into file? pstein 6 1,493 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  Split pdf in pypdf based upon file regex standenman 1 2,186 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
Photo String comparison in a csv file in Python Pandas fleafy 2 1,221 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,888 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  [split] Results of this program in an excel file eisamabodian 1 1,627 Feb-11-2022, 03:18 PM
Last Post: snippsat
  split txt file data on the first column value shantanu97 2 2,493 Dec-29-2021, 05:03 PM
Last Post: DeaD_EyE
  [split] Help- converting file with pyton script eltomassito 6 3,351 Jul-02-2021, 05:29 PM
Last Post: snippsat
  Split Characters As Lines in File quest_ 3 2,584 Dec-28-2020, 09:31 AM
Last Post: quest_
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,452 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Split and sort input file aawaleh 4 3,048 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