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 287 Mar-20-2019, 04:07 PM
Last Post: lateublegende
  reading data from command line mcmxl22 2 221 Feb-17-2019, 09:01 PM
Last Post: Axel_Erfurt
  Adding new line in a one line txt file. Mady 7 568 Nov-21-2018, 01:04 PM
Last Post: Gribouillis
  Add a line after a specific line morgandebray 3 468 Aug-07-2018, 12:12 PM
Last Post: buran
  Compare element of list with line of file : if match, delete line silfer 4 729 Jul-21-2018, 02:44 PM
Last Post: silfer
  Print Nested List Line by Line anelliaf 5 1,650 May-23-2018, 06:54 PM
Last Post: buran
  Line by line execution Filthy_McNasty 1 898 Dec-11-2017, 10:35 PM
Last Post: wavic
  How to change language line by line? dialbrocom 2 1,014 Sep-28-2017, 08:09 PM
Last Post: nilamo
  Reading a CSV lewoc 4 1,578 Mar-08-2017, 01:09 AM
Last Post: Axel_Erfurt
  First line of File gets deleted when reading file lrxM 2 1,553 Dec-24-2016, 10:56 AM
Last Post: lrxM

Forum Jump:


Users browsing this thread: 1 Guest(s)