简体   繁体   中英

Python - convert .txt content into json format

Name: Donald J. Trump
Username: @realDonaldTrump
Post: I look forward to paying my respects to our brave men and women on this Memorial Day at Arlington National Cemetery later this morning.
post's link: https://twitter.com/realDonaldTrump/status/869170615881793536
Replies: 16,259 replies
Retweet: 15,103 retweets
Likes: 90,839 likes
Date: 5:36 AM - 29 May 2017

Hi all, above is the format of each chunk of data in my first.txt file. I would like to read it and change it to to the json format like the one below, and store it to second.txt file.

def convert_to_json(path, name, username, post, link, replies, retweets, likes, retweetby, date, domainname):
with open(path, 'a') as file:
    stringData = [{"ContentUrl": link,
    "Text": post,
    "PublishDate": date.strip(),
    "Title": "",
    "SourceUrl": domainname,
    "SocialNetwork": media,
    "Source": "",
    "Author": name,
    "Like_count": likes.strip(),
    "Replies_count": replies.strip(),
    "Retweets_count": retweets.strip(),
    "Schema": "SOCIAL_MEDIA"}]

    objData = json.load(stringData)
    file.write(stringData)

The above code was suppose to take in data and then append it to second.txt file. However, my code did not manage to append the data i want into my second.txt file. No apparent error was shown at the console, and i seek suggestions and help from all of the experts here.

You are not using json.load correctly; try it like this:

record = {"ContentUrl": link,
    "Text": post,
    "PublishDate": date.strip(),
    "Title": "",
    "SourceUrl": domainname,
    "SocialNetwork": media,
    "Source": "",
    "Author": name,
    "Like_count": likes.strip(),
    "Replies_count": replies.strip(),
    "Retweets_count": retweets.strip(),
    "Schema": "SOCIAL_MEDIA"}  

with open(path, 'a') as file:
    objData = json.load(file)
    objData.append(record)
    file.write(json.dumps(objData))
  1. Try json.dumps instead of load
  2. Why do you wrap you dict in a list?
  3. Write to file the result of the json.dumps, not the object

     with open(path, 'a+') as file: stringData = {"ContentUrl": link, "Text": post, "PublishDate": date.strip(), "Title": "", "SourceUrl": domainname, "SocialNetwork": media, "Source": "", "Author": name, "Like_count": likes.strip(), "Replies_count": replies.strip(), "Retweets_count": retweets.strip(), "Schema": "SOCIAL_MEDIA"} objData = json.dumps(stringData) file.write(objData) 

Add all the data to the list ( stringData ) and finally write the stringData to the second file using

stringData = [] #init

def addToDataToBeWritten(path, name, username, post, link, replies, retweets, likes, retweetby, date, domainname):    

    row = {"ContentUrl": link,
    "Text": post,
    "PublishDate": date.strip(),
    "Title": "",
    "SourceUrl": domainname,
    "SocialNetwork": media,
    "Source": "",
    "Author": name,
    "Like_count": likes.strip(),
    "Replies_count": replies.strip(),
    "Retweets_count": retweets.strip(),
    "Schema": "SOCIAL_MEDIA"}
    stringData.append(raw)

''' Read and call the method here'''
with open(path, 'w') as file:  
    file.write(json.dumps(stringData))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM