如何在Python线程安全的情况下将数据写入文件?我想为每个请求安全地将一些变量保存到文件中,每隔一小时我想要进行一些分组并将其写入mysql.
在Java中,我现在将它放在一个缓存的数组中,当数组已满时将其写入文件.
我怎么能用Python做到这一点?有许多并发请求,因此它必须是线程安全的.
编辑:
我们最终使用了正常工作的日志模块.
从这个链接我使用以下代码:
my_other_string = 'the_boat_has_sunk'
my_list = ['car', 'boat', 'truck']
my_list = re.compile(r'\b(?:%s)\b' % '|'.join(my_list))
if re.match(my_list, my_other_string):
print('yay')
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我想打印__CODE__后__CODE__,它打印此:
re.compile('\\b(?:car|boot|truck)\\b')
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
编辑:
我不能分开单词,__CODE__因为我并不总是知道会出现什么样的字符串.因此,如果列表中的一个单词在字符串中,则不一定是单独的单词.
EDIT2:
改为__CODE__.
我为python3安装了带有pip的fuzzywuzzy.当我做点子列表时,我看到了
fuzzywuzzy (0.8.1)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试导入时,我收到一个错误.
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import fuzzywuzzy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'fuzzywuzzy'
>>>
Run Code Online (Sandbox Code Playgroud)
有没有人有这个问题的经验?
I had the following function in pandas 0.17:
df['numberrows'] = df.groupby(['column1','column2','column3'], as_index=False)[['column1']].transform('count').astype('int')
Run Code Online (Sandbox Code Playgroud)
But I upgraded pandas today and now I get the error:
Run Code Online (Sandbox Code Playgroud)File "/usr/local/lib/python3.4/dist-packages/pandas/core/internals.py",line 3810, in insert raise ValueError('cannot insert {}, already exists'.format(item))
ValueError: cannot insert column1, already exists
What has changed in the update which causes this function to not work anymore?
I want to groupby the columns and add a column which has the amount or rows of the groupby.
If what I did before was …
是否可以从google搜索中获取查询参数?
即,如果有人用谷歌搜索自行车,则网址将变为:
https://www.google.es/search?q=bicycles......
Run Code Online (Sandbox Code Playgroud)
如果您随后进入搜索结果并且有人点击您的页面,您将无法看到带有 document.referrer 的查询参数,它只会显示
https://www.google.es/
Run Code Online (Sandbox Code Playgroud)
有没有办法知道访问者在访问您的网站之前搜索了什么?
我有一个数据框 A:
's' 'S' 'T'
0 'abc' 'a' 12
1 'def' 'b' 15
2 'abc' 'b' 1.4
Run Code Online (Sandbox Code Playgroud)
现在我想拥有 'T' 的值,其中 's' == 'abc' 和 'S' == 'b'
所以我试过:
idx = (A['s'] == 'abc') & (A['S'] == 'b')
Run Code Online (Sandbox Code Playgroud)
但我看到 .get_value() 已被弃用,并且:
number = A.at[idx,'T']
Run Code Online (Sandbox Code Playgroud)
给出这个错误:
ValueError: At based indexing on an integer index can only have integer indexers
Run Code Online (Sandbox Code Playgroud)
编辑:
number = A.loc[idx, 'T']
Run Code Online (Sandbox Code Playgroud)
返回数据帧而不是值(整数或浮点数)
print(number)
2 1.4
Name: T, dtype: float64
Run Code Online (Sandbox Code Playgroud)
这样做时:
number2 = 1.3
if (number != number2):
Run Code Online (Sandbox Code Playgroud)
我得到:
ValueError: …Run Code Online (Sandbox Code Playgroud) 我在尝试使用 Python 将数据加载到 Redis 时收到此错误。
这是代码:
zkey = 'test'
k = 15648
nval = '15648-barry'
redis.zadd(zkey, k, nval)
Run Code Online (Sandbox Code Playgroud)
这是错误:
Traceback (most recent call last):
File "test.py", line 131, in main
redis.zadd(zkey, k, nval)
File "/usr/local/lib/python3.6/dist-packages/redis/client.py", line 2320, in zadd
for pair in iteritems(mapping):
File "/usr/local/lib/python3.6/dist-packages/redis/_compat.py", line 122, in iteritems
return iter(x.items())
AttributeError: 'int' object has no attribute 'items'
Run Code Online (Sandbox Code Playgroud)
我在 Github 上发现了这个问题: https ://github.com/rq/rq/issues/1014
问题已解决,解决方案应该是安装 RQ 0.13 我运行:
sudo pip3 install rq
Run Code Online (Sandbox Code Playgroud)
并且安装成功。然后重新启动redis-server。
但是我仍然遇到同样的错误。
这个问题还有其他解决方案吗?
眼镜:
Python 3.6.7
RQ 0.13
Redis-Server …Run Code Online (Sandbox Code Playgroud) 我有一个包含一组整数的字典。
{'A': {9, 203, 404, 481},
'B': {9},
'C': {110},
'D': {9, 314, 426},
'E': {59, 395, 405}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用以下方法生成数据:
data = {}
for i in string.ascii_uppercase:
n = 25
rng = np.random.default_rng()
data[i] = set(rng.choice(100, size=n, replace=False))
Run Code Online (Sandbox Code Playgroud)
我需要获取字典子集的交集列表。因此,在示例中,['A','B','D'] 的交集的输出将返回 [9]
我已经想出了 2 种不同的方法来做到这一点,但是当套装价值增长时,这两种方法都会变慢。
cols = ['A','B','D']
# method 1
lis = list(map(data.get, cols))
idx = list(set.intersection(*lis))
#method 2 (10x slower then method 1)
query_dict = dict((k, data[k]) for k in cols)
idx2 = list(reduce(set.intersection, (set(val) for val in query_dict.values()))) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 json 文件读入数据帧。
df = pd.read_json('test.log', lines=True)
Run Code Online (Sandbox Code Playgroud)
然而,有些值是 int64 和 Pandas 引发的:
ValueError: Value is too big
Run Code Online (Sandbox Code Playgroud)
我尝试设置precise_float为True,但这并没有解决它。
当我逐行执行时,它会起作用:
df = pd.DataFrame()
with open('test.log') as f:
for line in f:
data = json.loads(line)
df = df.append(data, ignore_index=True)
Run Code Online (Sandbox Code Playgroud)
然而,这是非常缓慢的。对于大约 50k 行的文件,它需要很长时间。
有没有办法可以将某些列的值设置为使用 int64?
我需要从REST API中检索数据.
在Centos shell中我可以做到:
curl -H "ID:1234" -H "Password:ABC" http://url.com/curl
Run Code Online (Sandbox Code Playgroud)
我试图用Python中的Requests做到这一点.
在快速入门页面上,我看到:
payload = {'ID': '1234' , 'Password' : 'ABC' }
requests.get("http://url.com/curl", params=payload)
Run Code Online (Sandbox Code Playgroud)
但这不起作用.它只返回状态200,但没有数据.
我正在阅读有关Python函数编程的文章(3)。
但是我在文本中不明白这个例子:
class Bus(object):
passengers = set()
def add_passenger(self, person):
self.passengers.add(person)
bus1 = Bus()
bus2 = Bus()
bus1.add_passenger('abe')
bus2.add_passenger('bertha')
bus1.passengers # returns ['abe', 'bertha']
bus2.passengers # also ['abe', 'bertha']
Run Code Online (Sandbox Code Playgroud)
为什么在该类的bus1实例上调用add_passenger()会更改bus2的乘客集?
当您不希望这种行为时,正确的方法是什么?
python ×10
pandas ×4
python-3.x ×2
curl ×1
fuzzywuzzy ×1
javascript ×1
json ×1
redis ×1
redis-py ×1
regex ×1
set ×1
version ×1