How to split text by comma into lines? - python

How to split a line of text by comma onto separate lines?
Code
text = "ACCOUNTNUMBER=Accountnumber,ACCOUNTSOURCE=Accountsource,ADDRESS_1__C=Address_1__C,ADDRESS_2__C"
fields = text.split(",")
text = "\n".join(fields)
Issue & Expected
But "\n" did not work. The result expected is that it adds new lines like:
ACCOUNTNUMBER=Accountnumber,
ACCOUNTSOURCE=Accountsource,
ADDRESS_1__C=Address_1__C,
ADDRESS_2__C
Note: I run it on Google Colab

if you want the commas to stay there you can use this code:
text = "ACCOUNTNUMBER=Accountnumber,ACCOUNTSOURCE=Accountsource,ADDRESS_1__C=Address_1__C,ADDRESS_2__C"
fields = text.split(",")
print(",\n".join(fields))

Your code should give this output
ACCOUNTNUMBER=Accountnumber
ACCOUNTSOURCE=Accountsource
ADDRESS_1__C=Address_1__C
ADDRESS_2__C
But if you want to seperate it by commas(,). You should add comma(,) with \n use text = ",\n".join(fields) instead of text = "\n".join(fields)
So the final code should be
text="ACCOUNTNUMBER=Accountnumber,ACCOUNTSOURCE=Accountsource,ADDRESS_1__C=Address_1__C,ADDRESS_2__C"
fields = text.split(",")
text = ",\n".join(fields)
print (text)
It will give your desirable output.

A more cross-compatible way could be to use os.linesep. It's my understanding that it's safer to do this for code that might be running on both Linux, Windows and other OSes:
import os
print("hello" + os.linesep + "fren")

I try to use print then it worked!, thank all you guys

You can use replace() :
text = "ACCOUNTNUMBER=Accountnumber,ACCOUNTSOURCE=Accountsource,ADDRESS_1__C=Address_1__C,ADDRESS_2__C"
print(text.replace(',',",\n"))
result:
ACCOUNTNUMBER=Accountnumber,
ACCOUNTSOURCE=Accountsource,
ADDRESS_1__C=Address_1__C,
ADDRESS_2__C

Related

Filter specific word in string using python

I got some data like this
https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000358.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d756/e285/f317/2ece2309-3d1c-49da-8d3a-32e0227e7732.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d379/e118/f25/554586cb-cf2d-40ef-9b6a-55fcf8d9e598.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d856/e130/f366/21ed2d17-7610-4ad2-b517-5b1b0007612a.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000360.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d356/e17/f185/b1a2de52-4110-4355-a9fb-bf1d0eb627c9.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d593/e103/f285/1633c311-e148-4d03-bb43-292d816951d2.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000359.jpghttps://www.travel.taipei/streams/scenery_file_audio/c03.mp3
The thing I want to do is to put URL that contains "jpg" or "png" into a list by using Python.
like["https.....jpg", "https......jpg", "https........png"]
But I have no ideas. Any suggestions?
Try:
s = """https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000358.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d756/e285/f317/2ece2309-3d1c-49da-8d3a-32e0227e7732.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d379/e118/f25/554586cb-cf2d-40ef-9b6a-55fcf8d9e598.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d856/e130/f366/21ed2d17-7610-4ad2-b517-5b1b0007612a.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000360.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d356/e17/f185/b1a2de52-4110-4355-a9fb-bf1d0eb627c9.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d593/e103/f285/1633c311-e148-4d03-bb43-292d816951d2.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000359.jpghttps://www.travel.taipei/streams/scenery_file_audio/c03.mp3"""
for url in s.split("http"):
if url.endswith(("jpg", "png")):
print("http" + url)
Prints:
https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000358.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d756/e285/f317/2ece2309-3d1c-49da-8d3a-32e0227e7732.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d379/e118/f25/554586cb-cf2d-40ef-9b6a-55fcf8d9e598.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d856/e130/f366/21ed2d17-7610-4ad2-b517-5b1b0007612a.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000360.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d356/e17/f185/b1a2de52-4110-4355-a9fb-bf1d0eb627c9.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d593/e103/f285/1633c311-e148-4d03-bb43-292d816951d2.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000359.jpg
replace and split
strs ="https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000358.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d756/e285/f317/2ece2309-3d1c-49da-8d3a-32e0227e7732.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d379/e118/f25/554586cb-cf2d-40ef-9b6a-55fcf8d9e598.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d856/e130/f366/21ed2d17-7610-4ad2-b517-5b1b0007612a.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000360.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d356/e17/f185/b1a2de52-4110-4355-a9fb-bf1d0eb627c9.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d593/e103/f285/1633c311-e148-4d03-bb43-292d816951d2.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000359.jpghttps://www.travel.taipei/streams/scenery_file_audio/c03.mp3"
strs =strs.replace("jpg", 'jpg ')
strs =strs.replace("png", 'png ')
print(strs.split())
output #
['https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000358.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d756/e285/f317/2ece2309-3d1c-49da-8d3a-32e0227e7732.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d379/e118/f25/554586cb-cf2d-40ef-9b6a-55fcf8d9e598.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d856/e130/f366/21ed2d17-7610-4ad2-b517-5b1b0007612a.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000360.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d356/e17/f185/b1a2de52-4110-4355-a9fb-bf1d0eb627c9.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d593/e103/f285/1633c311-e148-4d03-bb43-292d816951d2.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000359.jpg', 'https://www.travel.taipei/streams/scenery_file_audio/c03.mp3']

