Thread: python CSV

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    19
    Rep Power
    0

    python CSV


    I need to download the printed thread to a CSV file
    how do I do it?

     
    Code:
    from urllib.request import urlopen as uReq
    from bs4 import BeautifulSoup as soup
    import urllib
    
    my_url ='http://www.telesport.co.il/%D7%90%D7%96%D7%95%D7%A8%20%D7%95%D7%95%D7%99%D7%A0%D7%A8'
    uClient = uReq(my_url)
    page_html = uClient.read()
    uClient.close()
    page_soup = soup(page_html, "html.parser")
    
    cc = page_soup.findAll("td",{"class":"th_td_WinnerteamsAndBetType"})
    
    for i in range(10):
        print(cc[0+i].text, i)
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,893
    Rep Power
    509
    The easiest way would be to change the last few lines a bit, rather than fooling around with the csv module. Although this looks funky on my display.
    Code:
    from urllib.request import urlopen as uReq
    from bs4 import BeautifulSoup as soup
    import urllib
    
    my_url ='http://www.telesport.co.il/%D7%90%D7%96%D7%95%D7%A8%20%D7%95%D7%95%D7%99%D7%A0%D7%A8'
    uClient = uReq(my_url)
    page_html = uClient.read()
    uClient.close()
    page_soup = soup(page_html, "html.parser")
    
    cc = page_soup.findAll("td",{"class":"th_td_WinnerteamsAndBetType"})
    
    for i in range(10):
        fields = cc[0+i].text.split()
        print('{},{},{}\n'.format(fields[0].strip(), ' '.join(fields[1:]).strip(), i))
    You might need two \n's. You'd run this by directing the output to a file.
    $ python3 superduperprogram.py > winners.csv
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    19
    Rep Power
    0
    Hi hello first of all thank you
    Perhaps you didn't understand my question
    I need the output out (test.text file or test.csv)
    Need to write the output into the file and I always get an error





    Code:
    from urllib.request import urlopen as uReq
    from bs4 import BeautifulSoup as soup
    import urllib
    
    import sys
    
    Sys_st = sys.stdout               # sys.stdout out                            
    Exit_txt = open('out.txt', 'w')      # sys out                       
    sys.stdout = Exit_txt
    
    my_url ='http://www.telesport.co.il/%D7%90%D7%96%D7%95%D7%A8%20%D7%95%D7%95%D7%99%D7%A0%D7%A8'
    uClient = uReq(my_url)
    page_html = uClient.read()
    uClient.close()
    page_soup = soup(page_html, "html.parser")
    
    cc = page_soup.findAll("td",{"class":"th_td_WinnerteamsAndBetType"})
    
    #for i in range(10):
    #    print(cc[0+i].text)
    
    for i in range(10):
        fields = cc[0+i].text.split()
        print('{},{},{}\n'.format(fields[0].strip(), ' '.join(fields[1:]).strip(), i))
    
    sys.stdout = Sys_st
    Exit_txt.close()

    Traceback (most recent call last):
      File "/Users/davidcohen/Desktop/111ds.py", line 28, in <module>
        print('{},{},{}\n'.format(fields[0].strip(), ' '.join(fields[1:]).strip(), i))
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-6: ordinal not in range(128)
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,893
    Rep Power
    509
    run the program using python3
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    19
    Rep Power
    0
    The program is powered by Python 3.6.2
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    19
    Rep Power
    0
    Hey I was able to extract the file
    Getting a result

    Code:
    #/Users/davidcohen/Desktop
    # -*- coding: utf-8 -*-
    
    
    from urllib.request import urlopen as uReq
    from bs4 import BeautifulSoup as soup
    import urllib
    import sys
    
    
    
    Sys_st = sys.stdout               # sys.stdout out                            
    Exit_txt = open('out.txt', 'w')      # sys out                       
    sys.stdout = Exit_txt
    
    
    my_url ='http://www.telesport.co.il/%D7%90%D7%96%D7%95%D7%A8%20%D7%95%D7%95%D7%99%D7%A0%D7%A8'
    uClient = uReq(my_url)
    page_html = uClient.read()
    uClient.close()
    page_soup = soup(page_html, "html.parser")
    
    
    
    
    cc = page_soup.findAll("td",{"class":"th_td_WinnerteamsAndBetType"})
    
    
    for i in range(10):
        print(cc[0+i].text.encode('utf-8'))
    
    
    
    
    #for i in range(10):
    #    fields = cc[0+i].text.split()
    #    print('{},{},{}\n'.format(fields[0].strip(), ' '.join(fields[1:]).strip(), i))
        
    sys.stdout = Sys_st
    Exit_txt.close()
    out.txt
     b'\xd7\x94\xd7\x95\xd7\xa0\xd7\x93\xd7\x95\xd7\xa8\xd7\xa1\xc2\xa0-\xc2\xa0\xd7\x90\xd7\x95\xd7\xa1\xd7\x98\xd7\xa8\xd7\x9c\xd7\x99\xd7\x94'
    b'\xd7\x94\xd7\x95\xd7\xa0\xd7\x93\xd7\x95\xd7\xa8\xd7\xa1\xc2\xa0-\xc2\xa0\xd7\x90\xd7\x95\xd7\xa1\xd7\x98\xd7\xa8\xd7\x9c\xd7\x99\xd7\x94\xc2\xa0(\xe2\x80\x8e+1)'
    b'\xd7\xa1\xd7\x9a \xd7\x94\xd7\x9b\xd7\x9c \xd7\xa9\xd7\xa2\xd7\xa8\xd7\x99\xd7\x9d \xd7\x98\xd7\x95\xd7\x95\xd7\x97\xd7\x99\xd7\x9d (2-3)\xc2\xa0\xd7\x94\xd7\x95\xd7\xa0\xd7\x93\xd7\x95\xd7\xa8\xd7\xa1\xc2\xa0-\xc2\xa0\xd7\x90\xd7\x95\xd7\xa1\xd7\x98\xd7\xa8\xd7\x9c\xd7\x99\xd7\x94'
    b'\xd7\xa1\xd7\x9a \xd7\x94\xd7\x9b\xd7\x9c \xd7\xa7\xd7\xa8\xd7\xa0\xd7\x95\xd7\xaa (9-11)\xc2\xa0\xd7\x94\xd7\x95\xd7\xa0\xd7\x93\xd7\x95\xd7\xa8\xd7\xa1\xc2\xa0-\xc2\xa0\xd7\x90\xd7\x95\xd7\xa1\xd7\x98\xd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7\x90\xd7\xa8\xd7\x9c\xd7\x95\xd7\x98 \xd7\x94\xd7\x95\xd7\xa8\xd7\xa0\xd7\x98\xd7\xa1\xc2\xa0(\xe2\x80\x8e+3.5)'
    b'\xd7\xa9\xd7\x99\xd7\xa7\xd7\x92\xd7\x95 \xd7\x91\xd7\x95\xd7\x9c\xd7\xa1\xc2\xa0(\xe2\x80\x8e+3)\xc2\xa0-\xc2\xa0\xd7\x90\xd7\x99\xd7\xa0\xd7\x93\xd7\x99\xd7\x90\xd7\xa0\xd7\x94 \xd7\xa4\xd7\x99\xd7\x99\xd7\xa1\xd7\xa8\xd7\xa1'
  12. #7
  13. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,893
    Rep Power
    509
    I didn't observe that issue. Try decode.
    Code:
    >>> b'\xd7\x94\xd7\x95\xd7\xa0\xd7\x93\xd7\x95\xd7\xa8\xd7\xa1\xc2\xa0-\xc2\xa0\xd7\x90\xd7\x95\xd7\xa1\xd7\x98\xd7\xa8\xd7\x9c\xd7\x99\xd7\x94'.decode('utf8')
    'הונדורס\xa0-\xa0אוסטרליה'
    bytestring.decode('utf8')
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    19
    Rep Power
    0
    Perhaps there is another way
    This way is not right for me I'm looking for something else


    Example:

    General Diaz - Olympia Ace Netanyahu progress
    Bocaramanga - Riongru Agilas
    Washington Wizards z - Atlanta Alves (+9)
    Washington Wizards z - Atlanta Album (+10.5)
    New Orleans Flex - Los Angeles Clippers (+4)
    New Orleans Flex - Los Angeles Clippers (+6.5)
    Houston Rocket z - Memphis Grizzly (+7)
    Houston Rocket z - Memphis Grizzly (+6.5)
    New York Knicks - Sacramento Kings (+7)
    New York Knicks - Sacramento Kings (+6.5)

    Without deciphering just another more convenient way
  16. #9
  17. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,893
    Rep Power
    509
    I doubt my ability to find a general rule transforming post 6 into post 8.
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    19
    Rep Power
    0
    Anyway thank you friend
    I initially wanted to go in the direction of a csv file but I do not get along with understanding this video

    https://youtu.be/XQgXKtPSzUI
  20. #11
  21. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,893
    Rep Power
    509
    Perhaps start with an English web site. I think you're doing fine otherwise.
    https://www.racingpost.com/
    [code]Code tags[/code] are essential for python code and Makefiles!
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    19
    Rep Power
    0
    Yes in English I'm sure it works
    But in Hebrew there is also a way to extract the output to a file
    I have to memorize the language and hope to find a way
    Thank you again for helping me��
  24. #13
  25. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,893
    Rep Power
    509
    Try this program, then view the file named h . When viewed in emacs the file is in Hebrew, and written right to left. Warning, this program might overwrite a file named h in current directory.
    Code:
    a=[ b'\xd7\x94\xd7\x95\xd7\xa0\xd7\x93\xd7\x95\xd7\xa8\xd7\xa1\xc2\xa0-\xc2\xa0\xd7\x90\xd7\x95\xd7\xa1\xd7\x98\xd7\xa8\xd7\x9c\xd7\x99\xd7\x94',
    b'\xd7\x94\xd7\x95\xd7\xa0\xd7\x93\xd7\x95\xd7\xa8\xd7\xa1\xc2\xa0-\xc2\xa0\xd7\x90\xd7\x95\xd7\xa1\xd7\x98\xd7\xa8\xd7\x9c\xd7\x99\xd7\x94\xc2\xa0(\xe2\x80\x8e+1)',
    b'\xd7\xa1\xd7\x9a \xd7\x94\xd7\x9b\xd7\x9c \xd7\xa9\xd7\xa2\xd7\xa8\xd7\x99\xd7\x9d \xd7\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a1\xc2\xa0(\xe2\x80\x8e+8.5)',
    b'\xd7\x91\xd7\x95\xd7\xa1\xd7\x98\xd7\x95\xd7\x9f \xd7\xa1\xd7\x9c\xd7\x98\xd7\x99\xd7\xa7\xd7\xa1\xc2\xa0-\xc2\xa0\xd7\xa9\xd7\x90\xd7\xa8\xd7\x9c\xd7\x95\xd7\x98 \xd7\x94\xd7\x95\xd7\xa8\xd7\xa0\xd7\x98\xd7\xa1\xc2\xa0(\xe2\x80\x8e+3)',
    b'\xd7\x91\xd7\x95\xd7\xa1\xd7\x98\xd7\x95\xd7\x9f \xd7\xa1\xd7\x9c\xd7\x98\xd7\x99\xd7\xa7\xd7\xa1\xc2\xa0-\xc2\xa0\xd7\xa9\xd7\x90\xd7\xa8\xd7\x9c\xd7\x95\xd7\x98 \xd7\x94\xd7\x95\xd7\xa8\xd7\xa0\xd7\x98\xd7\xa1\xc2\xa0(\xe2\x80\x8e+3.5)',
    b'\xd7\xa9\xd7\x99\xd7\xa7\xd7\x92\xd7\x95 \xd7\x91\xd7\x95\xd7\x9c\xd7\xa1\xc2\xa0(\xe2\x80\x8e+3)\xc2\xa0-\xc2\xa0\xd7\x90\xd7\x99\xd7\xa0\xd7\x93\xd7\x99\xd7\x90\xd7\xa0\xd7\x94 \xd7\xa4\xd7\x99\xd7\x99\xd7\xa1\xd7\xa8\xd7\xa1',]
    
    
    with open('h','wb') as ouf:
        for l in a:
            ouf.write(l)
            ouf.write(bytes((10,)))
    [code]Code tags[/code] are essential for python code and Makefiles!
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    19
    Rep Power
    0
    It works fine
    my problem is how i deal with full text
    within this program always copy and paste
    set up 'a' name this program
    i make attempts and maybe there's a way
  28. #15
  29. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,893
    Rep Power
    509
    with slight change to the end of your post 1 program, how about
    Code:
    from urllib.request import urlopen as uReq
    from bs4 import BeautifulSoup as soup
    import urllib
    
    my_url ='http://www.telesport.co.il/%D7%90%D7%96%D7%95%D7%A8%20%D7%95%D7%95%D7%99%D7%A0%D7%A8'
    uClient = uReq(my_url)
    page_html = uClient.read()
    uClient.close()
    page_soup = soup(page_html, "html.parser")
    
    cc = page_soup.findAll("td",{"class":"th_td_WinnerteamsAndBetType"})
    
    with open('h','wb') as ouf:
        for i in range(10):
            ouf.write(cc[i].text.encode('utf8'))
            ouf.write(bytes((10,)))
    [code]Code tags[/code] are essential for python code and Makefiles!
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo