Python Forum
How to read module/class from list of strings?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to read module/class from list of strings?
#1
How can I get 'string_wrong' to print out what 'correct' is printing out?

from google.cloud import bigquery

corret = [bigquery.SchemaField("field1","STRING", mode="NULLABLE"),
bigquery.SchemaField("field2","STRING", mode="NULLABLE")]

string_wrong = ['bigquery.SchemaField("field1","STRING", mode="NULLABLE")',
'bigquery.SchemaField("field2","STRING", mode="NULLABLE")']

print(corret)
print(string_wrong)
correct output:
Output:
[SchemaField('field1', 'STRING', 'NULLABLE', None, None, (), None), SchemaField('field2', 'STRING', 'NULLABLE', None, None, (), None)]
string_wrong output:
Output:
['bigquery.SchemaField("field1","STRING", mode="NULLABLE")', 'bigquery.SchemaField("field2","STRING", mode="NULLABLE")']
Reply
#2
This is related to your other post? https://python-forum.io/thread-40853.html

You cannot "convert" string_wrong into correct. string_wrong is a str and correct is list containing the return value of a function call. What you could do is evaluate the python expresssion in string_wrong. It might return the same result.
correct = [5 + 2]
wrong = "[5 + 2]"
resolved = eval(wrong)
print(correct)
print(wrong)
print(resolved)
Output:
[7] [5 + 2] [7]
Be aware that eval(expression) executes the expression, any valid expression. The expression could add two numbers, or it could format your hard drive. Only use eval() if you know what expressions will be evaluated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyYAML read list of int zisco 2 328 Apr-02-2024, 12:36 PM
Last Post: zisco
  Trying to understand strings and lists of strings Konstantin23 2 779 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  problem in using int() with a list of strings akbarza 4 721 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,559 May-01-2023, 09:06 PM
Last Post: deanhystad
  Help with Logical error processing List of strings dmc8300 3 1,097 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  My code displays too much output when importing class from a module lil_e 4 1,171 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  read a text file, find all integers, append to list oldtrafford 12 3,622 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
Question How to move a class to a custom module? python300 4 1,578 Mar-08-2022, 09:19 PM
Last Post: python300
  Splitting strings in list of strings jesse68 3 1,782 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,935 Oct-03-2021, 05:16 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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