0

I have a csv file which contain some data example of a csv file below

these file is crypted with "asdfg"
Name,Status,Time
abc,failed,7:30

these file is crypted with "asdfgghklm"
Name,Status,Time
def,running,12:30

Output -

Name,Status,Time
abc,failed,7:30
def,running,12:30

i want to skip some line present in whole csv file using python is there any way to do it? Thanks for the help

9
  • so you want to loop over alternate rows in the csv? Commented Aug 25, 2020 at 19:33
  • yes ! if these works. Commented Aug 25, 2020 at 19:35
  • Have you looked at this: stackoverflow.com/questions/549674/… and this: stackoverflow.com/questions/49266463/… ? Commented Aug 25, 2020 at 19:38
  • link these gives every line in list so how can i skip the list i don't want in my csv? Commented Aug 25, 2020 at 19:46
  • If it's every second line you can use a modulus operation on the loop counter. Did you have a look at the first link I posted? Commented Aug 25, 2020 at 19:47

1 Answer 1

-1

Reading a csv in python is just taking in a dictionary containing the lines consider the following:

import csv
with open('mycsv.csv', mode='r') as file:
    mycsv = csv.DictReader(file)

you can then look at specific elements within the dicts, script ones you dont like. based on your example if you're trying to get rid of a line like these file is crypted with "asdfgghklm" you can just check to see if there's a second element and if not dump it, or you can ignore it when you're looping

If your file structure is exactly as you mentioned above the dictionary should come out looking like this

OrderedDict([('these file is crypted with "asdfg"', 'Name'), (None, ['Status', 'Time'])])        
OrderedDict([('these file is crypted with "asdfg"', 'abc'), (None, ['failed', '7:30'])])
OrderedDict([('these file is crypted with "asdfg"', 'these file is crypted with "asdfgghklm"')]) 
OrderedDict([('these file is crypted with "asdfg"', 'Name'), (None, ['Status', 'Time'])])        
OrderedDict([('these file is crypted with "asdfg"', 'def'), (None, ['running', '12:30'])])

This is based on the code:

import csv
with open('file.csv', mode='r') as file:
    mycsv = csv.DictReader(file)
    for line in mycsv :
        print(line)

from here we can look at the output and use an if statement it terminate what you don't want and print what you do want. You will need to make adjustments based on your actual output, I like to use the print command first to guarantee i know how the system will see my csv file and then adapt my code to filter based on that formatting.

Sign up to request clarification or add additional context in comments.

1 Comment

this isn't the complete solution i.e. doesn't answer the skipping the csv lines based on occurrence or regex. It'd be great if you could edit your answer to add that information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.