python - How to read multiline command line arguments into a python2 CSV reader -
i'm testing python 2 script takes csv file's text command line argument. csv standard excel csv file ','
delimiters items , presumably '\r\n'
line endings.
the problem need pass csv text single line script in order recognise string single argument. in order this, open csv in notepad , replace of new lines '\r\n'
enables me read test script successfully. however, when try create csv.reader
object string, csv.reader
sees single line want iot see multiple lines.
given following csv string example:
the,quick,brown,fox\r\njumps,over,the,lazy,dog
i expect following lines:
the,quick,brown,fox jumps,over,the,lazy,dog
but instead end single line:
the,quick,brown,fox\r\njumps,over,the,lazy,dog
once capture string command line, use following load csv.reader
:
input_str = self.getcsvstringfrominput() input_reader = csv.reader(stringio.stringio(input_str))
i'm using windows presumed \r\n
correct don't seem using correct method.
any ideas? thanks, in adv.!
why don't read lines directly csv file? such as:
open('d:/file-1.csv','r') csvfile: creader=csv.reader(csvfile, delimiter=',') line in creader: print line
just replace 'print line' own function. if need manually copy each line csv file,you can try split each line '\r\n' before pass reader.
for line in 'the,quick,brown,fox\r\njumps,over,the,lazy,dog'.split('\r\n'): print line
Comments
Post a Comment