'How do I read in a text file as list and use that list variable in a function?
I am trying to call text in a text file which is in the format of - 'word1', 'word2', etc. into a variable and use that variable within a function below. The reason I want to call the words from a text file into a variable within the code is because there are a large amount of words. I get an error message below when I run the code I have so far. The goal is to use the words in the text file as a variable in my code and then use that variable in my function. Any help is appreciated.
Code:
# read text into variable
with open('text.txt') as f:
variable=f.read()
# declarations
ser = df['Comments']
search_words1 = variable
l2 = []
# I want to count the words in df['Comments'] based on search_words1 variable and create dataframe
def count(search_words1):
for i in search_words1:
c = ser[(ser.str.contains(i))].count()
l2.append(c)
return pd.DataFrame({'Comments':search_words1, 'Count':l2})
# Call function and sort
twords=count(search_words1).sort_values(by='Count',ascending=False)
# graph results
twords.plot(kind='barh',x='Comments',figsize=(5,5),fontsize=14, title ='2021 Comments Common words')
Error:
---------------------------------------------------------------------------
error Traceback (most recent call last)
C:\Users\HECTOR~1.HER\AppData\Local\Temp/ipykernel_2560/3721059552.py in <module>
21 # Call function and sort
22
---> 23 twords=count(search_words1).sort_values(by='Count',ascending=False)
24
25 # graph results
C:\Users\HECTOR~1.HER\AppData\Local\Temp/ipykernel_2560/3721059552.py in count(search_words1)
15 def count(search_words1):
16 for i in search_words1:
---> 17 c = ser[(ser.str.contains(i))].count()
18 l2.append(c)
19 return pd.DataFrame({'Comments':search_words1, 'Count':l2})
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\strings\accessor.py in wrapper(self, *args, **kwargs)
114 )
115 raise TypeError(msg)
--> 116 return func(self, *args, **kwargs)
117
118 wrapper.__name__ = func_name
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\strings\accessor.py in contains(self, pat, case, flags, na, regex)
1151 dtype: bool
1152 """
-> 1153 if regex and re.compile(pat).groups:
1154 warnings.warn(
1155 "This pattern has match groups. To actually get the "
C:\ProgramData\Anaconda3\lib\re.py in compile(pattern, flags)
250 def compile(pattern, flags=0):
251 "Compile a regular expression pattern, returning a Pattern object."
--> 252 return _compile(pattern, flags)
253
254 def purge():
C:\ProgramData\Anaconda3\lib\re.py in _compile(pattern, flags)
302 if not sre_compile.isstring(pattern):
303 raise TypeError("first argument must be string or compiled pattern")
--> 304 p = sre_compile.compile(pattern, flags)
305 if not (flags & DEBUG):
306 if len(_cache) >= _MAXCACHE:
C:\ProgramData\Anaconda3\lib\sre_compile.py in compile(p, flags)
762 if isstring(p):
763 pattern = p
--> 764 p = sre_parse.parse(p, flags)
765 else:
766 pattern = None
C:\ProgramData\Anaconda3\lib\sre_parse.py in parse(str, flags, state)
946
947 try:
--> 948 p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
949 except Verbose:
950 # the VERBOSE flag was switched on inside the pattern. to be
C:\ProgramData\Anaconda3\lib\sre_parse.py in _parse_sub(source, state, verbose, nested)
441 start = source.tell()
442 while True:
--> 443 itemsappend(_parse(source, state, verbose, nested + 1,
444 not nested and not items))
445 if not sourcematch("|"):
C:\ProgramData\Anaconda3\lib\sre_parse.py in _parse(source, state, verbose, nested, first)
834 p = _parse_sub(source, state, sub_verbose, nested + 1)
835 if not source.match(")"):
--> 836 raise source.error("missing ), unterminated subpattern",
837 source.tell() - start)
838 if group is not None:
error: missing ), unterminated subpattern at position 0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
