How do I process this?

I

Igor Korot

Hi, ALL,
I have a csv file which depending on how it was produced gives 2 different
strings as shown in the example below (test1 and test2).
I am only interested in the first field in test1 and obviously in the whole
string of test2.

So, I tried to see if I can get what I want in one simple way, but failed.
Is it possible to process the file independently?

Thank you.

python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
Traceback (most recent call last):
 
J

John Gordon

In said:
--047d7b6dc250a1426004f3bffd8d
Content-Type: text/plain; charset=ISO-8859-1
Hi, ALL,
I have a csv file which depending on how it was produced gives 2 different
strings as shown in the example below (test1 and test2).
I am only interested in the first field in test1 and obviously in the whole
string of test2.
So, I tried to see if I can get what I want in one simple way, but failed.
Is it possible to process the file independently?
Thank you.

split() returns a list. Use indexing to grab only the element you want,
like this:

t1 = test1.split(',')[0]
t2 = test2.split(',')[0]
 
A

alex23

Hi, ALL,
I have a csv file which depending on how it was produced gives 2
different strings as shown in the example below (test1 and test2).
I am only interested in the first field in test1 and obviously in the
whole string of test2.

Try using the csv module:
from StringIO import StringIO
csvfile = StringIO()
csvfile.write('a,,,,\n')
csvfile.write('a\n')
csvfile.seek(0)

import csv
reader = csv.reader(csvfile)
[x[0] for x in reader]
['a', 'a']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top