0

I have a file with the following format:

2015-11-01|00:00:00|1
2015-11-02|23:00:00|11
2015-11-03|07:00:00|12
2015-11-04|09:00:00|20

I'd like to be able to convert these lines into list format so I can execute the fields like below:

for date, hour, count in arr:
       incoming_images.write(row, col, date)
       incoming_images.write(row, col + 1, hour)
       incoming_images.write(row, col + 2, count)
       row +=1

I need help figuring out how to create this "arr" (if possible).

1 Answer 1

1
Str = open("file").read()
lines =Str.count('\n')
Str= Str.replace('\n', '|')
words = Str.split("|")
for i in range(0,len(words)-1,3)
   date.append(words[i])
   hour.append(words[i+1])
   count.append(words[i+2])

for date, hour, count in zip(date,hour,count):
   incoming_images.write(row, col, date)
   incoming_images.write(row, col + 1, hour)
   incoming_images.write(row, col + 2, count)
   row +=1
Sign up to request clarification or add additional context in comments.

9 Comments

File "sql_test.py", line 29, in <module> date.append(words[i]) NameError: name 'date' is not defined
And where is zip defined?
Are u in python 2.x or 3.x
I'm in Python 2.7. I assigned array variables for date, hour and count, but the output is not correct. It's putting the hour, the count and then the date. It is also deleting the last piece of date data.
For zip "import itertool"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.