7

this is my main string

"action","employee_id","name"
"absent","pritesh",2010/09/15 00:00:00

so after name coolumn its goes to new line but here i append to list a new line character is added and make it like this way

data_list***** ['"action","employee_id","name"\n"absent","pritesh",2010/09/15 00:00:00\n']

here its append the new line character with absent but actually its a new line strarting but its appended i want to make it like

data_list***** ['"action","employee_id","name","absent","pritesh",2010/09/15 00:00:00']

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
pritesh modi
  • 111
  • 1
  • 2
  • 4

5 Answers5

9

Davide's answer can be written even simpler as:

data_list = [word.strip() for word in data_list]

But I'm not sure it's what you want. Please write some sample in python.

zefciu
  • 1,967
  • 2
  • 17
  • 39
8
replaces = inString.replace("\n", "");
sashkello
  • 17,306
  • 24
  • 81
  • 109
3

First, you can use strip() to get rid of '\n':

>>> data = line.strip().split(',')

Secondly, you may want to use the csv module to do that:

>>> import csv
>>> f = open("test")
>>> r = csv.reader(f)
>>> print(r.next())
['action', 'employee_id', 'name']
Bite code
  • 578,959
  • 113
  • 301
  • 329
  • 1
    Your suggestion to use the csv module is definitely the way to go. However, if using `strip()` then you may want to consider stripping only the `'\n'` and preserve any whitespace that may precede it, I would suggest using `line.rstrip('\n')` which will remove just the newline from the end of the line, rather than using `strip()` which will remove all whitespace from both ends of the line which may not be desirable in cases where a free-form text column appears as the first or last column. – aculich Jun 29 '13 at 23:37
2

I'd do like that:

in_string.replace('\n', ',', 1).split(',')
kishkin
  • 5,152
  • 1
  • 26
  • 40
1
def f(word):
    return word.strip()
data_list = map(f, data_list)
Davide Gualano
  • 12,813
  • 9
  • 44
  • 65