TypeError Traceback (most recent call last) Cell In[30], line 29 27 statutedf =…
- User
TypeError Traceback (most recent call last) Cell In[30], line 29 27 statute_df = os.path.join(input_dir, input_file) 28 print(statute_df) ---> 29 statutes = statute_df['statute'].to_list() 30 statutes_id = statute_df['statute id'].to_list() 31 # basic_factors = party_df["basic_factor"]
TypeError: string indices must be integers, not 'str'
- Assistant
The error message:
codeTypeError: string indices must be integers, not 'str'occurs because you are trying to use string indexing syntax on a string, but using a string as the index.
The problematic line:
pythonstatutes = statute_df['statute'].to_list()What is
statute_df?Looking at the earlier line:
pythonstatute_df = os.path.join(input_dir, input_file) print(statute_df) - User
- Assistant
- User
- Assistant
- User
UnicodeEncodeError Traceback (most recent call last) Cell In[41], line 73 65 os.makedirs(os.path.join(output_dir, "pickle"), exist_ok=True) 66 save_pickle( 67 clusterID_to_phrases, 68 os.path.join( (...) 71 ), 72 ) ---> 73 save_cluster( 74 clusterID_to_phrases, 75 os.path.join(output_dir, input_file.replace(".xlsx", "" + ".txt")), 76 )
Cell In[9], line 23, in save_cluster(clusterID_to_phrases, file_path) 21 for tuple in tuples: 22 complex_factor, complex_factor_to_id = tuple ---> 23 fw.write( 24 str(cid) 25 + "\t" 26 + complex_factor 27 + "\t" 28 + str(complex_factor_to_id) 29 + "\n" 30 )
File ~\AppData\Local\Programs\Python\Python312\Lib\encodings\cp1252.py:19, in IncrementalEncoder.encode(self, input, final) 18 def encode(self, input, final=False): ---> 19 return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u20b9' in position 115: character maps to
- Assistant
The error:
codeUnicodeEncodeError: 'charmap' codec can't encode character '\u20b9' in position 115: character maps to <undefined>happens because Python is trying to write a Unicode character ₹ (the Indian Rupee symbol, Unicode
\u20b9) to a text file using the default encoding of your system, which on Windows is usually'cp1252'.cp1252encoding cannot encode this character, causing the error. - User
convert text to xlsx
- Assistant
To convert text data into an Excel (.xlsx) file using Python, you can use pandas which makes this very straightforward.
Common scenarios:
- You have a text file (e.g., tab-separated
.txtor comma-separated.csv) and want to save it as.xlsx - You have (list/dict) and want to export it to
- You have a text file (e.g., tab-separated