how to import data from existing python file to write new csv file in python 3
For example this is my python file i.e. client.py, I want the data (ID, Ip_add, mac_address generated in this file to be exported in a new csv file.)
#client.py
import random
import socket
# funtion to define no. of switch
def switch_info(no_of_switch):
for number in range (1,no_of_switch+1):
print("ID:s",+number)
ip_add = '127.0.0.' + str(random.randint(0, 255))
print("IP_address:{}".format(ip_add))
mac_add = "-".join(map(str,(random.randint(0,255)for _ in range(4))))
print("Mac_address:{}".format(mac_add))
print('n')
switch_info(2)
python-3.x csv networking topology
add a comment |
For example this is my python file i.e. client.py, I want the data (ID, Ip_add, mac_address generated in this file to be exported in a new csv file.)
#client.py
import random
import socket
# funtion to define no. of switch
def switch_info(no_of_switch):
for number in range (1,no_of_switch+1):
print("ID:s",+number)
ip_add = '127.0.0.' + str(random.randint(0, 255))
print("IP_address:{}".format(ip_add))
mac_add = "-".join(map(str,(random.randint(0,255)for _ in range(4))))
print("Mac_address:{}".format(mac_add))
print('n')
switch_info(2)
python-3.x csv networking topology
I have to create a network topology which include switches, clients, gateway and router with (individual IDs, ipadd, macadd, status ) connected to each other, then i have to write a code to create a csv file which include information of every participant in the network...and at last i have to show the network via any visualizer.
– Shivangi Gupta
Nov 26 '18 at 6:30
add a comment |
For example this is my python file i.e. client.py, I want the data (ID, Ip_add, mac_address generated in this file to be exported in a new csv file.)
#client.py
import random
import socket
# funtion to define no. of switch
def switch_info(no_of_switch):
for number in range (1,no_of_switch+1):
print("ID:s",+number)
ip_add = '127.0.0.' + str(random.randint(0, 255))
print("IP_address:{}".format(ip_add))
mac_add = "-".join(map(str,(random.randint(0,255)for _ in range(4))))
print("Mac_address:{}".format(mac_add))
print('n')
switch_info(2)
python-3.x csv networking topology
For example this is my python file i.e. client.py, I want the data (ID, Ip_add, mac_address generated in this file to be exported in a new csv file.)
#client.py
import random
import socket
# funtion to define no. of switch
def switch_info(no_of_switch):
for number in range (1,no_of_switch+1):
print("ID:s",+number)
ip_add = '127.0.0.' + str(random.randint(0, 255))
print("IP_address:{}".format(ip_add))
mac_add = "-".join(map(str,(random.randint(0,255)for _ in range(4))))
print("Mac_address:{}".format(mac_add))
print('n')
switch_info(2)
python-3.x csv networking topology
python-3.x csv networking topology
edited Nov 26 '18 at 15:58
Robert Harvey♦
149k33276420
149k33276420
asked Nov 26 '18 at 3:47
Shivangi GuptaShivangi Gupta
11
11
I have to create a network topology which include switches, clients, gateway and router with (individual IDs, ipadd, macadd, status ) connected to each other, then i have to write a code to create a csv file which include information of every participant in the network...and at last i have to show the network via any visualizer.
– Shivangi Gupta
Nov 26 '18 at 6:30
add a comment |
I have to create a network topology which include switches, clients, gateway and router with (individual IDs, ipadd, macadd, status ) connected to each other, then i have to write a code to create a csv file which include information of every participant in the network...and at last i have to show the network via any visualizer.
– Shivangi Gupta
Nov 26 '18 at 6:30
I have to create a network topology which include switches, clients, gateway and router with (individual IDs, ipadd, macadd, status ) connected to each other, then i have to write a code to create a csv file which include information of every participant in the network...and at last i have to show the network via any visualizer.
– Shivangi Gupta
Nov 26 '18 at 6:30
I have to create a network topology which include switches, clients, gateway and router with (individual IDs, ipadd, macadd, status ) connected to each other, then i have to write a code to create a csv file which include information of every participant in the network...and at last i have to show the network via any visualizer.
– Shivangi Gupta
Nov 26 '18 at 6:30
add a comment |
1 Answer
1
active
oldest
votes
Something like this?
client.py
import numpy as np
ids =
ip_addr =
mac_addr =
def switch_info(no_of_switch):
for number in range (1,no_of_switch+1):
print("ID:s",+number)
ids.append(number)
ip_add = '127.0.0.' + str(np.random.randint(0, 255))
ip_addr.append(ip_add)
print("IP_address:{}".format(ip_add))
mac_add = "-".join(map(str,(np.random.randint(0,255)for _ in range(4))))
print("Mac_address:{}".format(mac_add))
mac_addr.append(mac_add)
print('n')
return ids, ip_addr, mac_addr
new_file.py
import pandas as pd
from client import switch_info
ids, ip_addr, mac_addr = switch_info(5)
df = pd.DataFrame()
df["Id"] = ids
df["IP"] = ip_addr
df["MAC"] = mac_addr
print(df)
df.to_csv("network.csv", index=False)
Output:
Id IP MAC
0 1 127.0.0.243 189-65-170-107
1 2 127.0.0.199 137-20-84-43
2 3 127.0.0.237 215-155-12-163
3 4 127.0.0.251 224-213-118-35
4 5 127.0.0.117 186-230-124-75
if i need to list the info of gateways and server files same as client_info to network.csv file, continuously like. c1 127.0.0.0 12-45-67 n c2 128.5.6.7 45-65-7-9 n s1 127.5.6.7 34-65-87 n gw1 134.5.6..7 43-56-76 s1-server1,c1-client1, gw1-gateway1. should i make different Dataframe object everytime?
– Shivangi Gupta
Nov 27 '18 at 3:51
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%2f53474552%2fhow-to-import-data-from-existing-python-file-to-write-new-csv-file-in-python-3%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
Something like this?
client.py
import numpy as np
ids =
ip_addr =
mac_addr =
def switch_info(no_of_switch):
for number in range (1,no_of_switch+1):
print("ID:s",+number)
ids.append(number)
ip_add = '127.0.0.' + str(np.random.randint(0, 255))
ip_addr.append(ip_add)
print("IP_address:{}".format(ip_add))
mac_add = "-".join(map(str,(np.random.randint(0,255)for _ in range(4))))
print("Mac_address:{}".format(mac_add))
mac_addr.append(mac_add)
print('n')
return ids, ip_addr, mac_addr
new_file.py
import pandas as pd
from client import switch_info
ids, ip_addr, mac_addr = switch_info(5)
df = pd.DataFrame()
df["Id"] = ids
df["IP"] = ip_addr
df["MAC"] = mac_addr
print(df)
df.to_csv("network.csv", index=False)
Output:
Id IP MAC
0 1 127.0.0.243 189-65-170-107
1 2 127.0.0.199 137-20-84-43
2 3 127.0.0.237 215-155-12-163
3 4 127.0.0.251 224-213-118-35
4 5 127.0.0.117 186-230-124-75
if i need to list the info of gateways and server files same as client_info to network.csv file, continuously like. c1 127.0.0.0 12-45-67 n c2 128.5.6.7 45-65-7-9 n s1 127.5.6.7 34-65-87 n gw1 134.5.6..7 43-56-76 s1-server1,c1-client1, gw1-gateway1. should i make different Dataframe object everytime?
– Shivangi Gupta
Nov 27 '18 at 3:51
add a comment |
Something like this?
client.py
import numpy as np
ids =
ip_addr =
mac_addr =
def switch_info(no_of_switch):
for number in range (1,no_of_switch+1):
print("ID:s",+number)
ids.append(number)
ip_add = '127.0.0.' + str(np.random.randint(0, 255))
ip_addr.append(ip_add)
print("IP_address:{}".format(ip_add))
mac_add = "-".join(map(str,(np.random.randint(0,255)for _ in range(4))))
print("Mac_address:{}".format(mac_add))
mac_addr.append(mac_add)
print('n')
return ids, ip_addr, mac_addr
new_file.py
import pandas as pd
from client import switch_info
ids, ip_addr, mac_addr = switch_info(5)
df = pd.DataFrame()
df["Id"] = ids
df["IP"] = ip_addr
df["MAC"] = mac_addr
print(df)
df.to_csv("network.csv", index=False)
Output:
Id IP MAC
0 1 127.0.0.243 189-65-170-107
1 2 127.0.0.199 137-20-84-43
2 3 127.0.0.237 215-155-12-163
3 4 127.0.0.251 224-213-118-35
4 5 127.0.0.117 186-230-124-75
if i need to list the info of gateways and server files same as client_info to network.csv file, continuously like. c1 127.0.0.0 12-45-67 n c2 128.5.6.7 45-65-7-9 n s1 127.5.6.7 34-65-87 n gw1 134.5.6..7 43-56-76 s1-server1,c1-client1, gw1-gateway1. should i make different Dataframe object everytime?
– Shivangi Gupta
Nov 27 '18 at 3:51
add a comment |
Something like this?
client.py
import numpy as np
ids =
ip_addr =
mac_addr =
def switch_info(no_of_switch):
for number in range (1,no_of_switch+1):
print("ID:s",+number)
ids.append(number)
ip_add = '127.0.0.' + str(np.random.randint(0, 255))
ip_addr.append(ip_add)
print("IP_address:{}".format(ip_add))
mac_add = "-".join(map(str,(np.random.randint(0,255)for _ in range(4))))
print("Mac_address:{}".format(mac_add))
mac_addr.append(mac_add)
print('n')
return ids, ip_addr, mac_addr
new_file.py
import pandas as pd
from client import switch_info
ids, ip_addr, mac_addr = switch_info(5)
df = pd.DataFrame()
df["Id"] = ids
df["IP"] = ip_addr
df["MAC"] = mac_addr
print(df)
df.to_csv("network.csv", index=False)
Output:
Id IP MAC
0 1 127.0.0.243 189-65-170-107
1 2 127.0.0.199 137-20-84-43
2 3 127.0.0.237 215-155-12-163
3 4 127.0.0.251 224-213-118-35
4 5 127.0.0.117 186-230-124-75
Something like this?
client.py
import numpy as np
ids =
ip_addr =
mac_addr =
def switch_info(no_of_switch):
for number in range (1,no_of_switch+1):
print("ID:s",+number)
ids.append(number)
ip_add = '127.0.0.' + str(np.random.randint(0, 255))
ip_addr.append(ip_add)
print("IP_address:{}".format(ip_add))
mac_add = "-".join(map(str,(np.random.randint(0,255)for _ in range(4))))
print("Mac_address:{}".format(mac_add))
mac_addr.append(mac_add)
print('n')
return ids, ip_addr, mac_addr
new_file.py
import pandas as pd
from client import switch_info
ids, ip_addr, mac_addr = switch_info(5)
df = pd.DataFrame()
df["Id"] = ids
df["IP"] = ip_addr
df["MAC"] = mac_addr
print(df)
df.to_csv("network.csv", index=False)
Output:
Id IP MAC
0 1 127.0.0.243 189-65-170-107
1 2 127.0.0.199 137-20-84-43
2 3 127.0.0.237 215-155-12-163
3 4 127.0.0.251 224-213-118-35
4 5 127.0.0.117 186-230-124-75
edited Nov 26 '18 at 15:52
answered Nov 26 '18 at 7:12
Srce CdeSrce Cde
1,184612
1,184612
if i need to list the info of gateways and server files same as client_info to network.csv file, continuously like. c1 127.0.0.0 12-45-67 n c2 128.5.6.7 45-65-7-9 n s1 127.5.6.7 34-65-87 n gw1 134.5.6..7 43-56-76 s1-server1,c1-client1, gw1-gateway1. should i make different Dataframe object everytime?
– Shivangi Gupta
Nov 27 '18 at 3:51
add a comment |
if i need to list the info of gateways and server files same as client_info to network.csv file, continuously like. c1 127.0.0.0 12-45-67 n c2 128.5.6.7 45-65-7-9 n s1 127.5.6.7 34-65-87 n gw1 134.5.6..7 43-56-76 s1-server1,c1-client1, gw1-gateway1. should i make different Dataframe object everytime?
– Shivangi Gupta
Nov 27 '18 at 3:51
if i need to list the info of gateways and server files same as client_info to network.csv file, continuously like. c1 127.0.0.0 12-45-67 n c2 128.5.6.7 45-65-7-9 n s1 127.5.6.7 34-65-87 n gw1 134.5.6..7 43-56-76 s1-server1,c1-client1, gw1-gateway1. should i make different Dataframe object everytime?
– Shivangi Gupta
Nov 27 '18 at 3:51
if i need to list the info of gateways and server files same as client_info to network.csv file, continuously like. c1 127.0.0.0 12-45-67 n c2 128.5.6.7 45-65-7-9 n s1 127.5.6.7 34-65-87 n gw1 134.5.6..7 43-56-76 s1-server1,c1-client1, gw1-gateway1. should i make different Dataframe object everytime?
– Shivangi Gupta
Nov 27 '18 at 3:51
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%2f53474552%2fhow-to-import-data-from-existing-python-file-to-write-new-csv-file-in-python-3%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
I have to create a network topology which include switches, clients, gateway and router with (individual IDs, ipadd, macadd, status ) connected to each other, then i have to write a code to create a csv file which include information of every participant in the network...and at last i have to show the network via any visualizer.
– Shivangi Gupta
Nov 26 '18 at 6:30