H
H
haniaman2020-06-03 04:34:50
Python
haniaman, 2020-06-03 04:34:50

How can I make sure that when writing to a csv file, the numbers are numeric, not dates?

Hello, there is a parser, I need the values ​​to be output to a csv file. I managed to do everything, but there are numbers like 5.3, 1.03, 7.10, etc. And in csv file they are displayed as date:

1 game Oct 07 Time: 04:17:27
2 game Feb.73 Time: 04:17:35
3 game Apr.61 Time: 04:17:55
4 game Mar.14 Time: 04:18:26
5 game Aug.24 Time: 04:19:10

How can I fix this to be displayed as in the console?:
['1', 'game', '7.10', 'Time: 04:17: 27']
['2', 'game', '2.73', 'Time: 04:17:35']
['3', 'game', '4.61', 'Time: 04:17:55']
[ '4', 'game', '3.14', 'Time: 04:18:26']
['5', 'game',

from selenium import webdriver
import time
import csv

driver = webdriver.Chrome()

driver.get("https://rublix.best/")

prev_bet = 0
a = 1

time.sleep(12)
while True:
    # Находит блок с "Историей игр".
    all_bets = driver.find_elements_by_xpath("//div[contains(@class, 'history-line')]")

    # Определение времени на данный момент.
    named_tuple = time.localtime() # получить struct_time.
    time_string = time.strftime("Время: %H:%M:%S", named_tuple)

    # Выбирает последнюю из списка игру.
    if all_bets:
        last_bet = all_bets[0].text

        # Выводит на экран последнюю игру в формате: ['5', 'game', '8.24', 'Время: 04:19:10'].
        if last_bet != prev_bet:
            prev_bet = last_bet
            # Создается список с данными: ['Number', 'game', 'result', 'Время: HH:MM:SS'].
            t = [str(a), 'game', '{}'.format(last_bet), '{}'.format(time_string)]
            print(t)
            a+=1 
            myFile = open('test_game1.csv', 'a', newline='')
            with myFile:
                writer = csv.writer(myFile, delimiter=';') # Записывает в myFile с разделителем ';' для перехода в след. ячейку.
                writer.writerows([t]) # Записывает список списка переменной t, иначе через каждый символ будет запятая.
    time.sleep(1)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Pankov, 2020-06-03
@trapwalker

You missed the most important thing. In the form of dates, these lines look in Excel, and CSV has nothing to do with it. This is a plain text format and you cannot specify the cell's data type in it.
In the next answer, you were already recommended to set the data type of the cells in Excel. This is exactly his Excel problem. Too smart.
However, there is also a kind of life hack. So that decimal fractional numbers are not perceived by Excel as dates, you can make them dissimilar to dates. This can be done by adding zeros to the right in the fractional part. You can even make a streaming converter that will format all fractional numbers with zeros up to, say, six decimal places. These are insignificant zeros and they will not affect the numerical data, but Excel will not show their date.
Another reason why Excel may not recognize a number is the decimal separator, which is determined by the locale and in some countries it is customary to make it a dot, in others a comma. This brings confusion. If you know for sure under which locale you are making the file, then you can substitute the desired separator. If not, use a dot.

G
galaxy, 2020-06-03
@galaxy

They are in the csv file, most likely, normally displayed. This is how it is formatted in excel. Use the Data->From Text dialog and specify the cell format

K
Korben5E, 2020-06-03
@Korben5E

csv is a text file, open it with notepad, see what's there and how.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question