Python 2.7 load and edit list from another python file
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a python file with a lot of code and lists. I need to add column in particular list.
My questions are - how can I load particular list from .py file? And, how can I add element into particular list?
Here in my code:
import os, datetime, json
login1 = os.environ["login"].split('')[1].strip()
login = login1.split('.')[0].strip()
USERID = str(os.environ['USERID'])
header = login + USERID + '.json'
with open("d:\python\monitor.py", "r") as infile:
data = infile.readlines()
#here I need to load a "dev_personal_files" list and append additional element
Content of monitor.py file
#some python code
dev_personal_files = [
'Yura.json',
'Sasha.json'
]
staging_files = [
#'ple.json',
'retailReleaseServer.json',
'topaz.json',
'ple2.json',
#'klub.json',
'gaabtMX.json'
]
staging_files2 = [
'retailDemo.json',
'resort.json',
'jhnkljkl.json',
'hbjk,nm,.json',
'bnbnj,jnk,.json'
]
#some python code
What I want to add into "dev_personal_files" list (list within monitor.py file):
dev_personal_files = [
'NEWRECORD.json',
'Yura.json',
'Sasha.json'
]
python python-2.7
add a comment |
I have a python file with a lot of code and lists. I need to add column in particular list.
My questions are - how can I load particular list from .py file? And, how can I add element into particular list?
Here in my code:
import os, datetime, json
login1 = os.environ["login"].split('')[1].strip()
login = login1.split('.')[0].strip()
USERID = str(os.environ['USERID'])
header = login + USERID + '.json'
with open("d:\python\monitor.py", "r") as infile:
data = infile.readlines()
#here I need to load a "dev_personal_files" list and append additional element
Content of monitor.py file
#some python code
dev_personal_files = [
'Yura.json',
'Sasha.json'
]
staging_files = [
#'ple.json',
'retailReleaseServer.json',
'topaz.json',
'ple2.json',
#'klub.json',
'gaabtMX.json'
]
staging_files2 = [
'retailDemo.json',
'resort.json',
'jhnkljkl.json',
'hbjk,nm,.json',
'bnbnj,jnk,.json'
]
#some python code
What I want to add into "dev_personal_files" list (list within monitor.py file):
dev_personal_files = [
'NEWRECORD.json',
'Yura.json',
'Sasha.json'
]
python python-2.7
What is the format of data? I assume you already tried loading the second file as a infile with a different name and then appending it to the first list with something likedata1.list1.extend(data2.list2)
?
– kushy
Jan 4 at 13:34
@kushy format - python file with bunch of lists and some python code. Can you give me an example, how to load list from such file?
– Vasiliy Vegas
Jan 4 at 13:44
I don't know about the infile stuff. I thought you knew about it since you were using it. Usually, if you put the monitor.py and dev_personal_files.py in the same directory as your main file, you can just import them, as the new answer just now states.
– kushy
Jan 4 at 13:55
@kushy - dev_personal_files - its a list within monitor.py file
– Vasiliy Vegas
Jan 4 at 14:05
1
Then where is the additional info stored that you wanted to add to the dev_personal_files list? Or were you going to add it to the list during runtime and then save the list again to monitor.py? If yes, you should rather use csv-files, config-files some other data format to save those lists.
– kushy
Jan 4 at 14:15
add a comment |
I have a python file with a lot of code and lists. I need to add column in particular list.
My questions are - how can I load particular list from .py file? And, how can I add element into particular list?
Here in my code:
import os, datetime, json
login1 = os.environ["login"].split('')[1].strip()
login = login1.split('.')[0].strip()
USERID = str(os.environ['USERID'])
header = login + USERID + '.json'
with open("d:\python\monitor.py", "r") as infile:
data = infile.readlines()
#here I need to load a "dev_personal_files" list and append additional element
Content of monitor.py file
#some python code
dev_personal_files = [
'Yura.json',
'Sasha.json'
]
staging_files = [
#'ple.json',
'retailReleaseServer.json',
'topaz.json',
'ple2.json',
#'klub.json',
'gaabtMX.json'
]
staging_files2 = [
'retailDemo.json',
'resort.json',
'jhnkljkl.json',
'hbjk,nm,.json',
'bnbnj,jnk,.json'
]
#some python code
What I want to add into "dev_personal_files" list (list within monitor.py file):
dev_personal_files = [
'NEWRECORD.json',
'Yura.json',
'Sasha.json'
]
python python-2.7
I have a python file with a lot of code and lists. I need to add column in particular list.
My questions are - how can I load particular list from .py file? And, how can I add element into particular list?
Here in my code:
import os, datetime, json
login1 = os.environ["login"].split('')[1].strip()
login = login1.split('.')[0].strip()
USERID = str(os.environ['USERID'])
header = login + USERID + '.json'
with open("d:\python\monitor.py", "r") as infile:
data = infile.readlines()
#here I need to load a "dev_personal_files" list and append additional element
Content of monitor.py file
#some python code
dev_personal_files = [
'Yura.json',
'Sasha.json'
]
staging_files = [
#'ple.json',
'retailReleaseServer.json',
'topaz.json',
'ple2.json',
#'klub.json',
'gaabtMX.json'
]
staging_files2 = [
'retailDemo.json',
'resort.json',
'jhnkljkl.json',
'hbjk,nm,.json',
'bnbnj,jnk,.json'
]
#some python code
What I want to add into "dev_personal_files" list (list within monitor.py file):
dev_personal_files = [
'NEWRECORD.json',
'Yura.json',
'Sasha.json'
]
python python-2.7
python python-2.7
edited Jan 4 at 14:06
Vasiliy Vegas
asked Jan 4 at 13:29
Vasiliy VegasVasiliy Vegas
414216
414216
What is the format of data? I assume you already tried loading the second file as a infile with a different name and then appending it to the first list with something likedata1.list1.extend(data2.list2)
?
– kushy
Jan 4 at 13:34
@kushy format - python file with bunch of lists and some python code. Can you give me an example, how to load list from such file?
– Vasiliy Vegas
Jan 4 at 13:44
I don't know about the infile stuff. I thought you knew about it since you were using it. Usually, if you put the monitor.py and dev_personal_files.py in the same directory as your main file, you can just import them, as the new answer just now states.
– kushy
Jan 4 at 13:55
@kushy - dev_personal_files - its a list within monitor.py file
– Vasiliy Vegas
Jan 4 at 14:05
1
Then where is the additional info stored that you wanted to add to the dev_personal_files list? Or were you going to add it to the list during runtime and then save the list again to monitor.py? If yes, you should rather use csv-files, config-files some other data format to save those lists.
– kushy
Jan 4 at 14:15
add a comment |
What is the format of data? I assume you already tried loading the second file as a infile with a different name and then appending it to the first list with something likedata1.list1.extend(data2.list2)
?
– kushy
Jan 4 at 13:34
@kushy format - python file with bunch of lists and some python code. Can you give me an example, how to load list from such file?
– Vasiliy Vegas
Jan 4 at 13:44
I don't know about the infile stuff. I thought you knew about it since you were using it. Usually, if you put the monitor.py and dev_personal_files.py in the same directory as your main file, you can just import them, as the new answer just now states.
– kushy
Jan 4 at 13:55
@kushy - dev_personal_files - its a list within monitor.py file
– Vasiliy Vegas
Jan 4 at 14:05
1
Then where is the additional info stored that you wanted to add to the dev_personal_files list? Or were you going to add it to the list during runtime and then save the list again to monitor.py? If yes, you should rather use csv-files, config-files some other data format to save those lists.
– kushy
Jan 4 at 14:15
What is the format of data? I assume you already tried loading the second file as a infile with a different name and then appending it to the first list with something like
data1.list1.extend(data2.list2)
?– kushy
Jan 4 at 13:34
What is the format of data? I assume you already tried loading the second file as a infile with a different name and then appending it to the first list with something like
data1.list1.extend(data2.list2)
?– kushy
Jan 4 at 13:34
@kushy format - python file with bunch of lists and some python code. Can you give me an example, how to load list from such file?
– Vasiliy Vegas
Jan 4 at 13:44
@kushy format - python file with bunch of lists and some python code. Can you give me an example, how to load list from such file?
– Vasiliy Vegas
Jan 4 at 13:44
I don't know about the infile stuff. I thought you knew about it since you were using it. Usually, if you put the monitor.py and dev_personal_files.py in the same directory as your main file, you can just import them, as the new answer just now states.
– kushy
Jan 4 at 13:55
I don't know about the infile stuff. I thought you knew about it since you were using it. Usually, if you put the monitor.py and dev_personal_files.py in the same directory as your main file, you can just import them, as the new answer just now states.
– kushy
Jan 4 at 13:55
@kushy - dev_personal_files - its a list within monitor.py file
– Vasiliy Vegas
Jan 4 at 14:05
@kushy - dev_personal_files - its a list within monitor.py file
– Vasiliy Vegas
Jan 4 at 14:05
1
1
Then where is the additional info stored that you wanted to add to the dev_personal_files list? Or were you going to add it to the list during runtime and then save the list again to monitor.py? If yes, you should rather use csv-files, config-files some other data format to save those lists.
– kushy
Jan 4 at 14:15
Then where is the additional info stored that you wanted to add to the dev_personal_files list? Or were you going to add it to the list during runtime and then save the list again to monitor.py? If yes, you should rather use csv-files, config-files some other data format to save those lists.
– kushy
Jan 4 at 14:15
add a comment |
1 Answer
1
active
oldest
votes
Getting a list (or any object) from a .py
file is done by importing the file:
import mypyfile # <- leave off the .py
importedlist = mypyfile.dev_personal_files
Or you can just import the object itself:
from mypyfile import dev_personal_files
dev_personal_files.extend(my_list_of_extra_items)
However the changes to the list will be lost after you end your python session.
If you want to permanently store changes to the list, save it in a format like json, not in a py file.
import json
# Read data from the file
with open('myfile.json') as f:
my_list = json.load(f)
# Add an item to your list
my_list.append('foo')
# Save data to the file
with open('myfile.json', 'w') as f:
json.dump(my_list)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039894%2fpython-2-7-load-and-edit-list-from-another-python-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Getting a list (or any object) from a .py
file is done by importing the file:
import mypyfile # <- leave off the .py
importedlist = mypyfile.dev_personal_files
Or you can just import the object itself:
from mypyfile import dev_personal_files
dev_personal_files.extend(my_list_of_extra_items)
However the changes to the list will be lost after you end your python session.
If you want to permanently store changes to the list, save it in a format like json, not in a py file.
import json
# Read data from the file
with open('myfile.json') as f:
my_list = json.load(f)
# Add an item to your list
my_list.append('foo')
# Save data to the file
with open('myfile.json', 'w') as f:
json.dump(my_list)
add a comment |
Getting a list (or any object) from a .py
file is done by importing the file:
import mypyfile # <- leave off the .py
importedlist = mypyfile.dev_personal_files
Or you can just import the object itself:
from mypyfile import dev_personal_files
dev_personal_files.extend(my_list_of_extra_items)
However the changes to the list will be lost after you end your python session.
If you want to permanently store changes to the list, save it in a format like json, not in a py file.
import json
# Read data from the file
with open('myfile.json') as f:
my_list = json.load(f)
# Add an item to your list
my_list.append('foo')
# Save data to the file
with open('myfile.json', 'w') as f:
json.dump(my_list)
add a comment |
Getting a list (or any object) from a .py
file is done by importing the file:
import mypyfile # <- leave off the .py
importedlist = mypyfile.dev_personal_files
Or you can just import the object itself:
from mypyfile import dev_personal_files
dev_personal_files.extend(my_list_of_extra_items)
However the changes to the list will be lost after you end your python session.
If you want to permanently store changes to the list, save it in a format like json, not in a py file.
import json
# Read data from the file
with open('myfile.json') as f:
my_list = json.load(f)
# Add an item to your list
my_list.append('foo')
# Save data to the file
with open('myfile.json', 'w') as f:
json.dump(my_list)
Getting a list (or any object) from a .py
file is done by importing the file:
import mypyfile # <- leave off the .py
importedlist = mypyfile.dev_personal_files
Or you can just import the object itself:
from mypyfile import dev_personal_files
dev_personal_files.extend(my_list_of_extra_items)
However the changes to the list will be lost after you end your python session.
If you want to permanently store changes to the list, save it in a format like json, not in a py file.
import json
# Read data from the file
with open('myfile.json') as f:
my_list = json.load(f)
# Add an item to your list
my_list.append('foo')
# Save data to the file
with open('myfile.json', 'w') as f:
json.dump(my_list)
answered Jan 4 at 13:54
snakecharmerbsnakecharmerb
12.2k42553
12.2k42553
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039894%2fpython-2-7-load-and-edit-list-from-another-python-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the format of data? I assume you already tried loading the second file as a infile with a different name and then appending it to the first list with something like
data1.list1.extend(data2.list2)
?– kushy
Jan 4 at 13:34
@kushy format - python file with bunch of lists and some python code. Can you give me an example, how to load list from such file?
– Vasiliy Vegas
Jan 4 at 13:44
I don't know about the infile stuff. I thought you knew about it since you were using it. Usually, if you put the monitor.py and dev_personal_files.py in the same directory as your main file, you can just import them, as the new answer just now states.
– kushy
Jan 4 at 13:55
@kushy - dev_personal_files - its a list within monitor.py file
– Vasiliy Vegas
Jan 4 at 14:05
1
Then where is the additional info stored that you wanted to add to the dev_personal_files list? Or were you going to add it to the list during runtime and then save the list again to monitor.py? If yes, you should rather use csv-files, config-files some other data format to save those lists.
– kushy
Jan 4 at 14:15