简体   繁体   English

Python-将.txt内容转换为json格式

[英]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. 大家好,以上是我的first.txt文件中每个数据块的格式。 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. 我想阅读并将其更改为类似于以下格式的json格式,并将其存储到second.txt文件中。

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. 上面的代码假定要接收数据,然后将其附加到second.txt文件中。 However, my code did not manage to append the data i want into my second.txt file. 但是,我的代码无法将我想要的数据追加到我的second.txt文件中。 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; 您没有正确使用json.load 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 试试json.dumps而不是load
  2. Why do you wrap you dict in a list? 为什么将字典存储在列表中?
  3. Write to file the result of the json.dumps, not the object 将json.dumps的结果而不是对象写入文件

     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 ),最后使用以下命令将stringData写入第二个文件

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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