-1

i have a script with few inputs as following :

wep = input("Q (0-5): ")
side = input("1 for defender, 2 for attacker: ")
Hit = input ("1 for hit,2 for berserk: ")
food = input("type the Q (0-5): ")
gift = input("type the Q (0-5): ")
work = input("region id: ")

how can i import mentioned inputs from a text file or csv file?

Abolfazl
  • 23
  • 4
  • 1
    Assuming you are using python3, you can use this module https://docs.python.org/3.8/library/csv.html to read from csv. Otherwise there are many answers on SO that show you how to read from text files e.g. https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list – Anthony Kong Dec 18 '19 at 07:46
  • @AnthonyKong thanks for your help, so i just use lists as input and index each element from list for my inputs right ? im really new to python :) like list=[input] and for example, wep = list[0] right ? – Abolfazl Dec 18 '19 at 07:52
  • Is using json an option? – marxmacher Dec 18 '19 at 08:09

1 Answers1

1

I would suggest using JSON file for the input ( yes it perfectly fine to use a simple text file with a list of values or a file which has a dictionary inside).

example JSON:

{
  "wep": "0",
  "side": "1",
  "food": "0"
}

Code to import json file

import json
with open("some.json") as data_file:
data = json.load(data_file)
print(data)
wep = data["wep"]
print(wep)

Output:

{'wep': '0', 'side': '1', 'food': '0'}
0
marxmacher
  • 591
  • 3
  • 20
  • hey man, this is perfect! now i can organize data and knowing each value is for what input. thanks a lot – Abolfazl Dec 18 '19 at 08:25