Reading CSV with Python

1

As I can go by giving a "scan" in the CSV file and when finding a certain String in the CSV line, print in another file the complete line with its values after the string. Ex When you find the string "Bruno lorencini" in csv, print to a second file. "Bruno lorencini", pageview: 1, bounces: 2

    
asked by anonymous 23.03.2017 / 21:05

1 answer

2

Algorithm:

  • open the input file
  • create the output file
  • For each line of the input file,
    • If the line is the same as the text you are looking for,
      • show message "I found it!" for the user
      • write the line of the input file, in the output file

Code:

with open('arquivo_entrada.csv', 'r') as f:
    with open('arquivo_saida.csv', 'w') as g:
        for l in f.readlines():
            if l.strip() == "Bruno lorencini":
                print("Encontrei!")
                g.write(l)
    
24.03.2017 / 14:44