Python - remove spaces and indents from string

I have a sql query string
query_for_update =
f'''
update {db_name}.{schema_name}.{self.table_name}
set {self.updated_field} = {self.updated_field}
where {self.key_field} in ({ids});
'''
But when I try to write this query to file f.write(query_for_update) I get following result:
update store_1.dbo.[my_table]
set [Trusted Plan] = [Trusted Plan]
where [Entry No_] in (1472371,
1472375,
1472377,
1472379,
1472373,
);
Code that creates string:
ids_string = ',\n'.join(["'" + str(item) + "'" for item in result.id])
query_for_update = mssql_table.get_update_query('dbo', mssql_db_name, ids_string).strip()
with open(mssql_server_name + '.sql', 'a') as f:
f.write(query_for_update)
How can i remove indents for strings in this case?
You can use textwrap.dedent (standard library):
import textwrap
query = textwrap.dedent(f"""\
update {db_name}.{schema_name}.{self.table_name}
set {self.updated_field} = {self.updated_field}
where {self.key_field} in ({ids});
""")
print(query)
This will remove all leading spaces that are common between every line. Useful for tipple quoted strings.
You can use the str.strip() function with a for loop to fix it.
for x in list:
if x.strip():
list2.append(x)
then you can use list2 as your new usable list
you can use the str.stip method https://www.w3schools.com/python/ref_string_strip.asp
For indentations and breaks you need to consider that you might need to use \n. There is also a dedent method in the textwrap library that could be interesting for you. https://docs.python.org/3/library/textwrap.html
I hope this helps :)

List files in directory separately python

I'm working on a program that has a list of all files in a folder. Right now it only outputs the files as one entity, like one string. I need the files to each be displayed separately. Is there a way to do this?
This is the code I use:
import os
options = [
"Select a publisher",
os.listdir("C:/Users/bodig/Desktop/ad.olo exporter/config")
]
print(options)
The output:
['Select a publisher', ['bdu.json', 'ea.json', 'frz.json', 'fs.json', 'nfz.json', 'rht.json', 'taa.json', 'vol.json']]
The problem is that they are all one Entity. I can't figure out how to display them in a line. The output that I want is like this:
'Select a publisher'
'bdu.json'
'ea.json'
'frz.json'
'fs.json'
'nfz.json'
'rht.json'
'taa.json'
'vol.json'
options = ["Select a publisher"] + os.listdir("C:/Users/bodig/Desktop/ad.olo exporter/config")
for option in options:
print(option)
I don't exactly understand your problem, but with putting the file names out in separate lines I can help (as I think that is what you want). Just use a for loop:
print("Select a publisher:")
for i in os.listdir("C:/Users/bodig/Desktop/ad.olo exporter/config"):
print(i)
To print them all in one line, separated by commas, you could use:
print("Select a publisher:")
my_text = ""
for i in os.listdir("C:/Users/bodig/Desktop/ad.olo exporter/config"):
my_text += i + ", "
print(my_text)
you can modify your print statement, print like below.
import os
options = [
"Select a publisher",
os.listdir("C:/Users/bodig/Desktop/ad.olo exporter/config")
]
print(options[0], "\n", "\n".join(options[1])) # modified print
Output:
Select a publisher
bdu.json
ea.json
frz.json
fs.json
nfz.json
rht.json
taa.json
vol.json

Cleaning Twitter Data in Excel

