2
import csv
import sys

f = open(sys.argv[1], 'rb') 

reader = csv.reader(f)

k = []

for i in reader:
    j = i.replace(' ','')
    k.append(j)

print k

the raw CSV is this

['1 323 104 564 382']
['2 322 889 564 483']
['3 322 888 564 479']
['4 322 920 564 425']
['5 322 942 564 349']
['6 322 983 564 253']
['7 322 954 564 154']
['8 322 978 564 121']

I want to make it look like this:

['1323104564382']
['2322889564483']
['3322888564479']
['4322920564425']
['5322942564349']
['6322983564253']
['7322954564154']
['8322978564121']

i get the following error:

Traceback (most recent call last): File "list_replace.py", line 12, in j = i.replace(' ','') AttributeError: 'list' object has no attribute 'replace'

Im super new at this so im probably screwing multiple things up,just need some guidance.

I eventually want the csv to look like the below text, but im taking it one step at a time

['1,323104,564382']
['2,322889,564483']
['3,322888,564479']
['4,322920,564425']
['5,322942,564349']
['6,322983,564253']
['7,322954,564154']
['8,322978,564121']
dergatroid
  • 23
  • 2

2 Answers2

0

Try this. I might be off with the slicing:

parts= i[0].split()
one=parts[0]
two=parts[1:2+1]
three=parts[3:4+1]
j= ",".join( [one,two,three] )
Community
  • 1
  • 1
handle
  • 5,859
  • 3
  • 54
  • 82
0
[[i[0].replace(' ','')] for i in reader]

and to get to your final goal:

reader=[[i[0].replace(' ',',',1)] for i in reader]
reader=[[i[0].replace(' ','',1)] for i in reader]
reader=[[i[0].replace(' ',',',1)] for i in reader]
reader=[[i[0].replace(' ','',1)] for i in reader]

to get:

[['1,323104,564382'],
 ['2,322889,564483'],
 ['3,322888,564479'],
 ['4,322920,564425'], 
 ['5,322942,564349'],
 ['6,322983,564253'],
 ['7,322954,564154'],
 ['8,322978,564121']]
Binyamin Even
  • 3,318
  • 1
  • 18
  • 45