Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading a line of a CSV
#1
my .csv file could have numbers or quoted strings between the commas. this seems to be the same syntax as that found in a Python literal for a list. so what if i read in a line, .strip() it, prepend '[' to the front, append ']' to the end, and run that through exec() and verify that what is returned is a single simple list with just numbers and strings in it?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
I would definitely use csv module, but if you insist on your approach, at least use ast.literal_eval()
>>>import ast
>>> line = '[1, "some text, with comma", "1,000.5"]'
>>> ast.literal_eval(line)
[1, 'some text, with comma', '1,000.5']
Skaperen likes this post
Quote
#3
it looks like ast.literal_eval() is the way to go.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  reading data from command line mcmxl22 2 53 Feb-17-2019, 09:01 PM
Last Post: Axel_Erfurt
  Database Creation and Reading Tussocky_Coder 3 129 Jan-15-2019, 02:20 AM
Last Post: Larz60+
  Reading json from webpage jmair 4 197 Jan-10-2019, 09:14 PM
Last Post: jmair
  Reading file names CertifiedPengu 1 137 Jan-07-2019, 06:54 PM
Last Post: Gribouillis
  Problem with reading a path gkiller007 30 644 Jan-05-2019, 10:09 PM
Last Post: snippsat
  Issue with reading CSV file nnsatpute 12 328 Dec-19-2018, 12:08 PM
Last Post: Gribouillis
  Adding new line in a one line txt file. Mady 7 281 Nov-21-2018, 01:04 PM
Last Post: Gribouillis
  reading from 2 pipes Skaperen 2 279 Oct-05-2018, 04:45 AM
Last Post: Skaperen
  reading csv and writing csv chawas 2 293 Aug-23-2018, 09:28 AM
Last Post: chawas
  Reading a html file peterl 4 358 Aug-20-2018, 03:16 PM
Last Post: peterl

Forum Jump:


Users browsing this thread: 1 Guest(s)