I am working on a project for school, but now with online instruction it is much harder to get help. I have a dataset in excel and there are links and emojis that I need to remove.
This is what my data looks like now. I want to get rid of the https://t.co/....... link, the emojis and some of the weird characters.
Does anyone have any suggestions on how to do this in excel? or maybe python?
I'm not sure how to do it in Excel, however, you can easily load the Excel file into 'pandas.dataFrame' and then use regex to ignore the non-ascii chars:
file_path = '/some/path/to/file.xlsx'
df = pd.read_excel(file_path , index_col=0)
df = df.replace(r'\W+', '', regex=True)
Here you can find an extra explanation about loading an Excel file into a dataframe
Here you can read about more ways to ignore non-ascii chars in dataframe
According to this reference, I believe you could do a function like this:
def checkChars(inputString):
outputString = ""
allowedChars = [" ", "/", ":", ".", ",",";"] # The characters you want to include
for l in inputString:
if l.isalnum() or l in allowedChars: # This line will check if the character is alphanumeric or is in your allowed character list
outputString += l
return outputString

Cleaning a string (probably encoded string) in python

I am trying to read some data which is suppose to be tab delimited but I see a lot of #FO# in it?
I was wondering how can i clean that text out?
Sample snippet
title=#F0#Sometimes#F0#the#F0#Grave#F0#Is#F0#a#F0#Fine#F0#and#F0#Public#F0#Place.#F0#|url=http://query.nytimes.com/gst/fullpage.html?
res=940DEFD71230F93BA15750C0A9629C8B63#F0#|quote=New#F0#Jersey#F0#is,#F0#indeed,#F0#a#F0#hom
e#F0#of#F0#poets.#F0#Walt#F0#Whitman's#F0#tomb#F0#is#F0#nestled#F0#in#F0#a#F0#wooded#F0#grov
e#F0#in#F0#the#F0#Harleigh#F0#Cemetery#F0#in#F0#Camden.#F0#Joyce#F0#Kilmer#F0#is#F0#buried#F
0#in#F0#Elmwood#F0#Cemetery#F0#in#F0#New#F0#Brunswick,#F0#not#F0#far#F0#from#F0#the#F0#New#F
0#Jersey#F0#Turnpike#F0#rest#F0#stop#F0#named#F0#in#F0#his#F0#honor.#F0#Allen#F0#Ginsberg#F0
#may#F0#not#F0#yet#F0#have#F0#a#F0#rest#F0#stop,#F0#but#F0#the#F0#Beat#F0#Generation#F0#auth
or#F0#of#F0#"Howl"#F0#is#F0#resting#F0#at#F0#B'Nai#F0#Israel#F0#Cemetery#F0#in#F0#Newark.#F0
#|work=The#F0#New#F0#York#F0#Times#F0#|date=March#F0#28,#F0#2004#F0#|accessdate=August#F0#21
Make title and res strings and then use [s.replace(old, new)][1]:
title="#F0#Sometimes#F0#the#F0#Grave#F0#Is#F0#a#F0#Fine#F0#and#F0#Public#F0#Place.#F0#|url=http://query.nytimes.com/gst/fullpage.html?"
res="""940DEFD71230F93BA15750C0A9629C8B63#F0#|quote=New#F0#Jersey#F0#is,#F0#indeed,#F0#a#F0#hom
e#F0#of#F0#poets.#F0#Walt#F0#Whitman's#F0#tomb#F0#is#F0#nestled#F0#in#F0#a#F0#wooded#F0#grov
e#F0#in#F0#the#F0#Harleigh#F0#Cemetery#F0#in#F0#Camden.#F0#Joyce#F0#Kilmer#F0#is#F0#buried#F
0#in#F0#Elmwood#F0#Cemetery#F0#in#F0#New#F0#Brunswick,#F0#not#F0#far#F0#from#F0#the#F0#New#F
0#Jersey#F0#Turnpike#F0#rest#F0#stop#F0#named#F0#in#F0#his#F0#honor.#F0#Allen#F0#Ginsberg#F0
#may#F0#not#F0#yet#F0#have#F0#a#F0#rest#F0#stop,#F0#but#F0#the#F0#Beat#F0#Generation#F0#auth
or#F0#of#F0#"Howl"#F0#is#F0#resting#F0#at#F0#B'Nai#F0#Israel#F0#Cemetery#F0#in#F0#Newark.#F0
#|work=The#F0#New#F0#York#F0#Times#F0#|date=March#F0#28,#F0#2004#F0#|accessdate=August#F0#21"""
title = title.replace('#FO#', '')
res = res.replace('#FO#', '')

Categories