Python Forum
Identifying items in a csv file that also appear in a Text extract
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Identifying items in a csv file that also appear in a Text extract
#12
(Sep-21-2016, 09:21 PM)nilamo Wrote:
(Sep-21-2016, 09:11 PM)Jaynorth Wrote: Counter() just returns Counter() in the console when the script is run. country_codes is the names of the csv file which is read into the script and codes is just a variable that I used to assign the relevant column in the csv file - country_codes['English short name lower case']

I am not matching on the Alpha-2 or Alpha-3 columns in the csv file which uses the 3 letter representation of the country like "CAN" XD

But, why use any sort of Counter() function at all?  len() would do the exact same thing, wouldn't it?

>>> text = '''
... Once upon a time, there was the great country of Mexico.  Then there... blah blah blah'''
>>> [word for word in text.split()]
['Once', 'upon', 'a', 'time,', 'there', 'was', 'the', 'great', 'country', 'of', 'Mexico.', 'Then', 'there...', 'blah', 'blah', 'blah']

>>> import re
>>> [word for word in text.split() if re.sub(r'\W', '', word) in codes]
['Mexico.']

>>> len([word for word in text.split() if re.sub(r'\W', '', word) in codes])
1
I used Pandas to extract the text so it is a dataframe and not a string so I cannot use .split() or can I?
Reply


Messages In This Thread
RE: Identifying items in a csv file that also appear in a Text extract - by Jaynorth - Sep-21-2016, 09:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cleaning a dataset: How to extract text between two patterns Palke 0 1,238 Mar-06-2023, 05:13 PM
Last Post: Palke
  extract and plot data from a txt file usercat123 2 1,307 Apr-20-2022, 06:50 PM
Last Post: usercat123
  [machine learning] identifying a number 0-9 from a 28x28 picture, not working SheeppOSU 0 1,925 Apr-09-2021, 12:38 AM
Last Post: SheeppOSU
  Comparing and Identifying ID with Percentage jonatasflausino 1 2,505 Jun-23-2020, 06:44 PM
Last Post: hussainmujtaba
  Identifying consecutive masked values in a 3D data array chai0404 12 6,049 Feb-01-2020, 12:59 PM
Last Post: perfringo
  Validate Excel with text in text file Vinci141 3 3,531 Dec-03-2018, 04:03 PM
Last Post: Larz60+
  OpenCV - extract 1st frame out of a video file kerzol81 2 22,547 Nov-12-2018, 09:12 AM
Last Post: kerzol81
  Upload csv file as numbers (floating?) and extract element, row, and column bentaz 7 4,688 Mar-19-2018, 05:34 PM
Last Post: bentaz
  Extract data between two dates from a .csv file using Python 2.7 sujai_banerji 1 10,511 Nov-15-2017, 09:48 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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