I've got a text file with some elements as such;
0,The Hitchhiker's Guide to Python,Kenneth Reitz,2/4/2012,0
1,Harry Potter,JK Rowling,1/1/2010,8137
2,The Great Gatsby,F. Scott Fitzgerald,1/2/2010,0
3,To Kill a Mockingbird,Harper Lee,1/3/2010,1828
The last element of these lists determine which user has taken out the given book. If 0 then nobody has it.
I want a code to replace the ',0' of any given line into an input 4 digit number to show someone has taken out the book.
I've used .replace to change it from 1828 e.g. into 0 however I don't know how I can change the last element of specific line from 0 to something else.
I cannot use csv due to work/education restrictions so I have to leave the file in .txt format.
I also can only use Standard python library, therefore no pandas.
You can capture txt using read_csv into a dataframe:
import pandas as pd
df = pd.read_csv("text_file.txt", header=None)
df.columns = ["Serial", "Book", "Author", "Date", "Issued"]
print(df)
df.loc[3, "Issued"] = 0
print(df)
df.to_csv('text_file.txt', header=None, index=None, sep=',', mode='w+')
This replaces the third book issued count to 0.
Serial Book Author Date \
0 0 The Hitchhiker's Guide to Python Kenneth Reitz 2/4/2012
1 1 Harry Potter JK Rowling 1/1/2010
2 2 The Great Gatsby F. Scott Fitzgerald 1/2/2010
3 3 To Kill a Mockingbird Harper Lee 1/3/2010
Issued
0 0
1 8137
2 0
3 1828
Serial Book Author Date \
0 0 The Hitchhiker's Guide to Python Kenneth Reitz 2/4/2012
1 1 Harry Potter JK Rowling 1/1/2010
2 2 The Great Gatsby F. Scott Fitzgerald 1/2/2010
3 3 To Kill a Mockingbird Harper Lee 1/3/2010
Issued
0 0
1 8137
2 0
3 0
Edit after comment:
In case you only need to use python standard libraries, you can do something like this with file read:
import fileinput
i = 0
a = 5 # line to change with 1 being the first line in the file
b = '8371'
to_write = []
with open("text_file.txt", "r") as file:
for line in file:
i += 1
if (i == a):
print('line before')
print(line)
line = line[:line.rfind(',')] + ',' + b + '\n'
to_write.append(line)
print('line after edit')
print(line)
else:
to_write.append(line)
print(to_write)
with open("text_file.txt", "w") as f:
for line in to_write:
f.write(line)
File content
0,The Hitchhiker's Guide to Python,Kenneth Reitz,2/4/2012,0
1,Harry Potter,JK Rowling,1/1/2010,8137
2,The Great Gatsby,F. Scott Fitzgerald,1/2/2010,84
3,To Kill a Mockingbird,Harper Lee,1/3/2010,7895
4,XYZ,Harper,1/3/2018,258
5,PQR,Lee,1/3/2019,16
gives this as output
line before
4,XYZ,Harper,1/3/2018,258
line after edit
4,XYZ,Harper,1/3/2018,8371
["0,The Hitchhiker's Guide to Python,Kenneth Reitz,2/4/2012,0\n", '1,Harry Potter,JK Rowling,1/1/2010,8137\n', '2,The Great Gatsby,F. Scott Fitzgerald,1/2/2010,84\n', '3,To Kill a Mockingbird,Harper Lee,1/3/2010,7895\n', '4,XYZ,Harper,1/3/2018,8371\n', '5,PQR,Lee,1/3/2019,16\n', '\n']
you can try this:
to_write = []
with open('read.txt') as f: #read input file
for line in f:
if int(line[-2]) ==0:
to_write.append(line.replace(line[-2],'1234')) #any 4 digit number
else:
to_write.append(line)
with open('output.txt', 'w') as f: #name of output file
for _list in to_write:
f.write(_list)
Related
I have a text file with a list of names:
Aaron
Abren
Adrian
Albert
When I run the following code:
import gender_guesser.detector as gender
d = gender.Detector()
file1 = open('names.txt','r')
count = 0
while True:
count += 1
line = file1.readline()
guess = d.get_gender(line)
print(line)
print(guess)
if not line:
break
print(count)
I get the following:
Aaron
unknown
Abren
unknown
Adrian
unknown
Albert
male
unknown
5
It looks like it is only able to evaluate the last name in the file (Albert), and I think it has to do with how it parses through the file. If I add a line break after Albert, it no longer detects Albert as male.
Any thoughts?
It looks like you have an issue with the line terminators. The library doesn't expect those.
Here's a working code snippet:
import gender_guesser.detector as gender
d = gender.Detector()
with open('names.txt') as fin:
for line in fin.readlines():
name = line.strip()
print(d.get_gender(name))
The main fix is adding line.strip().
Using with is just a best practice you should follow, but doesn't change the functionality.
The output is:
male
unknown
male
male
Like lets say i have text file data like this..
|-------|
|Arsenal|
|-------|
|2021
|-------|
|Rnd|A|W|D|L|Venu|Date|
|R1|Tottenham|1|0|0|Emirates|March|
|R2|Man utd|0|1|0|Old Trafford|March|
|Total|Average|1234|5678|
|Arsenal|
|-------|
|2020|
|-------|
|Rnd|A|W|D|L|Venu|Date|
|R1|Chelsea|1|0|0|Stamford Bridge|March|
|R2|Mancity|0|1|0|Ethiad|March|
|Total|Average|1234|5678|
I want to convert this file in to 2D array (list of list) without using pandas. And hoping for output like this
Arsenal 2021 R1 Tottenham 1 0 0 Emirates March
Arsenal 2021 R2 Man utd 0 1 0 Old Trafford March
Arsenal 2020 R1 Chelsea 1 0 0 Stamford Bridge March
Arsenal 2020 R2 Man city 0 1 0 Ethiad March
So here i need to ignore |----|, |Rnd|, |Total|Average|1234|5678|, and i need to make Arsenal and 2021 attached to the every row and Arsenal and 2020 to every row in next year..
I have applied for loop going every line by line and created the list of list. But i couldn't delete the header like (Rnd,T,W,D,L,Venu, Total) and total, average while going through line by line without using pandas...
You can use variable like first_part = True/False to run different code in loop.
You can also use next(file) to read next line(s) from file so in first part you can read more lines to get word and year and set first_part = False. In second part you has to only add this word and year to lines and check if line starts with |Total' to change first_part = True`.
Minimal working example.
I uses io to simulate file but you should use open().
text = '''|Arsenal|
|-------|
|2021
|-------|
|Rnd|A|W|D|L|Venu|Date|
|R1|Tottenham|1|0|0|Emirates|March|
|R2|Man utd|0|1|0|Old Trafford|March|
|Total|Average|1234|5678|
|Arsenal|
|-------|
|2020|
|-------|
|Rnd|A|W|D|L|Venu|Date|
|R1|Chelsea|1|0|0|Stamford Bridge|March|
|R2|Mancity|0|1|0|Ethiad|March|
|Total|Average|1234|5678|'''
import io
#fh = open('data.csv')
fh = io.StringIO(text)
first_part = True
for line in fh:
if first_part:
word = line.rstrip('\n').rstrip('|')
line = next(fh)
line = next(fh)
year = line.rstrip('\n').rstrip('|')
line = next(fh)
line = next(fh)
first_part = False
else:
if line.startswith('|Total|'):
first_part = True
else:
new_line = word + year + line
print(new_line, end='')
Result:
|Arsenal|2021|R1|Tottenham|1|0|0|Emirates|March|
|Arsenal|2021|R2|Man utd|0|1|0|Old Trafford|March|
|Arsenal|2020|R1|Chelsea|1|0|0|Stamford Bridge|March|
|Arsenal|2020|R2|Mancity|0|1|0|Ethiad|March|
with open('Arsenal.txt', 'r') as f:
for line in f:
if not line.startswith(('| --- |', '| Rnd |','| Totals |','| Averages |')) :
line= line.strip()
field= line.split('|')
print(field)
#furas this is my code I tried
I found the length and index and i want save all of them to new file:
example: index sentences length
my code
file = open("testing_for_tools.txt", "r")
lines_ = file.readlines()
for line in lines_:
lenght=len(line)-1
print(lenght)
for item in lines_:
print(lines_.index(item)+1,item)
output:
64
18
31
31
23
36
21
9
1
1 i went to city center, and i bought xbox5 , and some other stuff
2 i will go to gym !
3 tomorrow i, sill start my diet!
4 i achive some and i need more ?
5 i lost lots of weights؟
6 i have to , g,o home,, then sleep ؟
7 i have things to do )
8 i hope so
9 o
desired output and save to new file :
1 i went to city center, and i bought xbox5 , and some other stuff 64
2 i will go to gym ! 18
This can be achieved using the following code. Note the use of with ... as f which means we don't have to worry about closing the file after using it. In addition, I've used f-strings (requires Python 3.6), and enumerate to get the line number and concatenate everything into one string, which is written to the output file.
with open("test.txt", "r") as f:
lines_ = f.readlines()
with open("out.txt", "w") as f:
for i, line in enumerate(lines_, start=1):
line = line.strip()
f.write(f"{i} {line} {len(line)}\n")
Output:
1 i went to city center, and i bought xbox5 , and some other stuff 64
2 i will go to gym ! 18
If you wanted to sort the lines based on length, you could just put the following line after the first with block:
lines_.sort(key=len)
This would then give output:
1 i will go to gym ! 18
2 i went to city center, and i bought xbox5 , and some other stuff 64
I know it was asked already but the answers the super unclear
The first requirement is to open a file (sadly I have no idea how to do that)
The second requirement is a section of code that does the following:
Each line represents a single student and consists of a student number, a name, a section code and a midterm grade, all separated by whitespace
So I don't think i can target that element due to it being separate by whitespace?
Here is an excerpt of the file, showing line structure
987654322 Xu Carolyn L0101 19.5
233432555 Jones Billy Andrew L5101 16.0
555432345 Patel Amrit L0101 13.5
888332441 Fletcher Bobby L0201 18
777998713 Van Ryan Sarah Jane L5101 20
877633234 Zhang Peter L0102 9.5
543444555 Martin Joseph L0101 15
876543222 Abdolhosseini Mohammad Mazen L0102 18.5
I was provided the following hints:
Notice that the number of names per student varies.
Use rstrip() to get rid of extraneous whitespace at the end of the lines.
I don't understand the second hint.
This is what I have so far:
counter = 0
elements = -1
for sets in the_file
elements = elements + 1
if elements = 3
I know it has something to do with readlines() and the targeting the section code.
marks = [float(line.strip().split()[-1]) for line in open('path/to/input/file')]
average = sum(marks)/len(marks)
Hope this helps
Open and writing to files
strip method
Something like this?
data = {}
with open(filename) as f:#open a file
for line in f.readlines():#proceed through file lines
#next row is to split data using spaces and them skip empty using strip
stData = [x.strip() for x in line.split() if x.strip()]
#assign to variables
studentN, studentName, sectionCode, midtermGrade = stData
if sectionCode not in data:
data[sectionCode] = []
#building dict, key is a section code, value is a tuple with student info
data[sectionCode].append([studentN, studentName, float(midtermGrade)]
#make calculations
for k,v in data.iteritems():#iteritems returns you (key, value) pair on each iteration
print 'Section:' + k + ' Grade:' + str(sum(x[2] for x in v['grade']))
more or less:
infile = open('grade_file.txt', 'r')
score = 0
n = 0
for line in infile.readlines():
score += float(line.rstrip().split()[-1])
n += 1
avg = score / n
The sample CSV file looks like following:
aa danny james
1 3 6
aa bla h mno
1 3 6
aa danny james
1 10 15
I want to read this CSV file and prints output in the following form :
line number james danny
2 6 3
7 15 10
I tried using re.search to start finding the element having value 'Danny' but it came out as error.
import csv, re
file_location=r'D:\\'
spamReader = csv.reader(open(file_location))
for row in spamReader:
if re.search('Danny', row):
print(row)
I am not sure if I have to use dictionary or something else to get the desired output.
This isn't a particularly elegant solution, but was the easiest way I could come up with.
>>> with open('test.test') as f:
... num_to_scores = {}
... for linenum, line in enumerate(f):
... splitline = line.split()
... if 'danny' in line and 'james' in line:
... store_next_vals = True
... dannyloc, jamesloc = splitline.index('danny'), splitline.index('james')
... else:
... if store_next_vals:
... num_to_scores[linenum] = {"james":splitline[jamesloc], "danny":splitline[dannyloc]}
... store_next_vals = False
>>> with open('out.test', 'w') as f:
... f.write('line number,james,danny\n')
... for linenum in num_to_scores:
... f.write('%d,%s,%s\n' % (linenum, num_to_scores[linenum]['james'], num_to_scores[linenum]['danny']))
>>> print open('out.test').read()
line number,james,danny
1,6,3
7,15,10