Load raw data into array as strings - python

So I understand how to manipulate a text file and move data in and out of the program, but I'm trying to take raw data in a text file, and load them into an array that is originally empty, how would I make this approach?
Assume my raw data contains 3 words, I want to place those words into a variable called Array. The raw data of the text file contains the 3 following words: ' Apple Banana Orange '. I would like it to load into the array as: Array = ["Apple", "Banana", "Orange"]. How would you approach this?
with open("C:\\Users\\NameList.txt","r") as f:
Array = []
nameList = f.readlines(Array)
Am aware the code is wrong, but I'm not sure how to fix even after reading so much.

If your input test.txt is like below:
Apple Banana Orange
This is the solution you are looking for.
with open("test.txt","r")as f:
text = f.readlines()
Array = text[0].split()
In case you have more than 1 line, you can use this one:
with open("test.txt","r")as f:
text = f.read().splitlines()
Array = [i.split() for i in text]

This will read all the lines in your file:
with open("C:\\Users\\NameList.txt","r")as f:
lines = f.read().splitlines()
Array = list()
for line in lines:
Array.append(line)
print(Array)
for item in Array:
if 'Apple' == item:
print(item)
Output:
#first loop
['Apple', 'Banana', 'Orange']
#second loop
Apple

Related

What's the fastest way to convert a list into a python list?

Say I have a list like such:
1
2
3
abc
What's the fastest way to convert this into python list syntax?
[1,2,3,"abc"]
The way I currently do it is to use regex in a text editor. I was looking for a way where I can just throw in my list and have it convert immediately.
Read the file, split into lines, convert each line if numeric.
# Read the file
with open("filename.txt") as f:
text = f.read()
# Split into lines
lines = text.splitlines()
# Convert if numeric
def parse(line):
try:
return int(line)
except ValueError:
return line
lines = [parse(line) for line in lines]
If it's in a text file like you mentioned in the comments then you can simply read it and split by a new line. For example, your code would be:
with open("FileName.txt", "r") as f:
L = f.read().split("\n")
print(L)
Where L is the list.
Now, if your looking for the variables to be of the correct type instead of all being strings, then you can loop through each in the list to check. For example, you could do the following:
for i in range(len(L)):
if L[i].isnumeric():
L[i] = int(L[i])

How to save data from text file to python dictionary and select designation data

