Cannot read data from textfile [duplicate] - python

This question already has answers here:
How to read a file line-by-line into a list?
(28 answers)
Closed 2 years ago.
the idea is to write a python code reading data from a text file indicated below.
https://www.minorplanetcenter.net/iau/MPCORB/CometEls.txt
Further on, I would like to filter comet-data like name magnitude etc. etc.
Right now my problem is getting data output.
My code is:
import os.path
word_dict = {}
scriptpath = os.path.dirname(__file__)
filename = os.path.join(scriptpath, 'CometEls.txt','r')
for line in filename:
line = line.strip()
relation = line.split(' ')
word_dict[relation[0]] = relation[0:20]
while True:
word = input('Comet name : ')
if word in word_dict:
print ('Comets in list :' , word_dict[word])
print(filename) #show file location
else:
print( 'No comet data!')
print(word_dict) #show data from dictionary
As you can see data in my dictionary isn't the comet-data.
It should be
Typing in "a"
Theoretically the code works, the problem is creating the dictionary?
Maybe I'm completely wrong, but it doesn't work neither with tuples or lists, or it's better to copy data into a .csv file?
Best regards

You saved the file path to the filename variable, but did not open the file for reading. You should open file, and then read it:
import os.path
word_dict = {}
scriptpath = os.path.dirname(__file__)
file_path = os.path.join(scriptpath, 'CometEls.txt')
with open(file_path, 'r') as commet_file:
for line in commet_file:
line = line.strip()
relation = line.split(' ')
word_dict[relation[0]] = relation[0:20]
while True:
word = input('Comet name : ')
if word in word_dict:
print ('Comets in list :' , word_dict[word])
print(file_path)
else:
print('No comet data!')
print(word_dict) #show data from dictionary
Also, your example has wrong margins near in while block, please check it.

Related

Python - define a function to manage files

I need to define a fucntion that will, in short:
Open and grab the content from an existing file
Transform that content
Create a new file
Write that new content in this new file
Print the content of the new file
I'm a complete begginer, but I got this until now. How can I improve this?
def text():
#open the existing file
text_file = open('music.txt', 'r')
#reads the file
reading = text_file.read ()
#this turns everything to lower case, counts the words and displays the list vertically
from collections import Counter
new_text = reading.lower()
list_words = Counter(new_text.split())
ordered_list = sorted(list_words.items())
#creates a new file and writes the content there
with open('finheiro_saida.txt', 'x') as final_file:
for i in ordem:
finheiro_saida.write(str(i) + '\n')
#not sure how to open this new file and print its content, when I tried it says the new file doesn't exist in the directory - tried everything.
final = open('C:/Users/maria/OneDrive/Documents/SD_DTM/ficheiro_saida.txt', 'r')
read_file = final.read ()
print(read_file)
You can open the new file and print its content the same way you read and wrote to it!
# ...After all your previous code...
with open('finheiro_saida.txt', 'r') as final_file:
final_file_content = final_file.read()
print(final_file_content)
Fixed some syntax error in your code.
you can display the the same way you read.
Also provide all imports to the start of the file.
you can also read all lines from the file as a list using file.readlines()
from collections import Counter
def text():
# open the existing file
text_file = open("music.txt", "r")
# reads the file
reading = text_file.read()
# this turns everything to lower case, counts the words and displays the list vertically
new_text = reading.lower()
list_words = Counter(new_text.split())
ordered_list = sorted(list_words.items())
# creates a new file and writes the content there
file_name = "finheiro_saida.txt"
with open("finheiro_saida.txt", "x") as final_file:
for i in ordered_list:
final_file.write(str(i) + "\n")
return file_name
def display(final_file_name):
with open(final_file_name) as file:
print(file.read())
final_file_name = text()
display(final_file_name)

Creating a search function in a list from a text file

