1

I want to read last line of temperature (i.e. 24.925). My data is in following format in .csv format

Date                Temp            RH
2020-12-07 14:00:00 24.5198601667   66.9861108333
2020-12-07 15:00:00 24.9940271667   64.4736103333
2020-12-07 16:00:00 24.925          65

I'm following the given code:

from collections import deque
import csv

def get_last_row(csv_filename):
    with open(csv_filename, 'rb') as f:
        return deque(csv.reader(f), 1)[0]

lastline = ', '.join(get_last_row('data.csv'))
values = lastline.split("\t")
print ((values[1]))

Any suggestions will be helpful. Thank you in Advance.

4
  • 1
    What's not clear about "iterator should return strings, not bytes (did you open the file in text mode?)"? Have you tried opening the file in text mode, as the error message suggests? Commented Dec 9, 2020 at 15:28
  • I don't understand what the join with commas then split with tab is for. There aren't any tabs in your data. Your example data isn't in a CSV format - is it really a tab-separated file? Commented Dec 9, 2020 at 15:58
  • Thank you. I have tried in txt but entire row was displaying. I want only temperature value... Commented Dec 9, 2020 at 16:00
  • If "data.csv" is really comma separated, then the final temperature would be get_last_row("data.csv")[1]. The CSV module would have split the columns out for you. No need for the tab splitting part. If its a tab separated file, then csv.reader(f, dialect=csv.excel_tab) may work. But the data you've shown us is some sort of expanded view of the data - perhaps what you'd get from displaying a pandas data frame. Can you post the actual CSV file content? Commented Dec 9, 2020 at 16:09

1 Answer 1

1

This is a division of labor issue. The CSV module is not in the business of figuring out string encodings. When decoding it will look for string delimiters, quotes and separators. Since strings don't compare directly against bytes unless they've been decoded, the module can't proceed. As an example, CSV looks for a single character delimiter, but a comma in UTF-16 is 2 bytes. Solution: open in text mode.

All I did for this example is remove "rb" from the open call. This will open the file in your system's default encoding. You may want to specify an encoding, e.g., open('data.csv', encoding='utf-8') instead.

from collections import deque
import csv

def get_last_row(csv_filename):
    with open(csv_filename) as f:
        return deque(csv.reader(f), 1)[0]

lastline = ', '.join(get_last_row('data.csv'))
values = lastline.split("\t")
print ((values[1]))

Note: This code doesn't work with your supplied example, which isn't actually CSV and since it doesn't contain a tab, values[1] is an index error.

Sign up to request clarification or add additional context in comments.

1 Comment

Anyway, I have gone through your first suggestion. I have save my data into .txt format and run earlier your suggested code. It works...:). thnks

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.