Example of data in txt file:
apple
orange
banana
lemon
pears
Code of filtering words with 5 letters without dictionary:
def numberofletters(n):
file = open("words.txt","r")
lines = file.readlines()
file.close()
for line in lines:
if len(line) == 6:
print(line)
return
print("===================================================================")
print("This program can use for identify and print out all words in 5
letters from words.txt")
n = input("Please Press enter to start filtering words")
print("===================================================================")
numberofletters(n)
My question is how create a dictionary whose keys are integers and values the English words with that many letters and Use the dictionary to identify and print out all the 5 letter words?
Imaging with a huge list of words
Sounds like a job for a defaultdict.
>>> from collections import defaultdict
>>> length2words = defaultdict(set)
>>>
>>> with open('file.txt') as f:
... for word in f: # one word per line
... word = word.strip()
... length2words[len(word)].add(word)
...
>>> length2words[5]
set(['lemon', 'apple', 'pears'])
If you care about duplicates and insertion order, use a defaultdict(list) and append instead of add.
you can make your for loop like this:
for line in lines:
line_len = len(line)
if line_len not in dicword.keys():
dicword.update({line_len: [line]})
else:
dicword[line_len].append(line)
Then you can get it by just doing dicword[5]
If I understood, you need to write filter your document and result into a file. For that you can write a CSV file with DictWriter (https://docs.python.org/2/library/csv.html).
DictWriter: Create an object which operates like a regular writer but maps dictionaries onto output rows.
BTW, you will be able to store and structure your document
def numberofletters(n):
file = open("words.txt","r")
lines = file.readlines()
file.close()
dicword = {}
writer = csv.DictWriter(filename, fieldnames=fieldnames)
writer.writeheader()
for line in lines:
if len(line) == 6:
writer.writerow({'param_label': line, [...]})
return
I hope that help you.

How to create a list from a text file in Python

I have a text file called "test", and I would like to create a list in Python and print it. I have the following code, but it does not print a list of words; it prints the whole document in one line.
file = open("test", 'r')
lines = file.readlines()
my_list = [line.split(' , ')for line in open ("test")]
print (my_list)
You could do
my_list = open("filename.txt").readlines()
When you do this:
file = open("test", 'r')
lines = file.readlines()
Lines is a list of lines. If you want to get a list of words for each line you can do:
list_word = []
for l in lines:
list_word.append(l.split(" "))
I believe you are trying to achieve something like this:
data = [word.split(',') for word in open("test", 'r').readlines()]
It would also help if you were to specify what type of text file you are trying to read as there are several modules(i.e. csv) that would produce the result in a much simpler way.
As pointed out, you may also strip a new line(depends on what line ending you are using) and you'll get something like this:
data = [word.strip('\n').split(',') for word in open("test", 'r').readlines()]
This produces a list of lines with a list of words.

Reading inputs written in different lines from a file and storing each input inside a list

I'm trying to read a file named one.txt which contains the following:
hat
cow
Zu6
This is a sentence
and I'm trying to store each string written on each line inside a list. For example, my output list should contain the following elements:
['hat', 'cow', 'Zu6', 'This is a sentence']
Here's my approach for doing this:
def first(ss):
f = open(ss, 'r')
text = f.readline()
f.close()
lines = []
li = [lines.append(line) for line in text]
print li
first('D:\\abc\\1\\one.txt')
However, when I try to print li, here's what I get as the output:
[None, None, None, None]
What's wrong with my approach?
print list(open("my_text.txt"))
is probably a pretty easy way to do it ...
ofc people are gonna come screaming about dangling pointers so for the sake of good habits
with open("my_text.txt") as f:
print list(f)
alternatively
f.readlines()
you might need to strip off some newline characters
[line.strip() for line in f]

Create a column from a CSV list in Python 3

What I have is a CSV file where the header is "keyword" and each cell under the header contains text so that it that looks like this:
Keyword
Lions Tigers Bears
Dog Cat
Fish
Shark Guppie
What I am trying to do is to parse each of the phrases in that list into individual words so that the end product looks like this:
Keyword
Lion
Tigers
Bear
Dog
Cat...
Right now, my code takes the CSV file and splits the list into individual parts but still does not create a uniform column.
datafile = open(b'C:\Users\j\Desktop\helloworld.csv', 'r')
data = []
for row in datafile:
data.append(row.strip().split(","))
white = row.split()
print (white)
and my output looks like this:
['Keyword']
['Lion', 'Tigers']
['Dolphin', 'Bears', 'Zebra']
['Dog', 'Cat']
I know that a possible solution would involve the use of lineterminator = '\n' but I am not sure how to incorporate that into my code. Any help would be very much appreciated!
** EDITED -- the source CSV does not have commas separating the words within each phrase
Use extend instead of append on lists to add all items from a list to another one:
datafile = open(b'C:\Users\j\Desktop\helloworld.csv', 'r')
data = []
for row in datafile:
data.extend(row.strip().split())
print(data)
To get rid of further whitespace around the individual entries, use
datafile = open(b'C:\Users\j\Desktop\helloworld.csv', 'r')
data = []
for row in datafile:
data.extend(item.strip() for item in row.split())
print(data)
Also, to read files safely, you can make use of a with statement (you won't have to take care of closing your files anymore):
with open('C:\Users\j\Desktop\helloworld.csv', 'r') as datafile:
data = []
for row in datafile:
data.extend(item.strip() for item in row.split())
print(data)
EDIT: After OP clarification, I removed the "," argument in split to split on whitespace rather than on commata.
You should be able to use this code to read your file. Replace file name with what you have. My file content is exactly what you posted above.
keyword = "Keyword"
with open("testing.txt") as file:
data = file.read().replace("\n", " ").split(" ")
for item in data:
if item == keyword:
print("%s" % keyword)
else:
print(" %s" % item)
Output:
Keyword
Lions
Tigers
Bears
Dog
Cat
Fish
Shark
Guppie
Keyword
Dog
Something
Else
Entirely
You just need to split the read:
with open("in.txt","r+") as f:
data = f.read().split()
f.seek(0) # go back to start of file
f.write("\n".join(data)) # write new data to file
['Keyword', 'Lions', 'Tigers,', 'Bears', 'Dog', 'Cat', 'Fish', 'Shark', 'Guppie']

Categories

Resources