everyone. I have a Python assignment that requires me to do the following:
Download this CSV fileLinks to an external site of female Oscar winners (https://docs.google.com/document/d/1Bq2T4m7FhWVXEJlD_UGti0zrIaoRCxDfRBVPOZq89bI/edit?usp=sharing) and open it into a text editor on your computer
Add a text file to your sandbox project named OscarWinnersFemales.txt
Copy and paste several lines from the original file into your sandbox file. Make sure that you include the header.
Write a Python program that does the following:
Open the file and store the file object in a variable
Read the entire contents line by line into a list and strip away the newline character at the end of each line
Using list slicing, print lines 4 through 7 of your file
Write code that will ask the user for an actress name and then search the list to see if it is in there. If it is it will display the record and if it is not it will display Sorry not found.
Close the file
Below is the code I currently have. I've already completed the first three bullet points but I can't figure out how to implement a search function into the list. Could anyone help clarify it for me? Thanks.
f = open('OscarsWinnersFemales.txt')
f = ([x.strip("\n") for x in f.readlines()])
print(f[3:7])
Here's what I tried already but it just keeps returning failure:
def search_func():
actress = input("Enter an actress name: ")
for x in f:
if actress in f:
print("success")
else:
print("failure")
search_func()
I hate it when people use complicated commands like ([x.strip("\n") for x in f.readlines()]) so ill just use multiple lines but you can do what you like.
f = open("OscarWinnersFemales.txt")
f = f.readlines()
f.close()
data = {} # will list the actors and the data as their values
for i, d in enumerate(data):
f[i] = d.strip("\n")
try:
index, year, age, name, movie = d.split(",")
except ValueError:
index, year, age, name, movie, movie2 = d.split(",")
movie += " and " + movie2
data[name] = f"{index}-> {year}-{age} | {movie}"
print(f[3:7])
def search_actr(name):
if name in data: print(data[name])
else: print("Actress does not exist in database. Remember to use captols and their full name")
I apologize if there are any errors, I decided not to download the file but everything I wrote is based off my knowledge and testing.
I have figured it out
file = open("OscarWinnersFemales.txt","r")
OscarWinnersFemales_List = []
for line in file:
stripped_line = line.strip()
OscarWinnersFemales_List.append(stripped_line)
file.close()
print(OscarWinnersFemales_List[3:7])
print()
actress_line = 0
name = input("Enter An Actress's Name: ")
for line in OscarWinnersFemales_List:
if name in line:
actress_line = line
break
if actress_line == 0:
print("Sorry, not found.")
else:
print()
print(actress_line)

How to edit specific line for all text files in a folder by python?

Here below is my code about how to edit text file.
Since python can't just edit a line and save it at the same time,
I save the previous text file's content into a list first then write it out.
For example,if there are two text files called sample1.txt and sample2.txt in the same folder.
Sample1.txt
A for apple.
Second line.
Third line.
Sample2.txt
First line.
An apple a day.
Third line.
Execute python
import glob
import os
#search all text files which are in the same folder with python script
path = os.path.dirname(os.path.abspath(__file__))
txtlist = glob.glob(path + '\*.txt')
for file in txtlist:
fp1 = open(file, 'r+')
strings = [] #create a list to store the content
for line in fp1:
if 'apple' in line:
strings.append('banana\n') #change the content and store into list
else:
strings.append(line) #store the contents did not be changed
fp2 = open (file, 'w+') # rewrite the original text files
for line in strings:
fp2.write(line)
fp1.close()
fp2.close()
Sample1.txt
banana
Second line.
Third line.
Sample2.txt
First line.
banana
Third line.
That's how I edit specific line for text file.
My question is : Is there any method can do the same thing?
Like using the other functions or using the other data type rather than list.
Thank you everyone.
Simplify it to this:
with open(fname) as f:
content = f.readlines()
content = ['banana' if line.find('apple') != -1 else line for line in content]
and then write value of content to file back.
Instead of putting all the lines in a list and writing it, you can read it into memory, replace, and write it using same file.
def replace_word(filename):
with open(filename, 'r') as file:
data = file.read()
data = data.replace('word1', 'word2')
with open(filename, 'w') as file:
file.write(data)
Then you can loop through all of your files and apply this function
The built-in fileinput module makes this quite simple:
import fileinput
import glob
with fileinput.input(files=glob.glob('*.txt'), inplace=True) as files:
for line in files:
if 'apple' in line:
print('banana')
else:
print(line, end='')
fileinput redirects print into the active file.
import glob
import os
def replace_line(file_path, replace_table: dict) -> None:
list_lines = []
need_rewrite = False
with open(file_path, 'r') as f:
for line in f:
flag_rewrite = False
for key, new_val in replace_table.items():
if key in line:
list_lines.append(new_val+'\n')
flag_rewrite = True
need_rewrite = True
break # only replace first find the words.
if not flag_rewrite:
list_lines.append(line)
if not need_rewrite:
return
with open(file_path, 'w') as f:
[f.write(line) for line in list_lines]
if __name__ == '__main__':
work_dir = os.path.dirname(os.path.abspath(__file__))
txt_list = glob.glob(work_dir + '/*.txt')
replace_dict = dict(apple='banana', orange='grape')
for txt_path in txt_list:
replace_line(txt_path, replace_dict)

Loop through folder full of text files, grab values for variables, match & populate CSV with storeID and variable name

super new to Python, and looking for some guidance. I'm trying to
loop through hundreds of text files in a folder (one for each store), and generate a CSV file with the store ID (given in the title of the text document i.e. xxx2902ncjc), and various parameters about the store (i.e. maxPeople=31, or space_temp=78, etc.). Each text file may have difference parameters depending on the location, so I've captured all of the unique variables in the third for loop below. I've captured all of the store IDs in the second for-loop. That's all I've gotten so far.
Challenges that I'm seeing are 1) figuring out how to import this all to Excel, 2) Finding someway to store IDs (which are at this point a slice of each filename) with the correct parameters 3) Finding a way to have excel match up the Store ID and the parameters to the variables.
I honestly have no idea what I should be doing next. Any and all help would be very appreciated as I am a suuuper novice. Cheers.
import os, sys, glob
path = r"C:\Users\XXXXX" #insert folder for data here
dirs=os.listdir(path)
fullfilenames=[]
variablelist=[]
allvariables=[]
variables=[]
for file in os.listdir(path):
if ".prop" in file:
fullfilenames.append(path+'\\'+file)
for name in fullfilenames: #create list of StoreIDs
index_of_c = name.index('qPA')
file_name= name[index_of_c:] #cuts off path
file_name=file_name.rsplit(".",1)[0] #removes extension
SiteID= file_name[4:] #splits filename into Site ID
print (SiteID) #prints SiteID
for file in fullfilenames:
f = open(file,'r') #opens the file and enters reading mode
content=f.readlines() #reads each line of file and seperates based on whitespace
for line in content:
variables.append(line.split('=')[0]) #splits up each line of each file, specified before the "="
for variable in variables:
if variable not in allvariables: #checks if variable is included in the variable list
allvariables.append(variable) #if variabe isn't include in the list, it adds it to list
def createkeys():
print(allvariables)
print(type(allvariables))
print(len(allvariables))
import os, sys, glob, re
path = r"C:\Users\mcantwell\Desktop\Projects\kohls_prop" #insert folder for data here
outfile = r"C:\Users\mcantwell\Desktop\Projects\kohls_prop.csv"
dirs=os.listdir(path)
fullfilenames=[]
variablelist=[]
allvariables=set()
variables=[]
for file in os.listdir(path):
if ".prop" in file:
fullfilenames.append(path+'\\'+file)
for file in fullfilenames:
f = open(file,'r') #opens the file and enters reading mode
content=f.readlines() #reads each line of file and seperates based on whitespace
for line in content:
line_split = line.split('=') #splits up each line of each file, specified before the "="
if len(line_split) == 2:
variable = line_split[0]
allvariables.add(variable)
out = open(outfile, 'w')
def writerow(row):
out.write(', '.join(row))
out.write('\n')
writerow(['SiteID'] + list(allvariables))
for file in fullfilenames:
m = re.search('qPAC(\d+)', file)
SiteID = m.group(1)
f = open(file,'r') #opens the file and enters reading mode
content=f.readlines() #reads each line of file and seperates based on whitespace
data={}
for line in content:
line_split = line.strip().split('=') #splits up each line of each file, specified before the "="
if len(line_split) == 2:
variable = line_split[0]
value = line_split[1]
data[variable] = value
values = [SiteID] + [data.get(variable, '') for variable in allvariables]
writerow(values)
print(allvariables)
print(type(allvariables))
print(len(allvariables))

Checking data in a file for duplicates (Python)

I am trying to make a list of topics for another project to use and I am storing the topics in Topics.txt. However, when the topics are stored in the file, I do not want duplicate topics. So when I am saving my topics to my Topics.txt file, I also save them to a Duplicates.txt file. What I want to do is create a conditional statement that won't add topics to Topics.txt if the topics are in the Duplicates.txt. My problem is, I don't know how I could create a conditional statement that could check if the topic is listed in Duplicates.txt. A problem may arise if you scan for keywords such as "music", seeing that "electro-music" contains the word "music".
Entry = input("Enter topic: ")
Topic = Entry + "\n"
Readfilename = "Duplicates.txt"
Readfile = open(Readfilename, "r")
Readdata = Readfile.read()
Readfile.close()
if Topic not in Duplicates:
Filename = "Topics.txt"
File = open(Filename, "a")
File.append(Topic)
File.close()
Duplicate = Topic + "\n"
Readfile = open(Readfilename, "a")
Readfile.append(Topic)
Readfile.close()
You can read a file line by line which would result in a solution like this one
Entry = input("Enter topic: ")
Topic = Entry + "\n"
Readfilename = "Duplicates.txt"
found=False
with open(Readfilename, "r") as Readfile:
for line in Readfile:
if Topic==line:
found=True
break # no need to read more of the file
if not found:
Filename = "Topics.txt"
with open(Filename, "a") as File:
File.write(Topic)
with open(Readfilename, "a") as Readfile:
Readfile.write(Topic)
You can store your topics in a set. A set is collection of unique items.
topics = {'Banjo', 'Guitar', 'Piano'}
You can check for membership using:
>>> 'Banjo' in topics
True
You add new things to a set via .add()
topics.add('Iceskating')
>>> topics
set(['Banjo','Guitar', 'Piano', 'Iceskating'])
Python 3 Docs on sets here. The tutorial page on sets is here.

Categories

Resources