简体   繁体   English

字典文本文件Python

[英]Dictionary text file Python

text 文本

Donald Trump:
791697302519947264,1477604720,Ohio USA,Twitter for iPhone,5251,1895
Join me live in Springfield, Ohio!
Lit
<<<EOT
781619038699094016,1475201875,United States,Twitter for iPhone,31968,17246
While Hillary profits off the rigged system, I am fighting for you! Remember the simple phrase: #FollowTheMoney... 
<<<EOT

def read(text):
    with open(text,'r') as f:
        for line in f:

Is there a way that i can separate each information for the candidates So for example for Donald Trump it should be 有没有办法可以为候选人分开每个信息所以例如唐纳德特朗普应该是这样

[
[Donald Trump],
[791697302519947264[[791697302519947264,1477604720,'Ohio USA','Twitter for iPhone',5251,18951895], 'Join['Join me live in Springfield, Ohio! Lit']Lit']],
[781619038699094016[[781619038699094016,1475201875,'United States','Twitter for iPhone',31968,1724617246], 'While['While Hillary profits off the rigged system, I am fighting for you! Remember the simple phrase: #FollowTheMoney...']']]
]

The format of the file is the following: 该文件的格式如下:

ID,DATE,LOCATION,SOURCE,FAVORITE_COUNT,RETWEET_COUNT text(the tweet) ID,DATE,LOCATION,SOURCE,FAVORITE_COUNT,RETWEET_COUNT个文字(推文)

So basically after the 6 headings, everything after that is a tweet till '<< 所以基本上在6个标题之后,之后的所有内容都是推文,直到'<<

Also is there a way i can do this for every candidate in the file 还有一种方法可以为文件中的每个候选人执行此操作

I am not quite understanding... but here is my example to read a file line by line then add that line to a string of text to post to twitter. 我不是很了解......但这是我的例子,逐行读取文件然后将该行添加到文本字符串以发布到twitter。

candidates = open("FILEPATH WITH DOUBLE \") #example "C:\\users\\fox\\desktop\\candidates.txt"

for candidate in candidates():
  candidate = candidate.rstrip('\n') #removes new line(this is mandatory)
  #next line post means post to twitter
  post("propaganda here " + candidate + "more propaganda)

note for every line in that file this code will post to twitter ex.. 20 lines means twenty twitter posts 注意该文件中的每一行此代码将发布到Twitter ex .. 20行表示二十个Twitter帖子

I'm not sure why you need a multi-dimensional list (I would pick tuples and dictionaries if possible) but this seems to produce the output you asked for: 我不确定为什么你需要一个多维列表(如果可能的话我会选择元组和字典)但这似乎产生了你要求的输出:

>>> txt = """Donald Trump:
... 791697302519947264,1477604720,Ohio USA,Twitter for iPhone,5251,1895
... Join me live in Springfield, Ohio!
... Lit
... <<<EOT
... 781619038699094016,1475201875,United States,Twitter for iPhone,31968,17246
... While Hillary profits off the rigged system, I am fighting for you! Remember the simple phrase: #FollowTheMoney... 
... <<<EOT
... Another Candidate Name:
... 12312321,123123213,New York USA, Twitter for iPhone,123,123
... This is the tweet text!
... <<<EOT"""
>>> 
>>> 
>>> buffer = []
>>> tweets = []
>>> 
>>> for line in txt.split("\n"):
...     if not line.startswith("<<<EOT"):
...         buffer.append(line)
...     else:
...         if buffer[0].strip().endswith(":"):
...             tweets.append([buffer.pop(0).rstrip().replace(":", "")])
...         metadata = buffer.pop(0).split(",")
...         tweet = [" ".join(line for line in buffer).replace("\n", " ")]
...         tweets.append([metadata, tweet])
...         buffer = []    
... 
>>> 
>>> from pprint import pprint
>>> 
>>> pprint(tweets)
[['Donald Trump'],
 [['791697302519947264',
   '1477604720',
   'Ohio USA',
   'Twitter for iPhone',
   '5251',
   '1895'],
  ['Join me live in Springfield, Ohio! Lit']],
 [['781619038699094016',
   '1475201875',
   'United States',
   'Twitter for iPhone',
   '31968',
   '17246'],
  ['While Hillary profits off the rigged system, I am fighting for you! Remember the simple phrase: #FollowTheMoney... ']],
 ['Another Candidate Name'],
 [['12312321',
   '123123213',
   'New York USA',
   ' Twitter for iPhone',
   '123',
   '123'],
  ['This is the tweet text!']]]
>>> 

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

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