Python Forum
Help me with python read file and save file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help me with python read file and save file
#6
import json

file = open("input.txt", "r")
print ("Name of the file is : ",file.name)
print(file.read())

with open('input.txt') as f:
    read_data = f.read()
file.closed

#a for loop will be created to look for emails, password and email associate with
#re.findall(r'([a-z0-9]+(\.[a-z0-9]+)*@[a-z]+(\.[a-z]+)+)

#trying to read the file from input.txt and safe the output to output.txt with the help of json.dumps
file2 = open("output.txt", "rb+")
json.dumps(file)


file2.closed
Traceback (most recent call last):
  File "p.py", line 16, in <module>
    json.dumps(file)
  File "/usr/lib/python3.6/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'TextIOWrapper' is not JSON serializable

This is the file called input.txt
Quote:"1":"[email protected]","p":"password1","r":"PYTHON DEMO OUTPUT","d":"yahoo.com"
"2":"[email protected]","p":"password2","r":"PYTHON DEMO OUTPUT","d":"mail.bg"
"3":"[email protected]","p":"password3","r":"PYTHON DEMO OUTPUT,"d":"ya.ru"
"4":"[email protected]","p":"password4","r":"PYTHON DEMO OUTPUT","d":"yandex.ua"
"5":"[email protected]","p":"password5","r":"PYTHON DEMO OUTPUT","d":"yandex.by"
"6":"[email protected]","p":"password6","r":"PYTHON DEMO OUTPUT","d":"yahoo.com"
"7":"Demo_V;7829837","r":"PYTHON DEMO OUTPUT","d":null
"8":"[email protected]","p":"password8","r":"PYTHON DEMO OUTPUT","d":"gmail.com"
"9":"demo9_email@Tut.by","p":"password9","r":"PYTHON DEMO OUTPUT","d":"tut.by"
"10":"[email protected]","p":"password10","r":"PYTHON DEMO OUTPUT,"d":"t-online.de"

by default output.txt is empty file. I want to grab the email using the function
Quote:findall(r'([a-z0-9]+(\.[a-z0-9]+)*@[a-z]+(\.[a-z]+)+)
and also grab password and the last line. So after the scraping the out should look like this

Quote:[email protected]:password1:yahoo.com
[email protected]:password2:ymail.bg
[email protected]:password3:ya.ru
[email protected]:password4:yandex.ua
[email protected]:password5:yandex.by
[email protected]:password6:yahoo.com
[email protected]:password8:gmail.com
demo9_email@Tut.by:password9:tut.by
[email protected]:password10:t-online.de

Line 7 has a value "null" so if the null value is found skip the line and continue outputting other lines.
the line 7 has bee removed because it encounter the null value
Quote:"7":"Demo_V;7829837","r":"PYTHON DEMO OUTPUT","d":null
Reply


Messages In This Thread
RE: Help me with python read file and save file - by wereak - Apr-02-2019, 07:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python coding to run file NSR115 1 169 Jun-18-2024, 11:05 AM
Last Post: Kajalishu
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 718 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Delete file with read-only permission, but write permission to parent folder cubei 6 22,431 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  Cannot get cmd to print Python file Schauster 11 721 May-16-2024, 04:40 PM
Last Post: xMaxrayx
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 427 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Python openyxl not updating Excel file MrBean12 1 510 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 539 Feb-15-2024, 11:15 AM
Last Post: rawatg
  Open/save file on Android frohr 0 444 Jan-24-2024, 06:28 PM
Last Post: frohr
  connect sql by python using txt. file dawid294 2 580 Jan-12-2024, 08:54 PM
Last Post: deanhystad
  file open "file not found error" shanoger 8 1,542 Dec-14-2023, 08:03 AM
Last Post: shanoger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020