Reputation: 2738
My code
token = open('out.txt','r')
linestoken=token.readlines()
tokens_column_number = 1
r=[]
for x in linestoken:
r.append(x.split()[tokens_column_number])
token.close()
print (r)
Output
'"tick2/tick_calculated_2_2020-05-27T11-59-06.json.gz",'
Desired output
"tick2/tick_calculated_2_2020-05-27T11-59-06.json.gz"
How to get rid of ''
and ,
?
Upvotes: 2
Views: 104
Reputation: 5329
It would be nice to see your input data. I have created an input file which is similar than yours (I hope).
My test file content:
example1 "tick2/tick_calculated_2_2020-05-27T11-59-06.json.gz", aaaa
example2 "tick2/tick_calculated_2_2020-05-27T11-59-07.json.gz", bbbb
You should replace the "
, ',' characters.
You can do it with replace
(https://docs.python.org/3/library/stdtypes.html#str.replace):
r.append(x.split()[tokens_column_number].replace('"', "").replace(",", ""))
You can do it with strip
(https://docs.python.org/2/library/string.html#string.strip):
r.append(x.split()[tokens_column_number].strip('",'))
You can do it with re.sub
(https://docs.python.org/3/library/re.html#re.sub):
import re
...
...
for x in linestoken:
x = re.sub('[",]', "", x.split()[tokens_column_number])
r.append(x)
...
...
Output in both cases:
>>> python3 test.py
['tick2/tick_calculated_2_2020-05-27T11-59-06.json.gz', 'tick2/tick_calculated_2_2020-05-27T11-59-07.json.gz']
As you can see above the output (r
) is a list
type but if you want to get the result as a string, you should use the join
(https://docs.python.org/3/library/stdtypes.html#str.join).
Output with print(",".join(r))
:
>>> python3 test.py
tick2/tick_calculated_2_2020-05-27T11-59-06.json.gz,tick2/tick_calculated_2_2020-05-27T11-59-07.json.gz
Output with print("\n".join(r))
:
>>> python3 test.py
tick2/tick_calculated_2_2020-05-27T11-59-06.json.gz
tick2/tick_calculated_2_2020-05-27T11-59-07.json.gz
Upvotes: 1