So I am trying to read line by line from the text file using python And I got list of lists. And I have to use the elements of the list to create dictionary. So what I did was
list1 = []
for line in file:
lines = line.strip().split()
list1.append(lines)
print(list1)
And what I got when I ran it is the list of lists but something different than what I wanted to get, I got something like this,
['a,b,c,d,e,f']
What I wanted to get was something like this,
[a,b,c,d,e,f]
So how do we get rid of that ' ' inside the list?
I tried to use remove method, but it did not work.
you just need to change the separator in the strip from the default white space to ",".
i.e you just need to add the separator argument in the strip function .
list1 = []
for line in file:
lines = line.strip().split(',')
list1.append(lines)
print(list1)
so if text is:
a,b,c,d,e,f
the strip function would take the separator as "," and create a list for it.
hence the output would be:
['a','b','c','d','e','f']
therefore finally you just need to change the separator argument of line.strip() from white spaces to comma separated list and you are done with it .
What you have here is a list with a single element. It contains a single string which is 'a,b,c,d,e,f'.
We can double-check:
data = ['a,b,c,d,e,f']
print(len(data)) # prints 1
print(data[0]) # prints 'a,b,c,d,e,f'
What you want is to split this string into a list:
row = data[0].split(',')
Now row contains what you expected data to be, which is ['a', 'b', 'c', 'd', 'e', 'f'].
In a real case you would probably fix this while reading the file, the code might look like this:
list1 = []
with open('test.csv') as test_file:
for line in test_file:
list1.append(line.strip().split(','))
print(list1)
Now you would then have a list per line in your input file:
[
['a', 'b', 'c', 'd', 'e', 'f'],
['g', 'h', 'i', 'j', 'k', 'l'],
]
Just change the delimeter from '' to ','
list1 = []
for line in file:
lines = line.strip().split(,)
list1.append(lines)
print(list1)
you are good to go
Related
I'm importing data from a file object. It's pretty simple; it's a .txt file, and the data was entered like this: ABCDEFGHIJKLMNOPQRSTUVWXYZ
I am trying to get it to be a list of individual characters, e.g.
my_list = ['A', 'B', 'C', 'D', ...etc.]
but it's showing up like this:
my_list = ['ABCDEFGHIBJK... etc.]
Where am I going wrong?
def split(string):
return [char for char in string]
# This opens a file, gets a file object, and returns it to the program.
def get_file_object1():
infile = open(r'#', 'r')
file_object = infile.readlines()
testing = split(file_object) # this is a test line
print(testing) # this is a test line
print(split('This is another test.'))
infile.close()
return file_object
Note: when I pass the file object to split(file_object), I get this
['ABCDEFGHIJKLMNOPQRSTUVWXYZ']
But when I pass a string of text to split('This is another string.'), I get this:
['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', 'n', 'o', 't', 'h', 'e', 'r', ' ',
't', 'e', 's', 't', '.']
Simple, just read and apply list() ;-)
with open(r'C:\Users\Liz\Desktop\PYTHON PROBLEMS\cipherUpper.txt') as f:
myList = list(f.read().strip())
Get myList as your list
The return type of readlines() is list, so you're basically doing this:
string = ['ABC..', ...]
[char for char in string]
where char is equal to 'ABC..'
so you need to iterate over the result of readlines:
testing = []
for line in f.readlines():
testing = testing + split(line)
or read a single line with readline() instead.
split is iterating the object it receives
When you pass file_object, the lines of the file are iterated. There is only one line so you receive the first line as a string.
testing = split(file_object) # >> ['ABCDEFGHIJKLMNOPQRSTUVWXYZ']
When you pass a string, the characters of the string are iterated so you get a list of characters.
print(split('This is another test.')) # >> ['T', 'h', 'i', 's', ....
I'm writing a script that queries a database file a multitude of times and writes the results of each query to a csv file, but I'm running into a problem that I can't seem to think of a solution for in python.
The program runs like so:
Query ->
returns a match that looks like this
<HEADER1|HEADER2|HEADER3|HEADER4|HEADER5>\n<id|K|X|D|Z|>
I then take this result, strip the headers, and convert what's leftover into a map
if matches == 1:
split = data.value.find("\n")
testLine = data.value[split:]
lineArray = map(str.strip, testLine.split('|'))
print lineArray
This leaves me with some like
['id', 'K', 'X', 'D', 'Z']
That I can then write to a CSV file
My problem is happening when I get a match of > 1 items, I am left with a big map that is obviously not correct as that data before conversion is returned as
<id|K|X|D|Z|>\n<id|K|X|D|Z|>\n<id|K|X|D|Z|>\n ------Before conversion to map
['id', 'K', 'X', 'D', 'Z'\n<id', 'K', 'X', 'D', 'Z'\nid', 'K', 'X', 'D', 'Z'] ---Results after map conversion
How can I go about splitting this the correct number of ways depending on how many matches are returned, while still being able to convert it to a map so it can be written to a csv file?
You should split your value on '\n'. Then first line is the header line, and all other lines are data:
if matches >= 1:
lines = data.value.split('\n')[1:] # split value in lines and skip first one
for line in lines:
lineArray = map(str.strip, line.strip('<>').split('|'))
# append lineArray to the csv file
Instead of finding the location of the first "\n" you could split by "\n". For example
if matches == 1:
for testLine in data.split("\n")[1:]:
lineArray = map(str.strip, testLine.split("|"))
This is not a full solution (still need to clean up the "<" and ">"), but this is the general idea.
You don't have to use a map. This uses a nested list comprehension:
data = '<HEADER1|HEADER2|HEADER3|HEADER4|HEADER5>\n<id|K|X|D|Z|>\n<id|K|X|D|Z|>\n<id|K|X|D|Z|>\n<id|K|X|D|Z|>'
values = data.split()[1:]
# get rid of leading '<` and trailing '|>':
lineArray = [ [str.strip() for str in value[1:-2].split('|')] for value in values]
print(lineArray)
Prints:
[['id', 'K', 'X', 'D', 'Z'], ['id', 'K', 'X', 'D', 'Z'], ['id', 'K', 'X', 'D', 'Z'], ['id', 'K', 'X', 'D', 'Z']]
I'm trying to write a program for the micro:bit which displays text as morse code. I've looked at multiple websites and Stack Overflow posts for a way to split a string into characters.
E.g.
string = "hello"
to
chars = ["h","e","l","l","o"]
I tried creating a function called array, to do this, but this didn't work.
I then tried this:
def getMessage():
file = open("file.txt", "r")
data = file.readlines()
file.close()
words = []
for line in data:
for word in line:
words.append(word)
return words
Any ideas?
You can use builtin list() function:
>>> list("A string")
['A', ' ', 's', 't', 'r', 'i', 'n', 'g']
In your case, you can call list(getMessage()) to convert the contents of the file to chars.
You can try something like this:
word="hello"
result = []
result[:0] = word
print(result)
Now result will be ['h', 'e', 'l', 'l', 'o']
Let's say I have a file containing this:
xxoxoxoxox
xxxxxxxxxx
xoxoxoxoxo
ooxoxoxoxo
and I want to iterate through each character and line and store them.
I know that if the characters are separated by spaces, I can do this:
mylist=[]
with open("myfile.txt") as myfile:
for line in file:
line = line.strip().split(" ")
first = line[0]
second = line[1]
alist = [first, second]
mylist.append(alist)
But how do I do something like this without spaces as delimiters? I tried .split()
and
for line in file:
for char in line:
But neither seems to work.
Thanks in advance for any help!
Here is a small snippet that might be useful to you.
Say you have a file called 'doc.txt' with two lines:
kisskiss
bangbang
With the following python script:
with open('doc.txt', 'r') as f:
all_lines = []
# loop through all lines using f.readlines() method
for line in f.readlines():
new_line = []
# this is how you would loop through each alphabet
for chars in line:
new_line.append(chars)
all_lines.append(new_line)
The output is:
>>> all_lines
Out[94]:
[['k', 'i', 's', 's', 'k', 'i', 's', 's', '\n'],
['b', 'a', 'n', 'g', 'b', 'a', 'n', 'g', '\n']]
More "pythonic" way:
def t():
r = []
with open("./test_in.txt") as f_in:
[r.extend(list(l)) for l in f_in]
return r
Note that you can't use return [r.extend(list(l)) for l in f_in] because extend returns None.
I have a list of words but I need to take the last item off the list, perform a function with the rest of the list then replace the last item. But for some reason when i go to to replace the last item, it does this...
>>> List = ['this', 'is', 'a', 'list']
>>> last = List[-1]
>>> others = List[:-1]
>>> others += last
>>> print others
['this', 'is', 'a', 'l', 'i', 's', 't']
Is there any way I can concatenate the list called last onto others but have it just one single element.
Try using append
others.append(last)
You can further simplify the code by doing this:
last = List.pop()
This removes the last element or List if no parameter is specified, and saves it in the variable last for you
Use append instead of +=:
>>> others.append(last)
Use:
others.append(last)
instead of
others += last
This is because:
When you are doing
list += ["somethingHere"]
it's equivalent to
list.extend(["somethingHere"])
According to the doc,
list.extend(L) = Extend the list by appending all the items in the given list
but
list.append(x) = Add an item to the end of the list
And what you need here is to " add an item " not to " append all the items in the given list " (all characters in this case.)
Please use one following:
others += [last]
others.append(last)
+ for list instances is iterating over element being added to it, thus iterating over string you get list of characters.
others += 'string'
others += ['s', 't', 'r', 'i', 'n', 'g']