I'm new to Python and this may sound basic but I have 2 files/class, task1.py and task2.py. I would like to access task1.py functions and data onto task2.py. In other words, whatever printed out that has been printed out by task1.py, I would like to take that output and make use of it, and in this case what I'm doing with that output is exporting it to a CSV file.
This is what my task1.py looks like:
def matchCountry():
userName = raw_input("Enter user's name: ")
with open('listOfUsers.json') as f:
data = json.load(f)
def getId(name):
for userId, v in data.items():
if v['Name'][0].lower() == name:
return userId;
id = getId(userName)
for k, v in data.items():
if any(x in data[id]['Country'] for x in v['Country']):
if v['Name'][0].lower() != userName.lower():
result = (v['Name'][0] + " : " + ", ".join(v['Country']))
print result
And this is what my task2.py looks like:
def exportCSV():
with open('output.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile, f, lineterminator='\n')
csvwriter.writerow(["Name", "Country"])
for k, v in data.items():
if any(x in data[id]['Country'] for x in v['Country']):
if v['Name'][0].lower() != userName.lower():
csvwriter.writerow([v['Name'][0], ", ".join(v['Country'])])
My JSON file for reference:
{
"user1":{
"Country":[
"China",
"USA",
"Nepal"
],
"Name":[
"Lisbon"
]
},
"user2":{
"Country":[
"Sweden",
"China",
"USA"
],
"Name":[
"Jade"
]
},
"user3":{
"Country":[
"India",
"China",
"USA"
],
"Name":[
"John"
]
}
}