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
  print number of a list line per line lateublegende 2 108 Mar-20-2019, 04:07 PM
Last Post: lateublegende
  Reading and manipulating csv Prince_Bhatia 11 350 Mar-14-2019, 11:40 AM
Last Post: Larz60+
  reading data from command line mcmxl22 2 109 Feb-17-2019, 09:01 PM
Last Post: Axel_Erfurt
  Adding new line in a one line txt file. Mady 7 379 Nov-21-2018, 01:04 PM
Last Post: Gribouillis
  reading from 2 pipes Skaperen 2 349 Oct-05-2018, 04:45 AM
Last Post: Skaperen
  reading csv and writing csv chawas 2 351 Aug-23-2018, 09:28 AM
Last Post: chawas
  Add a line after a specific line morgandebray 3 328 Aug-07-2018, 12:12 PM
Last Post: buran
  Compare element of list with line of file : if match, delete line silfer 4 575 Jul-21-2018, 02:44 PM
Last Post: silfer
  Print Nested List Line by Line anelliaf 5 1,113 May-23-2018, 06:54 PM
Last Post: buran
  Reading and Extracting XML Python4Ever 2 937 Dec-12-2017, 04:32 PM
Last Post: Python4Ever

Forum Jump:


Users browsing this thread: 1 Guest(s)