-4

I was working on my project pandas data frame to csv file. The rows are created based on the input this is what I mean :3 so the issue is that the 1st rows keeps on changing as I loop the script the results :3. Also need help for looping the script

from ast import Break
import pandas as pd
import socket
import webbrowser,time

def introduction():
    msg = '''
         This is a python project made by Abhay R 
         of ************************************.
      
         This project is based on how internet 
          service providers collect 
          data from clients\n\n\n\n\n'''
    for x in msg:
         print(x, end='')
         time.sleep(0.002)
          wait = input('Press any key to continue.....')

introduction()
print()
  
def begin():
      url=input("ENTER YOUR WEB HERE:")
      web=webbrowser.open_new_tab(url) 


      weblink = url


      Domain_name = ""


      x = weblink.split("/")



      if(x[0] == "https:" or x[0] == "http:"):

             x = x[2].split(".")



      else:

             x = x[0].split(".")


     if(len(x) == 2):

             Domain_name = x[0]


     else:

             Domain_name = x[1]

     print("Domain Name Is:",Domain_name)        

     dns=socket.gethostbyname(weblink)
     print("DNS:",dns)


     domain=Domain_name
     dns=dns
     data={"WEBSITE":weblink,"DOMAIN":domain,"DNS":dns}
     df1=pd.DataFrame(data,index=[1])
     df1.to_csv("C:\\Users\\stdev\\Desktop\\IP PROJECT\\Book 9.csv")



     cvv=pd.read_csv("C:\\Users\\stdev\\Desktop\\IP PROJECT\\Book 9.csv")
     print(cvv)

     begin()

Hope my question is understandable and anyone can help me with this soon as possible.

Abhay
  • 46
  • 4
  • also is there any other way to loop it rather than using begin() multiple time – Abhay Oct 19 '22 at 14:03
  • You are overwriting the same file again and again with only one row. What you should do is append to a .csv file instead of creating a new one which deletes your previous data. – LazyClown Oct 19 '22 at 14:07
  • @LazyClown can you show me an example. i added this – Abhay Oct 19 '22 at 14:21
  • Something like [this](https://stackoverflow.com/questions/17530542/how-to-add-pandas-data-to-an-existing-csv-file) – LazyClown Oct 19 '22 at 14:50
  • 1
    always put code, data and full error message as text (not screenshot, not link) in question (not in comment). It will be more readable and easier to use in answer (simpler to select and copy), and more people will see it - so more people can help you. – furas Oct 19 '22 at 15:23
  • Put code with your query to make it easier for us understand the problem. – rafathasan Oct 19 '22 at 16:31
  • @rafathasan ok I've put the code in query – Abhay Oct 19 '22 at 17:38
  • edit question and put code with correct indentations. Current code is useless. – furas Oct 19 '22 at 18:15
  • if you want to add new element to existing file then you have two options: (1) load all data to python, update data in python, write all data again to file - and this method is useful for some type of data like Excel, XML. (2) write in `append mode` - `to_cvs(..., mode="a")` – furas Oct 19 '22 at 18:18

1 Answers1

0

The issue have been solved thankyou @furas

import pandas as pd
import socket
import webbrowser,time

def introduction():
      msg = '''
           This is a python project made by Abhay R 
           of ************************************.
      
           This project is based on how internet service providers collect 
           data from clients\n\n\n\n\n'''
      for x in msg:
          print(x, end='')
          time.sleep(0.002)
          wait = input('Press any key to continue.....')

introduction()
print()

def begin():
    url=input("ENTER YOUR WEB HERE:")
    web=webbrowser.open_new_tab(url) 


    weblink = url


    Domain_name = ""


    x = weblink.split("/")



    if(x[0] == "https:" or x[0] == "http:"):

          x = x[2].split(".")



    else:

          x = x[0].split(".")


    if(len(x) == 2):

          Domain_name = x[0]


    else:

          Domain_name = x[1]

    print("Domain Name Is:",Domain_name)        

    dns=socket.gethostbyname(weblink)
    print("DNS:",dns)


   domain=Domain_name
   dns=dns
   data={"WEBSITE":weblink,"DOMAIN":domain,"DNS":dns}
   def alt():
       df=pd.DataFrame(data,index=[1])
       df.to_csv("data.csv",index=False,mode='a',header=False)
       print(df)
   alt()    

begin()

begin()
Abhay
  • 46
  • 4