社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

DirtyBit

DirtyBit 最近创建的主题
DirtyBit 最近回复了
5 年前
回复了 DirtyBit 创建的主题 » Python:如何使用regex显示文本文件中的前几个数字

从我上面的评论来看:

  1. 读取两个文件并将它们的行存储在列表中

  2. 平展列表

  3. 按字符串中的视图对列表排序

因此 :

list.txt文件:

file Marvel/GuardiansOfGalaxy 300 1
file DC/Batman 504 1
file GameOfThrones 900 0
file DC/Superman 200 1
file Marvel/CaptainAmerica 342 0

列表2.txt:

file Science/Biology 200 1
file Math/Calculus 342 0
file Psychology 324 1
file Anthropology 234 0
file Science/Chemistry 444 1

以及 :

fileOne = 'list.txt'
fileTwo = 'list2.txt'

result = []
with open (fileOne, 'r') as file1Obj, open(fileTwo, 'r') as file2Obj:
      result.append(file1Obj.readlines())
      result.append(file2Obj.readlines())

result = sum(result, [])                 # flattening the nested list
result = [i.split('\n', 1)[0] for i in result]  # removing the \n char

print(sorted(result, reverse=True, key = lambda x: int(x.split()[2]))) # sorting by the view

输出 :

[
 'file GameOfThrones 900 0', 'file DC/Batman 504 1', 'file Science/Chemistry 444 1', 
 'file Marvel/CaptainAmerica 342 0', 'file Math/Calculus 342 0', 
 'file Psychology 324 1', 'file Marvel/GuardiansOfGalaxy 300 1', 
 'file Anthropology 234 0', 'file DC/Superman 200 1', 'file Science/Biology 200 1'
]

较短版本 :

with open (fileOne, 'r') as file1Obj, open(fileTwo, 'r') as file2Obj: result = file1Obj.readlines() + file2Obj.readlines()    
print(list(i.split('\n', 1)[0] for i in sorted(result, reverse=True, key = lambda x: int(x.split()[2]))))   # sorting by the view
4 年前
回复了 DirtyBit 创建的主题 » 用python打印

你不需要这个:

from py_translator import Translator

测试时间 第3.6页 :

import sys

from translate import Translator
translator= Translator(from_lang="hin",to_lang="en")
translation = translator.translate("१३")
print (translation)

输出 :

13
5 年前
回复了 DirtyBit 创建的主题 » Python XML元素树查找XML标记的值

使用 BeautifulSoup :

from bs4 import BeautifulSoup
import urllib

test = '''<data>
    <items>
        <item>1</item>
    </items>
    <items>
        <item>2</item>
    </items>
    <items>
        <item>3</item>
    </items>
    <items>
        <item>4</item>
    </items>
</data>'''

soup = BeautifulSoup(test, 'html.parser')
data = soup.find_all("item")

for d in data:
    print(d.text)

输出:

1
2
3
4

使用 XML Element Tree :

from xml.etree import ElementTree
tree = ElementTree.parse('list.txt')
root = tree.getroot()

items = root.findall("items")

for elem in items:
    desired_tag = elem.find("item")
    print(desired_tag.text)

输出:

1                
2                
3                
4

编辑:

如果你想把它们打印成一行,用空格隔开。

print(desired_tag.text, "\t", end = "")
5 年前
回复了 DirtyBit 创建的主题 » 在Python3.x中查找数组是否存在且不为空
json_data = json.dumps({
    "attributes": [
        {
            "name": "test"
        }
    ]
})


item_dict = json.loads(json_data)
print(type((item_dict['attributes'])))    # list
print(len((item_dict['attributes'][0])))     # 1
5 年前
回复了 DirtyBit 创建的主题 » 删除python中的双引号

我相信这就是你想要的:

str_list = ['Test1', 'Test2', 'Test3']
# desired output:  ('Test1', 'Test2', 'Test3')

# print(tuple(str_list))
print(str(tuple(str_list)).rstrip(',)') + ')')

输出:

('Test1', 'Test2', 'Test3')
5 年前
回复了 DirtyBit 创建的主题 » 使用python解析json行

如评论中所述,您需要 self.on_data(json.loads(line)) :

lines = rec.split("\n")
rec = ''
size = len(lines)
i=0
for line in lines:
    try:
        self.on_data(json.loads(line))
    except:
        logging.warning ('warning, could not parse line:', line)
        if i == size - 1:
            # if it is the last element, we can keep it, since it might not be complete
            rec+=line
    finally:
        i += 1