python store data from file to variables - python

I’m new to python and I’m taking my first steps to create some scripts.
I want to access a file and store the items into a list and access each item as its own variable.
The format of the file is .txt and it’s as such but not limited to 3 columns it could be 4 or more.
textData,1
textData,2
textData,3,moreData
textData,4,moreData4
textData,5
I know how to read and append to a list and access individual items starting with [0] but when I do that I get textData, 1 for [0] and I only want textData on its own and 1 on its own and so on as I loop through the file.
Below is my start of this:
file = open('fileName','r')
list = []
for items in file:
list.append(items)
print(list[0])
Thank you for taking the time to read and provide direction.

You need to split the lines:
my_list = []
for lines in file:
my_list.append(lines.split(','))
print(my_list)

When you loop over a file object, you get each line as a str. You can then get each word by using the .split method, which returns a list of strs. As your items are comma-separated, we split on ','. We do not want to append this list to the overall list, but rather add all elements of the list to the overall list, hence the +=:
file = open('fileName', 'r')
mylist = []
for line in file:
words = line.split(',')
mylist += words
print(mylist[0])
Also, avoid using list as a variable name, as this is the name of the builtin list function.

Related

TypeError: write() argument must be str, not list - How do I convert a list into a text file to be stored?

houses = []
#print(houses)
#Creates an empty list for the houses
f = open("houses.txt", "r+")
#Creates a variable called "f" which opens the houses.txt file and imports it r+ = Read and + means you can append on the end
lines = f.readlines()
#The readlines() method returns a list containing each line in the file as a list item. Use the hint parameter to limit the number of lines returned.
#The for loop grabs each line of text from the file, and splits the houses and scores and makes them separate. It runs for as many lines are in the text file
for line in lines:
#The append adds the stripped and split code separately in a list
#.strip .split separates the houses and scores into separate strings
houses.append(line.strip().split())
for i in range(len(houses)):
#Loops for how many houses are in the list
houses[i][1] = int(houses[i][1])
#Turns the second part of the list into an integer
print(houses)
This part of the code imports houses (teams) from the text file which is laid out like this:
StTeresa 0
StKolbe 0
StMary 0
StAnn 0
I created a function to save the points. I would basically like it to take the houses and scores from the list in the program. To do so, it will delete all the contents in the text file, and then I would like it to rewrite it in the same format as the original text file to keep the updated house scores.
def save():
f.truncate(0)
f.write(str(houses))
I tried this but the output is:
This
Can anyone help me to rewrite the text file to include the updated scores and be in the same format as the text file orignally was?
This should accomplish what you want:
def save():
with open("houses.txt", "w") as file:
for index, line_content in enumerate(houses):
houses[index][1] = str(houses[index][1])
file.write("\n".join(map(lambda sub_list: " ".join(sub_list), houses)))
However, as you can see, this is quite hacky.
So I would have two main recommendations for your code:
Use open() within a with statement. Currently your code does not reliably close the file it opens, which can cause your code to do unexpected things (for example when an exception is thrown in the middle of your processing). Therefore, it is recommended practice to use with (further information on this can be found here)
Instead of using lists within a list, you could use tuples:
for line in lines:
# The append adds the stripped and split code separately in a list
# .strip .split separates the houses and scores into separate strings
house_name, house_score = tuple(line.strip().split())
houses.append((house_name, int(house_score)))
This gives you something like this: [("StTeresa", 0), ("StKolbe", 0), ("StMary", 0), ("StAnn", 0)]. Tuples in lists have the advantage that you can unpack them in loops, which makes it easier to handle them compared to a list within a list:
for index, (house_name, house_score) in enumerate(houses):
# do something with the score
houses[index] = (house_name, updated_score)
Another option is to follow what Barmar suggested and use the csv module, which is part of the standard library.
As a bonus: enumerate() provides you the indices of an iterable, so you can easily loop over them. So instead of
for i in range(len(houses)):
#Loops for how many houses are in the list
you can write
for i, house in enumerate(houses):
# Loop over all the houses
which makes the code a little easier to read and write.
So with all my suggestions combined you could have a save() function like this:
def save():
with open("houses.txt", "w") as file:
for house_name, house_score in houses:
file.write(f"{house_name} {house_score}\n")
Hope this helps :)

Tokenize my CSV in one list rather than separate using Python

