Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following text file that has these lists :

Python
['2009-06-12 14:19:28 ', '0']
['2009-06-13 00:58:10 ', '0']
['2009-06-13 01:00:25 ', '0']
['2009-06-15 03:24:23 ', '0']
['2009-06-25 17:50:46 ', '0']
['2009-06-29 20:54:52 ', '0']
['2009-06-30 20:31:19 ', '0']
['2009-07-09 03:01:25 ', '0']
['2009-07-12 14:45:53 ', '0']
['2009-07-12 15:09:38 ', '0']



The second element is an integer, however, the first element is a string. I need the first element of each lis to be a date instead of string?

I tried this code:

Python
word[0]=datetime.strptime(word[0],'%Y/%m/%d %H:%M:%S')



but I get this error:

Python
ValueError: time data '2009-04-02 22:50:06 ' does not match format '%Y/%m/%d %H:%M:%S'



the current code I have that cleans the data is the following:

Python
with open('newdata.txt') as f:
    for line in f:
       tweet=re.sub(r'T\t',"",line)
       t=re.sub(r'W\t',"\/\/",tweet)
       data=t.split("\/\/")
       sentiment_value= s.sentiment(data[1])
       data.append(sentiment_value)

       words = [w.replace('pos', '1') for w in data]
       word=[w.replace('neg','0')for w in words]
       del word[1]
       print(word)


What I have tried:

I tried this code:

Python
word[0]=datetime.strptime(word[0].strip(),'%Y/%m/%d %H:%M:%S')



but I get this error:

Python
ValueError: time data '2009-04-02 22:50:06 ' does not match format '%Y/%m/%d %H:%M:%S'
Posted
Updated 20-Jun-18 20:32pm
v2
Comments
Richard MacCutchan 21-Jun-18 3:34am    
You have hyphens (-) as separators in your date string, but forward slashes (/) in your format string.
Member 13647869 21-Jun-18 3:43am    
i changed it to this:
word[0]=datetime.strptime(word[0],'%Y-%m-%d %H:%M:%S')

and got this:

ValueError: unconverted data remains:
Richard MacCutchan 21-Jun-18 3:46am    
Look at your data, there is a space after the seconds field. Your format string must match exactly.
Member 13647869 21-Jun-18 3:46am    
i have put that space, or do i have to remove that?
Richard MacCutchan 21-Jun-18 3:54am    
Like I said above.

1 solution

On the first look... Seems you need to replace:
Python
dt = datetime.strptime("2009-04-02 22:50:06",'%Y/%m/%d %H:%M:%S')

with:
Python
dt = datetime.strptime("2009-04-02 22:50:06", "%Y-%m-%d %H:%M:%S")


See:
Converting Strings To Datetime[^]
8.1. datetime — Basic date and time types — Python 3.6.6rc1 documentation[^]

But... the documentation[^] states also:
Quote:
Calling strptime() with incomplete or ambiguous ISO 8601 directives will raise a ValueError.

(...)

Because the format depends on the current locale, care should be taken when making assumptions about the output value. Field orderings will vary (for example, “month/day/year” versus “day/month/year”), and the output may contain Unicode characters encoded using the locale’s default encoding (for example, if the current locale is ja_JP, the default encoding could be any one of eucJP, SJIS, or utf-8; use locale.getlocale() to determine the current locale’s encoding).
 
Share this answer
 
v2
Comments
Member 13647869 21-Jun-18 2:30am    
i tried it i got this error:

ValueError: time data '2009-04-02 22:50:06 ' does not match format '%y-%m-%d %H:%M:%S'


plus, i need to index the element of each list, because each list has a different date string in the first element
Member 13647869 21-Jun-18 2:32am    
sorry, it seems that I have put a wrong sample of the text file I changed it
Richard MacCutchan 21-Jun-18 3:35am    
Should be upper case (Y) for 4-digit year.
Maciej Los 21-Jun-18 4:05am    
Thanks! Updated!
BTW: I'm not an expert of Python ;)
Richard MacCutchan 21-Jun-18 4:23am    
You are now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900