The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python Help: Open csv file with tab delimiter. csv.DictReader vs. csv.reader
By Guest on 11th November 2022 07:29:33 AM | Syntax: PYTHON | Views: 194



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. import csv
  2.  
  3. i = 0
  4. with open('data/AAPLUSUSD_H4.csv', 'r') as f:
  5.     reader = csv.DictReader(f)
  6.     for row in reader:
  7.         print(row)
  8.         i += 1
  9.         if i > 7:
  10.             break
  11.  
  12. >>>{'Time\tOpen\tHigh\tLow\tClose\tVolume': '2017-02-01 20:00:00\t127.07\t128.43\t127.06\t128.163\t90'}
  13. {'Time\tOpen\tHigh\tLow\tClose\tVolume': '2017-02-02 00:00:00\t128.16\t130.112\t127.851\t129.983\t240'}
  14. {'Time\tOpen\tHigh\tLow\tClose\tVolume': '2017-02-02 04:00:00\t129.984\t130.48\t128.78\t128.785\t60'}
  15. {'Time\tOpen\tHigh\tLow\tClose\tVolume': '2017-02-02 20:00:00\t127.9\t129.38\t127.89\t128.68\t90'}
  16.  
  17.  
  18. import csv
  19.  
  20. i = 0
  21. with open('data/AAPLUSUSD_H4.csv', 'r') as f:
  22.     reader = csv.reader(f)
  23.     for row in reader:
  24.         print(row[0].split('\t'))
  25.         i += 1
  26.         if i > 7:
  27.             break
  28.  
  29. >>>['Time', 'Open', 'High', 'Low', 'Close', 'Volume']
  30. ['2017-02-01 20:00:00', '127.07', '128.43', '127.06', '128.163', '90']
  31. ['2017-02-02 00:00:00', '128.16', '130.112', '127.851', '129.983', '240']
  32. ['2017-02-02 04:00:00', '129.984', '130.48', '128.78', '128.785', '60']
  33. ['2017-02-02 20:00:00', '127.9', '129.38', '127.89', '128.68', '90']
  34. ['2017-02-03 00:00:00', '128.68', '128.722', '127.78', '128.204', '240']
  35.  
  36.  
  37. i = 0
  38. with open('data/AAPLUSUSD_H4.csv', 'r', encoding='utf-8') as f:
  39.     for row in f:
  40.         print(row.strip().split('\t'))
  41.         i += 1
  42.         if i > 7:
  43.             break
  44.  
  45. >>>['Time', 'Open', 'High', 'Low', 'Close', 'Volume']
  46. ['2017-02-01 20:00:00', '127.07', '128.43', '127.06', '128.163', '90']
  47. ['2017-02-02 00:00:00', '128.16', '130.112', '127.851', '129.983', '240']
  48. ['2017-02-02 04:00:00', '129.984', '130.48', '128.78', '128.785', '60']
  49. ['2017-02-02 20:00:00', '127.9', '129.38', '127.89', '128.68', '90']
  50. ['2017-02-03 00:00:00', '128.68', '128.722', '127.78', '128.204', '240']
  51. ['2017-02-03 04:00:00', '128.2', '128.54', '128.13', '128.51', '60']
  52. ['2017-02-03 20:00:00', '128.308', '129.18', '128.163', '128.7', '90']
  53.  
  54.  
  55. Reading tab-delimited files using basic python
  56. https://www.youtube.com/watch?v=RCWlfW5JRNA
















Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.

Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.

See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license
  • Recent Pastes