How to convert data in document using split-Python [duplicate] - python

This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 4 years ago.
list.txt document contain data like
a,b,c,d,e,f
I want to insert above data into list or convert it as list. I tried this code. But it's not correct.
document=open("list.txt","r")
Mylist=[document.read().split(",")]
print(Mylist)
document.close()

with open("list.txt", "r") as Document:
print(Document.read().split(","))

Related

Python - turn string of list of dictionaries to list of dictionaries [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 8 months ago.
I have a string that looks like this:
'{"screen_name":"Brian","avatar":1},{"screen_name":"David","avatar":21},{"screen_name":"Teo","avatar":34}'
How can I make it a list of dictionaries?
I tried with json.loads, but it threw an error...
before trying json.loads, you have to make sure that is surrounded by square brackets.
You can achieve that by using format or fstrings, so you can do:
>>> import json
>>> json.loads(f"[{original}]")
[{"screen_name":"Brian","avatar":1},{"screen_name":"David","avatar":21},{"screen_name":"Teo","avatar":34}]

python format list into comma separated string [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
I have a simple list
li=['beststreet','borocd']
print "I like strings a lot {}".format(li)
I want to format the list into a string but I want the output to be like this:
I like strings a lot beststreet,borocd
background: this is a simple example, I am doing string manipulation in a Class to be inserted into a SQL table...
Just join the strings in the list with a comma:
li=['beststreet','borocd']
print "I like strings a lot {}".format(‘,’.join(li))

Split string when ";" appears [duplicate]

This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 5 years ago.
I need to split the string everytime ; shows up.
words = "LightOn;LightOff;LightStatus;LightClientHello;"
Output should be something like this:
LightOn
LightOff
LightStatus
LightClientHello
Simply, everytime it finds ; in a string, it has to split it.
Thank you for help
res = words.split(";")
Refer to this link for more information on split.

how to lower case a list in python [duplicate]

This question already has answers here:
Convert a list with strings all to lowercase or uppercase
(13 answers)
Closed 6 years ago.
I am trying to read a text file in jupyter notebook and fetch the unique words in the file.
I am reading that file as a list and then trying to apply lower case on it. But the .lower() function doesn't work with a list. Please help with the same.
With a list of values in val you can do this:
valsLower = [item.lower() for item in vals]

cut the right side of a '=' delimited string and storing into a list in python [duplicate]

This question already has answers here:
How to get part of string and pass it to other function in python?
(4 answers)
Closed 8 years ago.
A beginner question
My file looks like -->
10.5.5.81=apache,php,solr
10.5.5.100=oracle,coherence
How can I cut the IP part and store it into a list for further processing?
Please help.
answer = []
with open('path/to/file') as infile:
for line in infile:
answer.append(line.partition('=')[0].strip())

Categories