Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Beginner question
#1
Hi all,

I'm trying to work with a large CSV file to effectively return what a filter would in excel....
So when the value in column 4 = a defined number then return that entire row.
Seems pointless, but some of the csv files won't open with excel as they have too many rows, so this would be quite useful for just looking at 1 customer.
Also, I plan on expanding this a lot further, this is just the 1st step!

What I have so far is:

import csv
import sys


number = '1'

csv_file = csv.reader(open('File.csv', "r", encoding="Latin1"))
filename  = open("Result.csv",'w')
sys.stdout =filename
print(next(csv_file))


for row in csv_file:

    if number == row[4]:
       print(','.join(row))
This is getting me some of the way there, however there are a few cases where cells have ',' within them, this results in the results from here going into 2 different columns.

I was trying to use quotechar="'" but I am using it wrong as my file returns nothing with this.

Also, using the
print(','.join(row))
it changed 'text' to text (which is what I want) however the header row still has 'text'.

Anyone know a good way of fixing these two issues?
Guessing it's quite straight forward, but anything I find online I think I end up using wrong as it returns errors, or it runs but the result file only has the header row, or nothing at all!

Thanks in advance,

N
Reply


Messages In This Thread
Basic Beginner question - by NHeav - Sep-11-2019, 02:37 PM
RE: Basic Beginner question - by ichabod801 - Sep-11-2019, 02:47 PM
RE: Basic Beginner question - by sd_0912 - Sep-11-2019, 02:53 PM
RE: Basic Beginner question - by buran - Sep-11-2019, 03:36 PM
RE: Basic Beginner question - by NHeav - Sep-13-2019, 11:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 233 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  Basic Coding Question: Exit Program Command? RockBlok 3 621 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,799 Jul-11-2022, 05:59 AM
Last Post: Serena2022
  [solved] Basic question on list matchiing paul18fr 7 1,936 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
Question Beginner Boolean question [Guessing game] TKB 4 2,389 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Very basic calculator question BoudewijnFunke 4 1,990 Dec-10-2021, 10:39 AM
Last Post: BoudewijnFunke
  Beginner question NameError amazing_python 6 2,549 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  Beginner question - storing values cybertron2 4 3,296 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  basic question isinstance tames 5 2,873 Nov-23-2020, 07:20 AM
Last Post: tames
  basic question about tuples and immutability sudonym3 6 2,979 Oct-18-2020, 05:11 PM
Last Post: sudonym3

Forum Jump:

User Panel Messages

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