I want to tokenize my CSV in one list rather than a separate list?
with open ('train.csv') as file_object:
for trainline in file_object:
tokens_train = sent_tokenize(trainline)
print(tokens_train)
This is how I am getting the output:
['2.1 Separated of trains']
['Principle: The method to make the signal is different.']
['2.2 Context']
I want all of them in one list
['2.1 Separated of trains','Principle: The method to make the signal is different.','2.2 Context']
Since sent_tokenize() returns a list, you could simply extend a starting list each time.
alltokens = []
with open ('train.csv') as file_object:
for trainline in file_object:
tokens_train = sent_tokenize(trainline)
alltokens.extend(tokens_train)
print(alltokens)
Or with a list comprehension:
with open ('train.csv') as file_object:
alltokens = [token for trainline in file_object for token in sent_tokenize(trainline)]
print(alltokens)
Both solutions will work even if sent_tokenize() returns a list longer than 1.
Initialize an empty list
out = []
And inside the loop append items to it.
out.append(tokens_train)
Maybe you have to modify your tokenizer too.

I have a list of lists, I want to group by 10

I've got a raw text file with a line-separated list of EAN numbers, which I'm adding to a list (as a string) as follows:
listofEAN = []
with open('Data', newline='\r\n') as inputfile:
for row in csv.reader(inputfile):
listofEAN.append(row)
This creates a "list of lists" (I'm not sure why it doesn't create a single list?) in the format:
[['0075678660924'], ['0093624912613'], ['3299039990322'], ['0190295790394'], ['0075678660627'], ['0075678661150'], ...]
I'm trying to do transform the list into a list of lists with 10 EAN's each. So running listofEAN[0] would return the first 10 EAN's, and so forth.
I'm afraid I'm struggling to do this - I presume I need to use a loop of some kind, but I'm having trouble with creating a loop and combining the loop operations with the list syntax.
Any pointers would be greatly appreciated.
EDIT: Similar to the query here: How do you split a list into evenly sized chunks? but the corrections here to the way I'm importing the list is of particular interest. Thank you!
A row in the CSV file is always a list, even for a file with just one column. You don't really need to use a CSV reader when you have just one value per line; just strip the whitespace from each line to create a flat list, and use the standard universal newline support:
with open('Data') as inputfile:
# create a list of non-empty lines, with whitespace removed
stripped = (line.strip() for line in inputfile)
listofEAN = [line for line in stripped if line]
Now it is trivial to make that into groups of a fixed size:
per_ten = [listofEAN[i:i + 10] for i in range(0, len(listofEAN), 10)]

How to create a list of string in nth position of every line in Python

What would be a pythonic way to create a list of (to illustrate with an example) the fifth string of every line of a text file, assuming it ressembles something like this:
12, 27.i, 3, 6.7, Hello, 438
In this case, the script would add "Hello" (without quotes) to the list.
In other words (to generalize), with an input "input.txt", how could I get a list in python that takes the nth string (n being a defined number) of every line?
Many thanks in advance!
You could use the csv module to read the file, and store all items in the fifth column in a list:
import csv
with open(my_file) as f:
lst = [row[4] for row in csv.reader(f)]
If its a text file it can be as simple as:
with open(my_file, 'r') as f:
mylist = [line.split(',')[4] for line in f] # adds the 5th element of split to my_list
Since you mentioned that you are using a .txt file, you can try this:
f = open('filename.txt').readlines()
f = [i.strip('\n').split(",") for i in f]
new_f = [i[4] for i in f]
This may not be the most efficient solution, but you could also just hard code it e.g. create a variable equivalent to zero, add one to the variable for each word in the line, and append the word to a list when variable = 5. Then reset the variable equal to zero.

Trouble with turning .txt file into lists

In a project I am doing I am storing the lists in a .txt file in order to save space in my actual code. I can turn each line into a separate list but I need to turn multiple lines into one list where each letter is a different element. I would appreciate any help I can get. The program I wrote is as follows:
lst = open('listvariables.txt', 'r')
data = lst.readlines()
for line in data:
words = line.split()
print(words)
Here is a part of the .txt file I am using:
; 1
wwwwwwwwwww
wwwwswwwwww
wwwsswwwssw
wwskssssssw
wssspkswssw
wwwskwwwssw
wwwsswggssw
wwwswwgwsww
wwsssssswww
wwssssswwww
wwwwwwwwwww
; 2
wwwwwwwww
wwwwwsgsw
wwwwskgsw
wwwsksssw
wwskpswww
wsksswwww
wggswwwww
wssswwwww
wwwwwwwww
If someone could make the program print out two lists that would be great.
You can load the whole file and turn it into a char-list like:
with open('listvariables.txt', 'r') as f
your_list = list(f.read())
I'm not sure why you want to do it, tho. You can iterate over string the same way you can iterate over a list - the only advantage is that list is a mutable object but you wouldn't want to do complex changes to it, anyway.
If you want each character in a string to be an element of the final list you should use
myList = list(myString)
If I understand you correctly this should work:
with open('listvariables.txt', 'r') as my_file:
my_list = [list(line) for line in my_file.read().splitlines()]

Categories

Resources